Reputation: 1497
Trying to build a proxy server.
recv from client -> send to server -> recv from server -> send to client.
The proxy receives the correct data from the client.
But after that I recv 0 bytes from the server.
This is the packet i should recv from the client and send to the server
0A 00 2C 01 23 00 0C 00 B3 01
Here is my code;
//intercept commu by server <-> client
memset(buffer, 0, buffer_len);
//recv from client
if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
fprintf(stderr, "Err: receiving data %d\n", errno);
return 0;
}
//display what we got from the client
printf("Received bytes %d\n", bytecount);
for ( int i = 0; i < bytecount; i++ ) {
printf( "%02x ", static_cast<unsigned char>( buffer[i] ) );
}
printf("\n");
//send to server what we got from the client
if((gbytecount=send(gsock, buffer, buffer_len, 0))== -1){
fprintf(stderr, "Error sending data %d\n", errno);
goto FINISH;
}
//recv from server
if((gbytecount = recv(gsock, buffer, buffer_len, 0))== -1){
fprintf(stderr, "Error receiving data %d\n", errno);
return 0;
}
printf("Received bytes %d\n", gbytecount);
for ( int i = 0; i < gbytecount; i++ ) {
printf( "%02x ", static_cast<unsigned char>( buffer[i] ) );
}
printf("\n");
//send back to client what we got from the server
if((bytecount = send(*csock, buffer, buffer_len, 0))== -1){
fprintf(stderr, "Err: sending data %d\n", errno);
return 0;
}
How do I check what the proxy is sending to the server after recv from the client?
And is there something wrong with my logic?
UPDATE
I guess i found the problem, its because of the buffer_len.
Now I have recv() bytes 93. But the server again sends another packet.
What do I do to my code?
Currently my code is:
recv from client -> send to server -> recv from server -> send to client.
How can I make this code like If the client/server sends something, it will
forward it to the other?
UPDATE
Got to solve it. Thanks.
Upvotes: 1
Views: 1003
Reputation: 19022
You are using blocking calls assuming a direct 1-1 ratio of client->server calls (and vice-versa), but that is almost certainly not going to be the case in a real world application. In reality, the application is going to receive portions of the TCP stream in numerous calls to receive from both the client and the server. You can handle this in two distinct ways. Using select()
to see what sockets need to have data read from them, or using an asynchronous library to facilitate the reading/writing to/from the client/server sockets for you.
Since this seems to come up quite a lot, see my answer to this question: How to make a proxy server in C++ using Boost
See the select() API
Or, here's an example for writing one in boost. There are many other examples if you google "boost::asio proxy server".
Upvotes: 2
Reputation: 661
BigBoss's answer is correct:- you should send bytecount bytes to gsock rather than sending buffer_len bytes. Even better, you should read the size of the packet from the header if possible and read upto that many bytes when possible
Upvotes: 0
Reputation: 6914
I don't know your server and client, but may be server actually close
the socket that cause a 0 as result of recv
, and the reason of closure may be is that you receive bytecount
data from client but send buffer_len
data to the server that may cause some invalid data after valid data to sent to the server!
Upvotes: 2