简体   繁体   English

C 代理崩溃

[英]C Proxy crashing

Im writing a program which connects to a browser and sends the http request from the browser to a server, and then sends the response back to the browser, which loads the page with some of the content.我正在编写一个程序,它连接到浏览器并将 http 请求从浏览器发送到服务器,然后将响应发送回浏览器,浏览器会加载包含一些内容的页面。 My program sends things successfully and loads pages, but does not run continuously and will crash after a random amount of time- sometimes 10 seconds of running sometimes 1 minute.我的程序成功发送内容并加载页面,但不会连续运行,并且会在随机时间后崩溃 - 有时运行 10 秒,有时运行 1 分钟。 I want this proxy to be able to run forever.我希望这个代理能够永远运行。 Below is how I have structured my code.以下是我构建代码的方式。 I have included the recv and write section which I think is causing my errors in full.我已经包含了我认为完全导致我的错误的 recv 和 write 部分。 I am pretty new to socket programming and c In general and looking for some tips on my structure and anything I may have missed.我对套接字编程和 c 非常陌生,总的来说,我正在寻找一些关于我的结构的技巧以及我可能错过的任何东西。

int main(int argc, char *argv[])
{
    char ip[40]
    char *host = argv[1];
    char *port_s = argv[2];
    int err;                             
    int socket_browser, socket_newBrowser, c;
    struct sockaddr_in server, client;
    int n;

    socket_browser= socket(AF_INET, SOCK_STREAM, 0);
    if (socket_browser < 0)
    {
        printf("Could not create socket");
    }

    if (err = bind(socket_browser , (struct sockaddr *)&server, sizeof(server)) != 0)
    {
        resourceError(err, "bind");
        return 1;
    }

    if (err = listen(socket_browser , 3) != 0)
    {
        resourceError(err, "listen");
    }

while (1){

        c = sizeof(struct sockaddr_in);
        server_socket= accept(socket_browser, (struct sockaddr *)&client, (socklen_t *)&c);

       char buf[256];
       int n;

       n = recv(socket_newBrowser, buf, 256, 0);
       if (n < 0){
          resourceError(n,"recv");
       }

      int server_socket;
      struct sockaddr_in server2;

       server_socket= socket(AF_INET, SOCK_STREAM, 0);
        if (server_socket < 0)
        {
            resourceError(server_socket, "serverSocket");
        }
        server2.sin_addr.s_addr = inet_addr(ip);
        server2.sin_family = AF_INET;
        server2.sin_port = htons(80);

       connect(server_socket, (struct sockaddr *)&server2, sizeof(server2))
       send(server_socket, buf, strlen(buf), 0);

      char reply[256];
      int bytes_reply = 0;

    do
            {
                bytes_reply = recv(server_socket, reply, sizeof(reply), 0);
                // Need to check for double enter as this currently does not work in telnet
                if (bytes_reply == -1)
                {
                    perror("Recv error");
                }
                else
                {
                    write(server_socket, reply, bytes_reply);
                }
            } while (bytes_reply > 0);


        printf("connections closed");
    }
    return 0;
}

I think your problem (or at least a problem) is:我认为您的问题(或至少是一个问题)是:

n = recv(socket_newBrowser, buf, 256, 0);
/*versus*/
send(server_socket, buf, strlen(buf), 0);

buf is not null-terminated, you should have used the n value returned from recv instead of strlen . buf不是以空值结尾的,您应该使用从 recv 返回的n值而不是strlen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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