Returning a Response

Socket clientSock = serverSocket.accept();
											DataOutputStream out =
											new DataOutputStream(
											clientSock.getOutputStream());
											out.writeInt(someValue);
											out.close();

This phrase shows an example of how to return a response from a server to a client. The accept() method of the ServerSocket instance will return a Socket instance when a connection is made with a client. We then get the socket’s output stream by calling the getOutputStream() method of the socket. We use the output stream to instantiate a DataOutputStream, which we then call the writeInt() method on to write an integer value, sending binary data to the client. Finally, we close the socket using the close() method of the Socket.

In this phrase, we use the write() method, ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.