我希望有一台正在运行的服务器,并从其他Java应用程序等客户端接收消息。我通过带有InputStream的BufferedReader执行此操作,只要我一次按预期工作。消息由方法处理并在屏幕上写入接收消息的测试消息,但是如果我让它在while循环中运行则表示 -
COMMAND=`eval aws ecr get-login --region us-west-2`
echo `eval $COMMAND`
因此,一旦服务器收到消息,我就不知道如何获得第二个或任何后续的消息。
我的主要源代码是:
java.net.SocketException: Connection reset
创建流的两个类看起来像这样:
public static void main (String[] args) {
int port = 13337;
BufferedReader msgFromClient = null;
PrintWriter msgToClient = null;
timeDate td = new timeDate(); //Class i did for myself to get time/date
ServerSocket s_socket = null;
try {
s_socket = new ServerSocket(port);
System.out.println("Server startet at "+td.getCurrDate()+" "+td.getCurrTime());
}
catch (IOException ioe) {
System.out.println("Server on Port "+port+" couldnt be created. \nException: "+ioe.getMessage());
}
Socket socket = null;
try {
socket = s_socket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
msgFromClient = utils.createInputStream(socket);
} catch (IOException ioe) {
System.out.println("Creation of an Input Stream failed.\n Exception - "+ioe);
}
try {
msgToClient = utils.createOutputStream(socket);
} catch (IOException ioe) {
System.out.println("Creation of an Output Stream failed.\n Exception - "+ioe);
}
String input = null;
while (true) {
try {
input = msgFromClient.readLine();
} catch (IOException ioe) {
System.out.println(ioe);
}
if(input!=null) {
System.out.println("Jumping out of loop: "+input);
utils.processCode(input);
}
}
" processCode"然后,班级只是一个开关。
答案 0 :(得分:0)
Socket socket = null;
try {
socket = s_socket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您只接受一个连接,此后您正在进行处理。您需要为每个连接打开一个新的线程
ExecutorService threadLimit = Executors.newFixedThreadPool(10);
while(true) {
Socket s = serverSocket.accept();
treadLimit.submit(new HandleThread(s));
}