Azmisov
Azmisov

Reputation: 7243

C/C++ server not sending last few lines of file

I am writing a simple server in C/C++. I have everything mostly complete, but there is one problem. The server fails to send the last three lines of a file to a client. I assume I am closing the socket connection prematurely, but my attempts to remedy this have failed. For example, calling

shutdown(clientSckt, SHUT_RDWR);

right before calling the close() method for the client socket. And adding a latency to the socket parameters like so:

struct linger l;
l.l_onoff = 1;
l.l_linger = 1;
setsockopt(clientSckt, SOL_SOCKET, SO_LINGER, &l, sizeof(l));

after it has been opened. But neither of these seem to work. The server writes everything with no errors, but the client is not receiving everything.

Upvotes: 0

Views: 104

Answers (2)

Azmisov
Azmisov

Reputation: 7243

It turns out, I forgot to add the character length of the header to the length of the file I was sending over. Hence, the client was closing the connection before the server had sent everything over.

Upvotes: 0

Ron Burk
Ron Burk

Reputation: 6231

From vague memory: a) if you want to use SO_LINGER, use close(). b) more robust is do a half shutdown

shutdown(clientSckt, SHUT_WR)

and then read() until you get a 0.

Upvotes: 1

Related Questions