我有一个应用程序允许服务器(在NODEJS)和Java应用程序之间的通信,但它在服务器端向我发送错误 . 这是错误:
错误:在TCP.onread(net.js:615:25)的_errnoException(util.js:1022:11)处读取ECONNRESET
这是我的代码
the sever in nodeJS:
const net = require('net');
const server = net.createServer((s) => {
// 'connection' listener
console.log('client connected');
s.on('end', () => {
console.log('client disconnected');
});
s.write('hello from server\r\n');
s.pipe(s);
s.write('ILYES\r\n');
s.pipe(s);
s.end();
});
server.on('data', ()=>{
Console.log(data);
});
server.on('error', (err) => {
throw err;
});
server.listen(6969, () => {
console.log('server bound');
});
和我的 JAVA code
package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class NodeJsEcho {
// socket object
private Socket socket = null;
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException{
// class instance
NodeJsEcho client = new NodeJsEcho();
// socket tcp connection
String ip = "127.0.0.1";
int port = 6969;
client.socketConnect(ip, port);
// writes and receives the message
String message = "message123";
System.out.println("Sending: " + message);
String returnStr = client.echo(message);
System.out.println("Receiving: " + returnStr);
String returnStr1 = client.echo(message);
System.out.println("Receiving: " + returnStr1);
String bourouba = client.echo(message);
System.out.println("Receiving: " + bourouba);
}
// make the connection with the socket
private void socketConnect(String ip, int port) throws UnknownHostException, IOException{
System.out.println("[Connecting to socket...]");
this.socket = new Socket(ip, port);
}
// writes and receives the full message int the socket (String)
public String echo(String message){
try {
// out & in
PrintWriter out = new PrintWriter(getSocket().getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));
// writes str in the socket and read
out.println(message);
String returnStr = in.readLine();
return returnStr;
} catch (IOException e){e.printStackTrace();}
return null;
}
// get the socket instance
private Socket getSocket(){
return socket;
}
}