-1

我想用一个 C 程序映射两个端口(任何到端口 x 到端口 y,反之亦然),我写了这个程序,但它不起作用。这是我的代码:

int recv_all_nonblock(int sockfd,char* buff,int buffersize)
{
    int numbytes;
    if ((numbytes = recv(sockfd, buff, buffersize-1, MSG_NOSIGNAL|MSG_DONTWAIT)) <= 0)
    {
    perror("recv");
    }
    else if( numbytes>0)
    {
    buff[numbytes] = '\0';
    }

    return numbytes;
}


int sendall(int sockfd, char *buf, int *len)
{
    int total = 0;        // how many bytes we've sent
    int bytesleft = *len; // how many we have left to send
    int n=0;
    while(total < *len)
    {
    n = send(sockfd, buf+total, bytesleft, MSG_NOSIGNAL|MSG_DONTWAIT);
    if (n == -1) { break; }
    total += n;
    bytesleft -= n;
    }

    *len = total; // return number actually sent here

    return n==-1?-1:0; // return -1 on failure, 0 on success
}


int Connect_To_Remote(char *Addr,char* PORT)
{
    int sockfd;
    struct addrinfo hints, *servinfo=NULL, *p=NULL;
    int rv;
    char s[INET6_ADDRSTRLEN];



    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    while (p==NULL)
    {
    sleep(5);
    while((rv = getaddrinfo(Addr, PORT, &hints, &servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        sleep(5);
    }

    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
        {
        perror("client: socket");
        continue;
        }

        if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
        {
        close(sockfd);
        perror("client: connect");
        continue;
        }

        break;
    }

    if (p == NULL)
    {
        fprintf(stderr, "client: failed to connect\n");
    }
    }

    inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
    printf("client: connecting to %s\n", s);

    freeaddrinfo(servinfo); // all done with this structure
    return sockfd;

}
int recv_all(int sockfd,char* buff,int buffersize)
{
    int numbytes;
    fd_set readfd_set;
    struct timeval recvwait;

    recvwait.tv_sec=10;
    recvwait.tv_usec=0;
    FD_ZERO(&readfd_set);
    FD_SET(sockfd, &readfd_set);
    if(select(sockfd+1, &readfd_set, NULL, NULL, &recvwait) <= 0)
    {
    perror("wait for recieve error:");
    }
    else
    {
    if ((numbytes = recv(sockfd, buff, buffersize-1, MSG_NOSIGNAL|MSG_DONTWAIT)) == -1)
    {
        perror("recv");
    }
    else if( numbytes>0)
    {
        buff[numbytes] = '\0';
    }

    return numbytes;
    }

    return 0;
}

void *Port_Mapper()
{
    int sockfd,newfd;  // listen on sock_fd, new connection on new_fd
    struct addrinfo hints, *servinfo, *tmpaddrinfo;
    struct sockaddr_storage their_addr; // connector's address information
    socklen_t sin_size;
    int rv;
    int yes=1;
    int ret=0;

    memset(&hints, 0, sizeof (hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    while (1)
    {
    if ((rv = getaddrinfo(NULL, MapPort, &hints, &servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        sleep(2);
    }
    else
    {
        break;
    }
    }

    while (1)
    {
    for(tmpaddrinfo = servinfo; tmpaddrinfo != NULL; tmpaddrinfo = tmpaddrinfo->ai_next)
    {
        if ((sockfd = socket(tmpaddrinfo->ai_family,tmpaddrinfo->ai_socktype,tmpaddrinfo->ai_protocol)) == -1)
        {
        perror("server: socket");
        continue;
        }
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
        {
            perror("setsockopt");
            exit(1);
        }
        if (bind(sockfd, tmpaddrinfo->ai_addr, tmpaddrinfo->ai_addrlen) == -1) {
        close(sockfd);
        perror("server: bind");
        continue;
        }

        break;
    }
    if (tmpaddrinfo == NULL)
    {
        fprintf(stderr, "server: failed to bind\n");
        sleep(1);
    }
    else
    {
        break;
    }
    }
    freeaddrinfo(servinfo); // all done with this structure
    if (listen(sockfd, MAXLISTENQ) == -1)
    {
    perror("listen");
    exit(1);
    }
    int bufpoint=0;
    printf("server: waiting for connections...\n");
    while(1)
    {  // main accept() loop
    char buff[MAX_SOCK_BUFFER];
    char buff2[MAX_SOCK_BUFFER];
    ret=0;
    int sockfdweb=0;
    sin_size = sizeof (their_addr);
    printf("mapping server: going to  accept connections...\n");
    newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    printf("\n\nmapping server: connections accepted:%d\n",newfd);

    sockfdweb=Connect_To_Remote("192.168.1.10","80");
    if (sockfdweb<0)
    {
        printf("can not connect to %s\n","192.168.1.10");
        break;
    }
      while(1)
    {



        memset(buff,0,sizeof(buff));
        ret=recv_all_nonblock(newfd,buff,MAX_SOCK_BUFFER);
printf("recved from client1,%d\n",ret);
        if (ret<=0)
        {
        perror("recieve error from browser:");
        close(newfd);
        sin_size = sizeof (their_addr);
        printf("mapping server: going to  accept connections...\n");
        newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
          ret=recv_all_nonblock(newfd,buff,MAX_SOCK_BUFFER);
    printf("recved from client10,%d\n",ret);
        if (ret<=0)
        {
            perror("recieve error from browser:");
            close(newfd);
            sin_size = sizeof (their_addr);
            printf("mapping server: going to  accept connections...\n");
            newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        }
        else if(ret>0)
        {
    printf("recved from client0\n");
    printf("%s\n",buff);
            if (sendall(sockfdweb,buff,&bufpoint)<0)
            {
            printf("can not send data to %s\n","192.168.1.10");
            }
    printf("send to 80,0\n");

        }

        }
        else if(ret>0)
        {
printf("recved from client\n");
    printf("%s\n",buff);
        if (sendall(sockfdweb,buff,&bufpoint)<0)
        {
            printf("can not send data to %s\n","192.168.1.10");
        }
    printf("send to 80\n");

        }

        memset(buff2,0,sizeof(buff2));
        ret=recv_all_nonblock(sockfdweb,buff2,MAX_SOCK_BUFFER);
printf("recv from 80...%d\n",ret);
        if (ret<=0)
        {
        close(sockfdweb);
        sockfdweb=Connect_To_Remote("192.168.1.10","80");
        if (sockfdweb<0)
        {
            printf("can not connect to %s\n","192.168.1.10");
            break;
        }
        ret=recv_all_nonblock(sockfdweb,buff2,MAX_SOCK_BUFFER);
    printf("recv from 80.9..%d\n",ret);
        if (ret<=0)
        {
            close(sockfdweb);
            sockfdweb=Connect_To_Remote("192.168.1.10","80");
            if (sockfdweb<0)
            {
            printf("can not connect to %s\n","192.168.1.10");
            break;
            }

        }
        else if (ret>0)
        {
    printf("recved from 809\n");
            if (sendall(newfd,buff2,&ret)<0)
            {
            printf("can not send data to %s\n","192.168.1.10");
            }
    printf("send to client9\n");

        }

        }
        else if (ret>0)
        {
printf("recved from 80\n");
        if (sendall(newfd,buff2,&ret)<0)
        {
            printf("can not send data to %s\n","192.168.1.10");
        }
printf("send to client\n");

        }


    }

    }
    return 0;
}

int main()
{
.
.
.
.
    pthread_attr_init (&attr);
    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
    pthread_create(&portmapper_threadid,&attr,Port_Mapper,NULL);
    pthread_attr_destroy (&attr);
.
.
.
.
}

我在 Port_Mapper 函数中添加了一些“printf”,并进行了一些更改以跟踪当我在浏览器中请求 Web 时发生的情况,如下所示:

http://127.0.0.1:8090/

刷新两三次浏览器输出的程序是这样的:

映射服务器:接受的连接数:5
客户端:连接到 192.168.1.10从客户端
接收的
1,360 从客户端接收的
GET /1/ HTTP/1.1
主机:127.0.0.1: 8090
用户代理:Mozilla/5.0 (X11; Linux i686; rv :6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8
Accept-Language: en-us,en;q= 0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cache-Control: max-age=0

发送到 80
recv:
来自 80...-1 的资源暂时不可用 recv
客户端:连接到 192.168.1.10
recv:
来自 80.9..-1 的资源暂时不可用 recv 客户端:连接到 192.168.1.10 recv: 从
客户端1 接收的
资源暂时不可用
,-1
从浏览器接收错误:: 资源暂时不可用
映射服务器: 将接受连接...从客户端接
收到 10,360 从客户
端接收到
GET /1/ HTTP/1.1
主机: 127.0.0.1:8090
用户代理: Mozilla/ 5.0 (X11; Linux i686; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
接受: text/html,application/xhtml+xml,application/xml;q=0.9, /;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep -alive
缓存控制:max-age=0

发送到 80,0
recv:资源暂时不可用
recv 从 80...0
客户端:连接到 192.168.1.10
recv:资源暂时不可用
recv 从 80.9..-1
客户端:连接到 192.168.1.10
recv:资源暂时不可用
从接收client1,-1
从浏览器收到错误:: 资源暂时不可用
映射服务器: 将接受连接...
recv: 资源暂时不可用
从 client10,-1
收到来自浏览器的错误:: 资源暂时不可用
映射服务器: 将接受连接...
recv:资源暂时不可用
recv 从 80...0
客户端:连接到 192.168.1.10
recv:资源暂时不可用
recv 从 80.9..-1
客户端:连接到 192.168.1.10从客户
端接收到
1,332 从客户端接收到
GET / HTTP/1.1
主机:127.0.0.1: 8090
用户代理: Mozilla/5.0 (X11; Linux i686; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8
Accept-语言:en-us,en;q=0.5
接受编码:gzip, deflate
接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7
连接:keep-alive

发送到 80
recv:
来自 80...-1 的资源暂时不可用 recv
客户端:连接到 192.168.1.10
recv:
来自 80.9..-1 的资源暂时不可用 recv 客户端:连接到 192.168.1.10 recv: 从
客户端1 接收的
资源暂时不可用
,-1
从浏览器收到错误:: 资源暂时不可用
映射服务器: 将接受连接...

如您所见,程序正确地从浏览器接收数据,但是当它将数据发送到网络服务器并希望从网络服务器接收数据时,它总是会出错并且不会从网络服务器接收任何数据:

发送到 80
recv:资源暂时不可用
recv 从 80...-1

我不得不说网络服务器正常工作没有问题。
任何人都可以告诉我我的问题是什么????

4

1 回答 1

0

对于如此简单的任务,您的代码非常复杂。它可以简化很多引用,请参阅此(伪)代码:

void mapper()
{
    int server_socket = create_server_socket();

    for (;;)
    {
        int incomming_socket = accept(server_socket);

        int web_server_socket = connect_to_webserver();

        make_socket_nonblocking(incomming_socket);
        make_socket_nonblocking(web_server_socket);

        for (;;)
        {
            int disconnected = 0;

            FD_ZERO(&read_set);

            FD_SET(incomming_socket, &read_set);
            FD_SET(web_server_socket, &read_set);

            select(max_socket + 1, &read_set, NULL, NULL, NULL);

            if (FD_ISSET(incomming_socket, &read_set))
            {
                disonnected = recv_and_send(incomming_socket, web_server_socket);
            }
            if (!disconnected && FD_ISSET(web_server_socket, &read_set))
            {
                disconnected = recv_and_send(web_server_socket, incomming_socket);
            }

            if (disconnected)
                break;
        }

        close(web_server_socket);
        close(incomming_socket);
    }
}

int recv_and_send(from_socket, to_socket)
{
    char buffer[1024];

    for (;;)
    {
        len = recv_all(from_socket, buffer, sizeof(buffer));
        if (len < 0)
            return 1;  /* Disconnect */
        if (len == 0)
            break;  /* No more to read for now */

        if (send_all(to_socket, buffer, len) < 0)
            return 1;  /* Disconnect */
    }

    return 0;
}

int recv_all(socket_fd, char *buffer, const size_t buflen)
{
    size_t total_recv = 0;
    size_t remaining_len = buflen;

    while (remaining_len > 0)
    {
        ssize_t len = recv(socket_fd, buffer + total_recv, remaining_len);
        if (len < 0)
        {
            if (errno == EWOULDBLOCK)
                break;  /* No more to read at the moment */
            return -1;  /* An error */
        }
        if (len == 0)
            break;  /* Connection closed */

        total_recv += len;
        remaining_len -= len;
    }

    return total_recv;
}

[剩下的我留作练习。]

与您的代码的主要区别在于主循环简单得多。您不需要多次accept调用(为什么需要它?)或几个不同的读写函数。我的部分代码可以进一步简化,比如recv_all函数。

还应该注意的是,我上面的代码一次只处理一个连接。解决这个问题的一种方法是为每个传入连接创建一个新线程,或者使用更高级的方法使用连接和缓冲区列表,并select在它们和侦听套接字之间进行多路复用。

于 2012-04-04T11:50:41.037 回答