0
다른 Java 응용 프로그램과 같은 클라이언트에서 메시지를 수신하고 실행중인 서버를 갖고 싶습니다. 나는 BufferedReader를 통해 InputStream과 함께 이것을하고 있는데, 한 번만하면 예상대로 작동한다. 이 메시지는 방법으로 처리하고 화면에 수신 된 메시지의 테스트 메시지를 작성,하지만 난 할 경우는 말한다 while 루프에서 실행됩니다 -서버가 클라이언트로부터 하나 이상의 메시지를 수신하게하는 방법은 무엇입니까?
서버는 내가 잘 모릅니다 메시지를 가지고 그래서 일단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);
}
}
양쪽 클래스는 스트림은 다음과 같이 만들 :
public static BufferedReader createInputStream (Socket socket) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
return br;
}
public static PrintWriter createOutputStream (Socket socket) throws IOException {
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
return pw;
}
은 "processCode"클래스는 단순히 스위치입니다.
에 대한 새로운 스레드를 열어야합니다. 하나의 스레드를 닫으면 (하나의 스레드를 처리 한 후에) 하나 이상의 스레드를 시작할 수 있습니다. –