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.
I don’t know your server and client, but may be server actually
closethe socket that cause a 0 as result ofrecv, and the reason of closure may be is that you receivebytecountdata from client but sendbuffer_lendata to the server that may cause some invalid data after valid data to sent to the server!