Purpose socket.shutdownOutput ()

I use the code below to send data to a tcp server. I assume that I need to use socket.shutdownOutput() to correctly indicate that the client sent the request. Is my assumption correct? If not, tell me the purpose of shutdownOutput() . Also appreciate any further optimizations I can do.

Client

 def address = new InetSocketAddress(tcpIpAddress, tcpPort as Integer) clientSocket = new Socket() clientSocket.connect(address, FIVE_SECONDS) clientSocket.setSoTimeout(FIVE_SECONDS) // default to 4K when writing to the server BufferedOutputStream outputStream = new BufferedOutputStream(clientSocket.getOutputStream(), 4096) //encode the data final byte[] bytes = reqFFF.getBytes("8859_1") outputStream.write(bytes,0,bytes.length) outputStream.flush() clientSocket.shutdownOutput() 

Server

 ServerSocket welcomeSocket = new ServerSocket(6789) while(true) { println "ready to accept connections" Socket connectionSocket = welcomeSocket.accept() println "accepted client req" BufferedInputStream inFromClient = new BufferedInputStream(connectionSocket.getInputStream()) BufferedOutputStream outToClient = new BufferedOutputStream(connectionSocket.getOutputStream()) ByteArrayOutputStream bos=new ByteArrayOutputStream() println "reading data byte by byte" byte b=inFromClient.read() while(b!=-1) { bos.write(b) b=inFromClient.read() } String s=bos.toString() println("Received request: [" + s +"]") def resp = "InvalidInput" if(s=="hit") { resp = "some data" } println "Sending resp: ["+resp+"]" outToClient.write(resp.getBytes()); outToClient.flush() } 
+4
source share
2 answers

Socket.shutdownOutput() means that the client has completed the transfer of any data over a TCP connection. It will send the remaining data, and then the completion sequence, which will complete the OUTGOING connection. It is not possible to send any additional data that will also indicate to your program that the request is fully completed. Therefore, it is recommended if you are sure that you no longer need to send data.

But you do not need to indicate that the request is completed (you do not need to open / close the output all the time if you have several requests), there are other ways.

+4
source

I use the code below to send data to a tcp server. I assume that I need to use socket.shutdownOutput () to correctly indicate that the client is sending the request. Is my assumption correct?

YES Your guess is correct. And this ShutDown exit is known as half . Using half the closure, TCP provides the ability for one end of the connection to complete its output, while still receiving data from the other end. Let me guide you through the effects of socket.shutdownOutput() :

  • Locally, the local socket and its input stream behave normally for reading purposes, but for written purposes, the socket and its output stream behave as if the socket was closed for this purpose: subsequent writes to the socket will IOException
  • The standard TCP connection termination sequence (a - FIN, ACK acknowledged) is queued for sending after sending and acknowledging any pending data.
  • Remotely, the remote socket behaves normally for writing, but for read purposes the socket behaves as if it had been closed by this end: further readings from the socket return an EOF condition, i.e. the number of samples from -1 or EOFException , depending on the called method.
  • When the local socket is finally closed, the connection termination sequence has already been sent and is not repeated; if the other end has also done half the closure, all protocol exchanges on the socket are now complete.

Therefore, we see that upon receiving the EOF this end is guaranteed that the other end has completed the exit. And this script is excellently achieved with socket.shutdownOutput() on the other hand.

Source: Java fundamental network, Esmond Pitt

+8
source

All Articles