在C语言中轮询TCP连接

4
我正在做一项学校任务,需要制作一个能够接收TCP连接并传递数据的服务器。我使用poll()方法来检查是否有新的连接请求或现有连接中是否有传入数据。它与回环地址(127.0.0.1)一起使用。
当只有1个连接时,它可以正常工作,但是当我打开第二个连接时,它停止轮询,并等待来自新连接的数据。 奇怪的是,如果我打开第三个连接,它也会提供那个第三个连接的数据,但仍然没有轮询,只是等待。
我已经阅读了很多相关资料,但我还是卡在这里了。
我在connmgr.c中编写了服务器代码(使用了很多printf语句以找出我的错误)。我将在下面进行解释:
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>

#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <syslog.h>
#include <poll.h>

#include "connmgr.h"

#define _GNU_SOURCE

static FILE * write_file;
struct pollfd *poll_list;
int serversd;
int conn_counter = 0;

int dplist_errno;

dplist_t * list = NULL;
list_node_t * dummy = NULL;


void * element_copy(void *element);
void element_free(void **element);
int element_compare(void *x, void *y);


void add_poll(struct pollfd * polllist, tcpsock_t *client, tcpsock_t *server){
conn_counter++;
polllist = realloc(polllist,sizeof(struct pollfd)*conn_counter+1);
int clientsd;
tcp_get_sd(client, &clientsd);
polllist[conn_counter].fd= clientsd;
//printf("fd in add_poll = %d \n",clientsd);
polllist[conn_counter].events = POLLIN;
}
    int stop = 0;
    tcpsock_t *server, *client;

void connmgr_listen(int port_number){

    sensor_data_t data;
dummy = malloc(sizeof(list_node_t));

write_file = fopen("sensor_data_recv", "w");

poll_list = malloc(sizeof(struct pollfd));
list = dpl_create(&element_copy, &element_free, &element_compare);

if(tcp_passive_open(&server,port_number)!=TCP_NO_ERROR)exit(EXIT_FAILURE);
tcp_get_sd(server, &serversd);

poll_list[0].fd = serversd;
poll_list[0].events = POLLIN;

printf("server fd = %d \n",serversd);

printf("start with fd %d = %d \n", 0,poll_list[0].fd);

int bytes,result;
int test = 0;

while(1){

    test++;
    int sd;
    int return_value,i;

    return_value = poll(poll_list,conn_counter+1,TIMEOUT);

    if (return_value>0){
        if(poll_list[0].revents & POLLIN>0){
            printf("add client\n");

            tcpsock_t * requestclient;
            if (tcp_wait_for_connection(server,&requestclient)!=TCP_NO_ERROR) exit(EXIT_FAILURE);
            tcp_get_sd(requestclient, &sd);
            dummy->sd = sd;
            printf("inserted sd = %d \n",sd);
            dummy->client = requestclient;
            time(&(dummy->ts));
            list = dpl_insert_at_index(list, dummy, conn_counter,true);



            //printf("sd from client = %d \n", tcp_get_sd(client, &sd));

            add_poll(poll_list, dummy->client, server);
            printf("conn_counter = %d \n",conn_counter);

        }

        //for (i=0; i<conn_counter;i++){
        i=0;
        while(i<conn_counter){
            if(poll_list[i+1].revents & POLLIN>0){

                printf("poll in %d \n",i+1);

                dummy = (list_node_t *) dpl_get_element_at_index(list,i);
                time(&(dummy->ts));
                client = dummy->client;
                sd = dummy->sd;

                /*for(int l = 0; l<conn_counter;l++){
                        printf("sensor %d \n",l);

                }*/

                bytes = sizeof(data.id);
                printf("@ line %d en i = %d \n", __LINE__,i);result = tcp_receive(client,(void *)&data.id,&bytes); 

                bytes = sizeof(data.value);
                printf("@ line %d \n", __LINE__);result = tcp_receive(client,(void *)&data.value,&bytes);

                bytes = sizeof(data.ts);
                printf("@ line %d \n", __LINE__);result = tcp_receive(client,(void *)&data.ts,&bytes);


                if ((result==TCP_NO_ERROR) && bytes){
                printf("sensor id = %" PRIu16 " - temperature = %g - timestamp = %ld\n", data.id, data.value, (long int)data.ts);
                        }

                else{
                    if(result == TCP_CONNECTION_CLOSED){printf("peer left \n");}
                    else{"error in peerconnection \n";}
                    tcp_close(&client);
                    list = dpl_remove_at_index(list, i, true);
                    }
                fflush(stdout);
            }
            if (poll_list[i+1].revents & POLLHUP){
                printf("client disconnected \n");
                poll_list[conn_counter+1].fd=-1;
                poll_list[conn_counter+1].events=0;
                fflush(stdout);
            }i++;
        }       
    }
    //if(return_value<0){exit(EXIT_FAILURE);}
    //if(stop == 1){break;}
    printf("end of while %d \n",test);



}

一旦服务器在tcp_passive_open中启动,它将开始轮询,直到return_value为高。然后检查POLLIN是否在[0]处,表示有新的连接请求。如果没有,我会检查poll_list中的所有连接,从1开始直到conn_counter。如果还是没有,我会检查POLLHUP并将该客户端的fd设置为-1,以便poll()忽略它。

我认为应该在使用while循环检查POLLIN是否来自其中一个连接的某个位置,因为我可以轻松添加连接。

dpl_...函数来自我自己编写的双向链表库,应该工作正常。它们创建一个列表,在索引处获取元素......该列表使用3个回调函数。

tcp_...函数是我从教授那里得到的函数。tcpsock.h文件应该提供足够的信息:

typedef struct tcpsock tcpsock_t;

int get_size_tcpsock();

// All functions below return TCP_NO_ERROR if no error occurs during execution

int tcp_passive_open(tcpsock_t ** socket, int port);
/* Creates a new socket and opens this socket in 'passive listening mode' (waiting for an active connection setup request) 
 * The socket is bound to port number 'port' and to any active IP interface of the system
 * The number of pending connection setup requests is set to MAX_PENDING
 * The newly created socket is returned as '*socket'
 * This function is typically called by a server
*/

int tcp_active_open(tcpsock_t ** socket, int remote_port, char * remote_ip);
/* Creates a new TCP socket and opens a TCP connection to the system with IP address 'remote_ip' on port 'remote_port'
 * The newly created socket is return as '*socket'
 * This function is typically called by a client 


int tcp_close(tcpsock_t ** socket); 
/* The socket '*socket' is closed , allocated resources are freed and '*socket' is set to NULL
 * If '*socket' is connected, a TCP shutdown on the connection is executed
 */

int tcp_wait_for_connection(tcpsock_t * socket, tcpsock_t ** new_socket); 
/* Puts the socket 'socket' in a blocking wait mode
 * Returns when an incoming TCP connection setup request is received
 * A newly created socket identifying the remote system that initiated the connection request is returned as '*new_socket'
 */

int tcp_send(tcpsock_t * socket, void * buffer, int * buf_size );
/* Initiates a send command on the socket 'socket' and tries to send the total '*buf_size' bytes of data in 'buffer' (recall that the function might block for a while)
 * The function sets '*buf_size' to the number of bytes that were really sent, which might be less than the initial '*buf_size'
 */

int tcp_receive (tcpsock_t * socket, void * buffer, int * buf_size);
/* Initiates a receive command on the socket 'socket' and tries to receive the total '*buf_size' bytes of data in 'buffer' (recall that the function might block for a while)
 * The function sets '*buf_size' to the number of bytes that were really received, which might be less than the inital '*buf_size'
 */

int tcp_get_ip_addr( tcpsock_t * socket, char ** ip_addr);
/* Set '*ip_addr' to the IP address of 'socket' (could be NULL if the IP address is not set)
 * No memory allocation is done (pointer reference assignment!), hence, no free must be called to avoid a memory leak
 */

int tcp_get_port(tcpsock_t * socket, int * port); 
/* Return the port number of the 'socket'
 */

int tcp_get_sd(tcpsock_t * socket, int * sd); 
/* Return the socket descriptor of the 'socket'
 */

感谢您阅读此内容,希望您能帮助我!顺便说一句,当我断开一个TCP连接并连接另一个时,仍然存在一些问题,但这是以后需要解决的问题 :)
1个回答

2
在你的函数中
void add_poll(struct pollfd * polllist, tcpsock_t *client, tcpsock_t *server)

您正在重新分配包含套接字fd的pollfd缓冲区,但您在一个临时指针变量(polllist)中获取结果,而不是全局变量“poll_list”。如果可以仅扩展它,则realloc可能会将内存移动到另一个位置。
当您重新进入循环时,没有任何变化,您仍在使用相同的缓冲区 - 可能已经被释放!- 包含服务器套接字和第一个连接,并且在内存中的其他位置有一个具有3个套接字的缓冲区。
因此,请传递指向指针的指针(struct pollfd ** polllist),或者设置“poll_list”变量:)。
此外,如果我没记错,在您的循环中,当您执行以下操作时:
if(poll_list[i+1].revents & POLLIN>0)

"

大于号(>)的优先级高于与号(&),所以它相当于:

"
if(poll_list[i+1].revents & (POLLIN>0))

只需删除"> 0",或在表达式周围加上括号


嗨,谢谢你的回答,但我更改了你所说的内容,结果仍然一样... 我现在执行 'poll_list = realloc(poll_list,sizeof(struct pollfd)*conn_counter+1);',但没有效果。你有其他可能的解决方案吗? :) - Gus Vanherf
请注意你的 sizeof(struct pollfd)*conn_counter+1,它等同于 (sizeof(struct pollfd)*conn_counter)+1,而不是 sizeof(struct pollfd)*(conn_counter+1)。除此之外,你是否也更改了 polllist[conn_counter].fd= clientsd;(以及下面类似的一行代码)?还有 POLLIN>0 的部分?你能解释一下你所说的“如果我打开第三个连接,它确实会给出第三个连接的数据,但仍然没有轮询,只是等待”的意思吗?(另外,在创建新的轮询集时,请确保清除“revent”字段) - KailoKyra
是的,我做了更改,但没有帮助。我的意思是,如果我打开第二个连接,它就会停止轮询第一个连接,并且只从第二个连接提供数据。(这是因为它不再通过while循环循环,而只是等待数据。我可以看到这一点,因为有虚拟的“while测试结束”)。如果我打开第三个连接,它将给出来自第二个和第三个连接的数据,但同样是没有轮询,只是等待数据,而不是通过while循环。所以像你说的那样,也许仍然存在POLLIN标志高? - Gus Vanherf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接