c - Only one write() call sends data over socket connection -


first stackoverflow question! i've searched...i promise. haven't found answers predicament. have...a severely aggravating problem least. make long story short, developing infrastructure game mobile applications (an android app , ios app) communicate server using sockets send data database. end server script (which call bes, or end server), several thousand lines of code long. essentially, has main method accepts incoming connections socket , forks them off, , method reads input socket , determines it. of code lies in methods send , receive data database , sends mobile apps. of them work fine, except newest method have added. method grabs large amount of data database, encodes json object, , sends mobile app, decodes json object , needs do. problem data large, , of time not make across socket in 1 data write. thus, added 1 additional data write socket informs app of size of json object receive. however, after write happens, next write sends empty data mobile app.

the odd thing is, when remove first write sends size of json object, actual sending of json object works fine. it's unreliable , have hope sends in 1 read. add more oddity situation, when make size of data second write sends huge number, ios app read properly, have data in middle of otherwise empty array.

what in world going on? insight appreciated! below basic snippet of 2 write commands on server side.

keep in mind everywhere else in script read's , write's work fine, place 2 write operations back.

the server script on ubuntu server in native c using berkeley sockets, , ios using wrapper class called asyncsocket.

int n; //outputmessage contains string tells mobile app how long next message //(returndata) n = write(sock, outputmessage, sizeof(outputmessage)); if(n < 0)    //error handling here //returndata json encoded string (well, char[] exact, native-c) n = write(sock, returndata, sizeof(returndata)); if(n < 0)    //error handling here 

the mobile app makes 2 read calls, , gets outputmessage fine, returndata bunch of empty data, unless overwrite sizeof(returndata) hugely large number, in case, ios receive data in middle of otherwise empty data object (nsdata object, exact). may important note method use on ios side in asyncsocket class reads data length receives first write call. if tell read, 10000 bytes, create nsdata object of size , use buffer when reading socket.

any greatly, appreciated. in advance everyone!

it's unreliable , have hope sends in 1 read.

the key successful programming tcp there no concept of tcp "packet" or "block" of data @ application level. application sees stream of bytes, no boundaries. when call write() on sending end data, tcp layer may choose slice , dice data in way sees fit, including coalescing multiple blocks together.

you might write 10 bytes 2 times , read 5 15 bytes. or maybe receiver see 20 bytes @ once. cannot "hope" chunks of bytes send arrive @ other end in same chunks.

what might happening in specific situation 2 back-to-back writes being coalesced one, , reading logic can't handle that.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -