简体   繁体   中英

C++ linux proxy server communications

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.

我不知道你的服务器和客户端,但可能是服务器实际上close了由于recv而导致0的套接字,并且关闭的原因可能是你从客户端接收bytecount数据但是将buffer_len数据发送到服务器有效数据发送到服务器后可能会导致一些无效数据!

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM