client socket sends data but server socket does not receive them. c++ buffered stream?

rnd_nr_gen picture rnd_nr_gen · Oct 29, 2010 · Viewed 7.1k times · Source

I am working on a project where a partner provides a service as socket server. And I write client sockets to communicate with it. The communication is two way: I send a request to server and then receive a response from server.

The problem is that I send the data to the server but apparently the server cannot receive the data.

From my side I just use very simple implementation just like the example from http://www.linuxhowtos.org/C_C++/socket.htm

#include <sys/socket.h>

socket_connect();

construct_request_data();

send(socket, request_data, request_length, 0/*flag*/); // I set flag as 0

// now the server should receive my request and send response to me    

recv(socket, response_data, response_length, 0);   

socket_close();

And it seems that the server socket is implemented with a "binding" to std::iostream and it is buffered stream. (i.e. the socket send/recv is done in iostream::write/read.)

server_socket_io >> receive_data;

server_socket_io << response_data; 

Btw, I got a test client from my partner and it is wrapped in a iostream as well. The test socket client can communicate with the server without problem, but it must do iostream::flush() after every socket send.

But I want to just keep it simple not to wrap my socket client in iostream.

I just wonder whether the buffered iostream results in the problem: the data is not processed since the data the client socket sent is just in very small amount and still buffered.

Or could it be my problem? how can I know if I really send out the data? does my client socket also buffer the data?

I have tried some "bad" workaround with TCP_NODELAY but it didn't help!

How can I solve the problem? from client side? or server side? Should I close the socket after sending request and before receiving response, so that the data will be "flushed" and processed?

or should I wrap my socket in iostream and do flush?

or the server socket should use a "unbuffered" stream?

thanks for any suggestion and advice!

Answer

Himanshu picture Himanshu · Oct 29, 2010

Further to Jay's answer, you can try any network packet sniffer and check whether your packets are getting to the server or not. Have a look at wireshark or tcpdump.