您好我正在尝试使用以下命令将一些字符串发送到远程服务器:
##1.2.3.4 is IP address of remote server
echo "hey" | nc 1.2.3.4 7728
>> hey
>> null
它不会出现循环,而在远程服务器中我遇到空指针异常。有人可以帮我解决这个错误吗?
New connection with client# 2 at Socket[addr=/1.2.3.4,port=55757,localport=7728]
Exception in thread "Thread-2" java.lang.NullPointerException
at org.o7planning.tutorial.socketthread.ServerProgram$ServiceThread.run(ServerProgram.java:85)
需要知道:
PS:客户端在Linux上运行。和服务器是基于Java的。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerProgram {
public static void main(String args[]) throws IOException {
ServerSocket listener = null;
System.out.println("Server is waiting to accept user...");
int clientNumber = 0;
// Try to open a server socket on port 7777
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)
try {
listener = new ServerSocket(7722);
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
try {
while (true) {
// Accept client connection request
// Get new Socket at Server.
Socket socketOfServer = listener.accept();
new ServiceThread(socketOfServer, clientNumber++).start();
}
} finally {
listener.close();
}
}
private static void log(String message) {
System.out.println(message);
}
private static class ServiceThread extends Thread {
private int clientNumber;
private Socket socketOfServer;
public ServiceThread(Socket socketOfServer, int clientNumber) {
this.clientNumber = clientNumber;
this.socketOfServer = socketOfServer;
// Log
log("New connection with client# " + this.clientNumber + " at " + socketOfServer);
}
@Override
public void run() {
try {
// Open input and output streams
BufferedReader is = new BufferedReader(new InputStreamReader(socketOfServer.getInputStream()));
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socketOfServer.getOutputStream()));
while (true) {
// Read data to the server (sent from client).
String line = is.readLine();
// Write to socket of Server
// (Send to client)
os.write(">> " + line);
// End of line.
os.newLine();
// Flush data.
os.flush();
// If users send QUIT (To end conversation).
if (line.equals("QUIT")) {
os.write(">> OK");
os.newLine();
os.flush();
break;
}
}
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
}