c - Winsock: How to stop sending/receiving without closing the connection -


i develop client/server application share camera images. general cycle is:

  1. the client captures camera image , compress binary image. after sends server
  2. the server knows how large buffer (640*480/8 bytes) able receive hole image 1 recv-call. next recv-call second image , on
  3. after receiving image server works image , send image client. here problem: image jpeg compressed , client doesn't know filesize , can't receive image server fixed bufferlength.

i think wouldn't problem if server close connection after sending image. don't want close connection because in next cycle send next image on same connection.

my question is: how can tell client server send hole image without closing connection?

here's code: (the client sends binary image server)

void sendtoserver(char* sendbuf, int sendbuflen) {     int iresult = send(connectsocket, sendbuf, sendbuflen, 0);     if (iresult == socket_error) {         (*errcallbackfunc)("sending image error", -1);     } } 

(the server receives image 1 functioncall - recbuflen = 640*480/8)

int receivecameraimages(aruint8* dataptr) { int         iresult;  iresult = recv(clientsocket, recvbuf, recvbuflen, 0); if (iresult > 0) {     if ((*(recvbuf+0)) != 'u' || (*(recvbuf+((xsize*ysize/8)-1))) != 'u')         return receiving_incomplete;      convertbwchartobgr(recvbuf, dataptr, recvbuflen);      return receiving_succeeded; } else if (iresult == 0) {     (*errcallbackfunc)("connection closing", -1);     return receiving_conn_closed; } else {     (*errcallbackfunc)("recv failed", wsagetlasterror());     return receiving_error; } } 

(now server works image , send jpeg compressed image back)

int sendobjectimage(aruint8* dataptr, int buflen) { int iresult;  /* send openglbuffer client */ iresult = send(clientsocket, dataptr, buflen, 0); if (iresult == socket_error) (*errcallbackfunc)("send openglbuffer client failed", wsagetlasterror()); } 

(and client doesn't know how large buffer receives 512byte parts)

int receivefromserver(aruint8* dataptr) { int iresult, returnbuflen = 0; int recvbuflen = default_buffer_len; aruint8 recvbuf[default_buffer_len];  /* receive opengl buffer */ {     iresult = recv(connectsocket, recvbuf, recvbuflen, 0);     if (iresult > 0) {         printf("bytes received: %d\n", iresult);         returnbuflen += iresult;     }     else if (iresult == 0)         printf("connection closed\n");     else         printf("recv failed: %d\n", wsagetlasterror()); } while (iresult >= default_buffer_len);  dataptr = recvbuf;  return returnbuflen; } 

with while (iresult >= default_buffer_len) thought stop receiving (because if part smaller 512bytes must end). recvbuf contains last bytes.

maybe it's possible don't understand hole winsock stuff, thought recvbuf contain hole buffer received. wrong? how hole received buffer without closing connection?

  1. don't rely on single calls recv() , send() being able run completion , process amount of data asked for. must prepared run them several times in loop until desired amount of data has been transferred.

  2. for case when server wants return image of unknown length, send length first, using well-defined format (for instance 32-bit integer in network byte order). client can 2 separate receives, 1 size , 1 data. again, doesn't mean can 2 recv() calls.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -