I am not sure about the "// Server" marking in your question, but I assume the following code is the server code:
InputStream in = client.getInputStream(); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); int i = 0; while((i = in.read()) >= 0) { bOut.write(i); } String complete = new String(bOut.toByteArray(), "UTF-8");
This will continue, blocking each time until it receives a value from read() less than zero. This only happens if the stream is closed.
It looks like you need to create your own protocol. Therefore, instead of searching "<= 0", find some constant value that signals the completion of the message.
Here's a quick demonstration of what I mean (I didn't have time yesterday). I have 3 classes, Message , MyClient (also the main class) and MyServer . Please note that there is no information about sending or receiving a new line. TcpNoDelay sets nothing. But it works great. Some other notes:
- This code sends and receives only one request and response.
- It does not support sending multiple instances of
Message . This will require checking the beginning of Message as well as the end.
Message class:
public class Message { public static final String MSG_START = "<message>"; public static final String MSG_END = "</message>"; private final String content; public Message(String string){ content = string; } @Override public String toString(){ return MSG_START + content + MSG_END; } }
MyServer class
public class MyServer implements Runnable{ public static final int PORT = 55555; @Override public void run(){ try { ServerSocket serverSocket = new ServerSocket(PORT); Socket socket = serverSocket.accept(); String message = getMessage(socket); System.out.println("Server got the message: " + message); sendResponse(socket); }catch (IOException e){ throw new IllegalStateException(e); } } private void sendResponse(Socket socket) throws IOException{ Message message = new Message("Ack"); System.out.println("Server now sending a response to the client: " + message); OutputStream out = socket.getOutputStream(); out.write(message.toString().getBytes("UTF-8")); } private String getMessage(Socket socket) throws IOException{ BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); StringBuilder sb = new StringBuilder(100); byte[] bytes = new byte[1024<<8]; while(sb.lastIndexOf(Message.MSG_END) == -1){ int bytesRead = in.read(bytes); sb.append(new String(bytes,0,bytesRead,"UTF-8")); } return sb.toString(); } }
MyClient class
public class MyClient { public static void main(String[] args){ MyClient client = new MyClient(); Thread server = new Thread(new MyServer()); server.start(); client.performCall(); } public void performCall(){ try { Socket socket = new Socket("127.0.0.1",MyServer.PORT); sendMessage(socket, "Why hello there!"); System.out.println("Client got a response from the server: " + getResponse(socket)); } catch (IOException e) { throw new IllegalStateException(e); } } public String getResponse(Socket socket) throws IOException{ String response; StringBuilder sb = new StringBuilder(100); InputStream in = socket.getInputStream(); byte[] bytes = new byte[1024]; while(sb.lastIndexOf(Message.MSG_END) == -1){ int bytesRead = in.read(bytes); sb.append(new String(bytes,0,bytesRead,"UTF-8")); } response = sb.toString(); return response; } public void sendMessage(Socket socket, String message) throws IOException{ Message msg = new Message(message); System.out.println("Client now sending message to server: " + msg); OutputStream out = socket.getOutputStream(); out.write(msg.toString().getBytes("UTF-8")); } }
Exit
Client now sending message to server: Why hello there!
Server got the message: Why hello there!
Server now sending a response to the client: Ack
Client got a response from the server: Ack
Process finished with exit code 0
Madconon
source share