c++ - Java Tcp socket: how to detect on client that no more data needs to be received by server? -


i sending byte[] on java socket c++ server. client doesn't know how data sent in advance. there other way detect on client side data sent socket can disconnected without data loss on server side? using thread.sleep(1000) not disconnecting before data has been received there better way this?

server.cpp:

do{  memset(chunk ,0 , 4096);  n = recv(sock , chunk , 4096 , 0);  myfile.write(chunk, n); } while (n>0); 

client.java

while(issending){  try {   dataoutputstream out = new dataoutputstream(socket.getoutputstream());   data = getdatafromqueue();   out.write(data,0,data.length);   out.flush();  } catch (ioexception e) {   // todo auto-generated catch block  e.printstacktrace();  } }  //how wait here until data received?  try {   socket.close(); } catch (ioexception e) {   // todo auto-generated catch block   e.printstacktrace(); } 

the tcp connection full-duplex. there 2 independent byte streams, 1 in each direction. can close write half of connection , called half-close. data in socket send buffer sent, followed tcp's normal connection termination sequence. server read 0 bytes in call recv indicates shutdown client side.

i not java expert quick googling telling me shutdownoutput() function using in java close sending half of connection.

http://docs.oracle.com/javase/6/docs/api/java/net/socket.html#shutdownoutput()


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -