Java로 표준 다중 클라이언트/서버 프로그램을 만들고 있습니다. 서버는 연결을 받아들이고 각각을 처리 할 새 스레드를 생성합니다. 또한 클라이언트는 서버로부터의 메시지를 기다리는 스레드를 생성합니다. 클라이언트와 서버는 ObjectInputStream 및 ObjectOutputStream을 통해 Message 객체를 전달하여 통신합니다.소켓을 통해 객체를 보내는 Java 클라이언트/서버 문제
초기 핸드 셰이크가 정상적으로 작동합니다. 클라이언트가 시작되면 서버에 대한 소켓 연결을 엽니 다. 서버는 소켓을 받아들이고 연결 성공을 알리는 메시지를 클라이언트에 보냅니다. 그런 다음 클라이언트는 사용자 이름을 다시 보내고 클라이언트와 서버는 모두 메시지 대기를 시작합니다.
그런 다음 채팅 메시지를 생성 한 클라이언트의 텍스트를 보내 서버에 성공적으로 보냅니다. 서버는이 메시지를 수신하고 연결되어있는 모든 클라이언트에게 메시지를 보내려고 시도합니다 (단 하나만 있음). 문제는이 메시지가 절대로 클라이언트로 되돌아 가지 않는다는 것입니다.
// This is Message.send
public void send(ObjectOutputStream stream) throws IOException{
stream.writeObject(this);
}
// ClientThread.run
public void run(){
try {
out = client.getOutputStream();
out.flush();
ObjectInputStream in = client.getInputStream();
Message msg = null;
int len;
byte[] bytes = null;
int i = 0;
// Continuously read new Messages from the server
while(true){
msg = (Message)in.readObject();
processInput(msg);
}
} catch (Exception e) {
Util.showError(e.getMessage(), "Connection Error");
}
System.out.println("Client exited");
}
// ServerThread.run
public void run() {
try {
out = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
Message msg = null;
while(client.isConnected()){
msg = (Message)in.readObject();
processInput(msg);
}
in.close();
client.close();
} catch (Exception e) {
server.addMessage(e.getMessage());
}
}
음, 이것을 통해 slogging 대신 SOAP 또는 REST를 사용하는 것이 좋습니다? – bmargulies
기존 프로토콜을 사용하지 않고 낮은 수준의 소켓으로 연습 할 수있는 운동입니다. – Jared