업데이트 -의 DataInputStream
클라이언트 일관된 유형이 제공 솔루션으로 이동이 서버 소켓에 메시지를 전송, 서버는 원래의 메시지와 함께 클라이언트에 응답합니다. 후자의 기능을 도입 할 때, 서버는 상기 메시지를 계속 수신하기보다는 하나의 메시지만을 수신하고, 클라이언트에 응답하지 않는다. 추가 된 행에 주석 처리됨. 전화 끊기에 대한 통찰력은 대단 할 것입니다.
클라이언트 측 주석 문제와 코드 업데이트 응답에서 :
{ private Socket socket = null;
private BufferedReader console = null;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
while (!line.equals(".bye"))
{ try
{ line = console.readLine();
streamOut.writeUTF(line); //Send console data to server socket
String reply = streamIn.readUTF(); //Recieve confirmation msg from server
System.out.println(reply); //Print the msg
streamOut.flush();
}
public void start() throws IOException
{ console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader
streamIn = new DataInputStream(socket.getInputStream());
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void stop()
{ try
{ if (console != null) console.close();
if (streamOut != null) streamOut.close();
if (streamIn != null) streamIn.close(); //Is it good practice to close
if (socket != null) socket.close();
}
서버 측 문제가 댓글을 달았습니다. 당신은 된 writeUTF 및 readUTF는하지만 난 그게 새로운 라인을 기다립니다으로 차단하는 기대 한 곳의 streamIn.readLine()에서를 사용하는
public void handleClient() throws IOException {
boolean done = false;
try {
System.out.println("Server Thread " + ID + " running.");
while (!done) {
String nextCommand = streamIn.readUTF();
if(nextCommand.equals(".bye")) {
System.out.println("Client disconnected with bye.");
done = true;
} else {
System.out.println(nextCommand);
String nextReply = "\"You sent me ->" +nextCommand.toUpperCase() + "\"\n";
streamOut.writeUTF(nextReply);
}
}
} finally {
streamIn.close();
streamOut.close();
socket.close();
}
}
public void open() throws IOException
{
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
// streamOut = new DataOutputStream(socket.getOutputStream());
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (streamOut != null) streamOut.close();
}
이 파일이 컴파일됩니까? 그렇지 않으면 streamOut.writeBytes (nextReply.getBytes()); ? – Zaki
@ Zaki 코드가 컴파일됩니다. – Astron