1

Is there anyway that will allow to get all data sent via tcp before the session gets closed? What I am getting is I have to close the session from server only after that data is received, and moreover I have to manually pass EOL or carriage-return or "\n" or "\r".

Any help and suggestions is appreciated.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • TCP always delivers all the sent data before you read EOS. What is your question exactly? – user207421 Oct 25 '12 at 07:57
  • But in my case it waits for session closing and then only all data is sent. I tried to extract one character at a time and also all full data but in both the situation i have to loop through with "\r" at the end to read next data. – Anoop Vaidya Oct 25 '12 at 08:04
  • Then clearly you aren't flushing the output at the appopriate points. – user207421 Oct 25 '12 at 09:53
  • Please let me know how can i flush the output before retrieving all the data, as I have to encrypt, deserialize and then render it into my tableviews based on some keys. – Anoop Vaidya Oct 25 '12 at 10:15
  • I'm unclear from the question which side is _not_ receiving the data and which side is closing the connection. Do you mean that you don't receive all data from the server until the server closes the connection? – Brian White Oct 25 '12 at 13:32
  • @Brian : I have a client on my system and apache running on other, database is on third system. I make a request of session from client, the server sends the data from db, but...only after the session gets closed. I dont want the session to be closed now, because once I get the data, I have to perform some related task. I hope now I am able to give you a clear idea of problem. – Anoop Vaidya Oct 26 '12 at 06:32
  • @AnoopVaidya, almost... By "after the session gets closed" do you mean the client closing the session or the server closing the session? Note that an HTTP server is free to close the session if it wishes after transmitting all data whether the client wishes the connection to remain open or not. Also note that the server will not start its response until it sees a blank line after the request, though shutting down client->server side of the link typically has the same result. – Brian White Oct 26 '12 at 12:44
  • @Brian : By "after the session gets closed" I mean client closes the session by "\n" or "\r". Only after this the data is seen. I did not want to close the session right at this point. – Anoop Vaidya Oct 26 '12 at 16:44

2 Answers2

0

You could try setting the SO_LINGER socket option:

GCDAsyncSocket asyncSocket = ...;
struct linger linger;
linger.l_onoff = 1;
linger.l_linger = 30;
int rv = setsockopt([asyncSocket socketFD], SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
if (rv < 0)
{
    // handle error
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • That wouldn't make any difference to the receiver. All it does is delay the sender for up to 30s in close() while pending data is being sent. – user207421 Oct 25 '12 at 23:24
  • @EJP What's happened to all the comments on this answer? – trojanfoe Oct 26 '12 at 06:33
  • 1
    @Anoop-Vaidya This actually solved the issue then? If so, would you are to explain and perhaps get EJP involved too. – trojanfoe Nov 23 '12 at 15:43
0

Is there anyway that will allow to get all data sent via tcp before the session gets closed?

You keep reading data from the socket till EOF is received.

What I am getting is I have to close the session from server only after that data is received

Technically, one can close your write side of the TCP connection, using shutdown(socket, SHUT_WR) indicating that this peer is not going to send any more data. But it will still need to read all the data till EOF is seen and then close(socket).

and moreover I have to manually pass EOL or carriage-return or "\n" or "\r"

This has little to do with sockets or TCP, rather with a higher application protocol, such as HTTP. There are libraries available for this popular protocol that simplify this task.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271