C 套接字编程:尽管从服务器发送了消息,select() 仍返回 0

发布于 2024-09-02 20:33:35 字数 5645 浏览 2 评论 0原文

我使用 TCP/IP,使用 select() 从服务器接收 recv() 消息。当我从服务器发送消息时,它会返回合理的字节数,表示发送成功。当我使用 while 循环进行 recv() 时,它确实成功到达客户端。一切都很好而且花花公子。

while(1)
   recv() // obviously pseudocode

但是,当我尝试使用 select() 时,select() 从超时返回 0(设置为 1 秒),并且我一生都无法弄清楚为什么它看不到从服务器发送的消息。我还应该提到,当服务器断开连接时, select() 也看不到这一点,就好像我要使用 recv() 一样,它会返回 0表明使用套接字的连接已关闭。任何意见或想法都深表感谢。

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>


#define SERVER_PORT 10000
#define MAX_CONNECTION  20
#define MAX_MSG     50


struct client
{
    char c_name[MAX_MSG];
    char g_name[MAX_MSG];

int csock;
int host;   // 0 = not host of a multicast group    

struct sockaddr_in client_address;

struct client * next_host;
struct client * next_client;    
};

struct fd_info
{
char c_name[MAX_MSG];

int socks_inuse[MAX_CONNECTION];
int sock_fd, max_fd;
int exit;

struct client * c_sys;
struct sockaddr_in c_address[MAX_CONNECTION];

struct sockaddr_in server_address;
struct sockaddr_in client_address;

fd_set read_set;
};

struct message
{
char c_name[MAX_MSG];
char g_name[MAX_MSG];
char _command[3][MAX_MSG];
char _payload[MAX_MSG];

struct sockaddr_in client_address;
struct client peer;
};

int main(int argc, char * argv[])
{
char * host;
char * temp;

int i, sockfd;
int msg_len, rv, ready;
int connection, management, socketread;
int sockfds[MAX_CONNECTION];


// for three threads that handle new connections, user inputs and select() for sockets
pthread_t connection_handler, manager, socket_reader;

struct sockaddr_in server_address, client_address;
struct hostent * hserver, cserver;
struct timeval timeout;
struct message msg;
struct fd_info info;
info.exit = 0;  // exit information: if exit = 1, threads quit
info.c_sys = NULL;

// looking up from the host database
if (argc == 3)
{
    host = argv[1];         // server address
    strncpy(info.c_name, argv[2], strlen(argv[2]));     // client name
}
else
{
    printf("plz read the manual, kthxbai\n");
    exit(1);
}

printf("host is %s and hp is %p\n", host, hserver);
hserver = gethostbyname(host);
if (hserver)
{
    printf("host found: %s\n", hserver->h_name );
}
else
{
    printf("host not found\n");
    exit(1);
}
// setting up address and port structure information on serverside
bzero((char * ) &server_address, sizeof(server_address)); // copy zeroes into string
server_address.sin_family = AF_INET;
memcpy(&server_address.sin_addr, hserver->h_addr, hserver->h_length);
server_address.sin_port = htons(SERVER_PORT);

bzero((char * ) &client_address, sizeof(client_address)); // copy zeroes into string
client_address.sin_family = AF_INET;
client_address.sin_addr.s_addr = htonl(INADDR_ANY);
client_address.sin_port = htons(SERVER_PORT);

// opening up socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
    exit(1);
else
{
    printf("socket is opened: %i \n", sockfd);
    info.sock_fd = sockfd;
}

// sets up time out option for the bound socket 
timeout.tv_sec = 1;     // seconds
timeout.tv_usec = 0; // micro seconds ( 0.5 seconds)
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval));

// binding socket to a port
rv = bind(sockfd, (struct sockaddr *) &client_address, sizeof(client_address));
if (rv < 0)
{
    printf("MAIN: ERROR bind() %i: %s\n", errno, strerror(errno));
    exit(1);
}
else
    printf("socket is bound\n");

printf("MAIN: %li \n", client_address.sin_addr.s_addr);


// connecting
rv = connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address));
info.server_address = server_address;
info.client_address = client_address;
info.sock_fd = sockfd;
info.max_fd = sockfd;
printf("rv = %i\n", rv);
if (rv < 0)
{
    printf("MAIN: ERROR connect() %i:  %s\n", errno, strerror(errno));
    exit(1);
}
else
    printf("connected\n");

fd_set readset;
FD_ZERO(&readset);
FD_ZERO(&info.read_set);
FD_SET(info.sock_fd, &info.read_set);

while(1)
{
    readset = info.read_set;
    printf("MAIN: %i \n", readset);
    ready = select((info.max_fd)+1, &readset, NULL, NULL, &timeout);

    if(ready == -1)
    {
        sleep(2);
        printf("TEST: MAIN: ready = -1. %s \n", strerror(errno));
    }
    else if (ready == 0)
    {
        sleep(2);
        printf("TEST: MAIN: ready = 0. %s \n", strerror(errno));
    }
    else if (ready > 0)
    {
        printf("TEST: MAIN: ready = %i. %s at socket %i \n", ready, strerror(errno), i);
        for(i = 0; i < ((info.max_fd)+1); i++)
        {
            if(FD_ISSET(i, &readset))
            {
                rv = recv(sockfd, &msg, 500, 0);
                if(rv < 0)
                    continue;
                else if(rv > 0)
                    printf("MAIN: TEST: %s %s \n", msg._command[0], msg._payload);
                else if (rv == 0)
                {
                    sleep(3);
                    printf("MAIN: TEST: SOCKET CLOSEDDDDDD \n");
                }

                FD_CLR(i, &readset);
            }
        }

    }
    info.read_set = readset;        

}

// close connection
close(sockfd);
printf("socket closed. BYE! \n");
return(0);

}

I'm using select() to recv() messages from server, using TCP/IP. When I send() messages from the server, it returns a reasonable number of bytes, saying it's sent successful. And it does get to the client successfully when I use while loop to just recv(). Everything is fine and dandy.

while(1)
   recv() // obviously pseudocode

However, when I try to use select(), select() returns 0 from timeout (which is set to 1 second) and for the life of me I cannot figure out why it doesn't see the messages sent from the server. I should also mention that when the server disconnects, select() doesn't see that either, where as if I were to use recv(), it would return 0 to indicate that the connection using the socket has been closed. Any inputs or thoughts are deeply appreciated.

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>


#define SERVER_PORT 10000
#define MAX_CONNECTION  20
#define MAX_MSG     50


struct client
{
    char c_name[MAX_MSG];
    char g_name[MAX_MSG];

int csock;
int host;   // 0 = not host of a multicast group    

struct sockaddr_in client_address;

struct client * next_host;
struct client * next_client;    
};

struct fd_info
{
char c_name[MAX_MSG];

int socks_inuse[MAX_CONNECTION];
int sock_fd, max_fd;
int exit;

struct client * c_sys;
struct sockaddr_in c_address[MAX_CONNECTION];

struct sockaddr_in server_address;
struct sockaddr_in client_address;

fd_set read_set;
};

struct message
{
char c_name[MAX_MSG];
char g_name[MAX_MSG];
char _command[3][MAX_MSG];
char _payload[MAX_MSG];

struct sockaddr_in client_address;
struct client peer;
};

int main(int argc, char * argv[])
{
char * host;
char * temp;

int i, sockfd;
int msg_len, rv, ready;
int connection, management, socketread;
int sockfds[MAX_CONNECTION];


// for three threads that handle new connections, user inputs and select() for sockets
pthread_t connection_handler, manager, socket_reader;

struct sockaddr_in server_address, client_address;
struct hostent * hserver, cserver;
struct timeval timeout;
struct message msg;
struct fd_info info;
info.exit = 0;  // exit information: if exit = 1, threads quit
info.c_sys = NULL;

// looking up from the host database
if (argc == 3)
{
    host = argv[1];         // server address
    strncpy(info.c_name, argv[2], strlen(argv[2]));     // client name
}
else
{
    printf("plz read the manual, kthxbai\n");
    exit(1);
}

printf("host is %s and hp is %p\n", host, hserver);
hserver = gethostbyname(host);
if (hserver)
{
    printf("host found: %s\n", hserver->h_name );
}
else
{
    printf("host not found\n");
    exit(1);
}
// setting up address and port structure information on serverside
bzero((char * ) &server_address, sizeof(server_address)); // copy zeroes into string
server_address.sin_family = AF_INET;
memcpy(&server_address.sin_addr, hserver->h_addr, hserver->h_length);
server_address.sin_port = htons(SERVER_PORT);

bzero((char * ) &client_address, sizeof(client_address)); // copy zeroes into string
client_address.sin_family = AF_INET;
client_address.sin_addr.s_addr = htonl(INADDR_ANY);
client_address.sin_port = htons(SERVER_PORT);

// opening up socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
    exit(1);
else
{
    printf("socket is opened: %i \n", sockfd);
    info.sock_fd = sockfd;
}

// sets up time out option for the bound socket 
timeout.tv_sec = 1;     // seconds
timeout.tv_usec = 0; // micro seconds ( 0.5 seconds)
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval));

// binding socket to a port
rv = bind(sockfd, (struct sockaddr *) &client_address, sizeof(client_address));
if (rv < 0)
{
    printf("MAIN: ERROR bind() %i: %s\n", errno, strerror(errno));
    exit(1);
}
else
    printf("socket is bound\n");

printf("MAIN: %li \n", client_address.sin_addr.s_addr);


// connecting
rv = connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address));
info.server_address = server_address;
info.client_address = client_address;
info.sock_fd = sockfd;
info.max_fd = sockfd;
printf("rv = %i\n", rv);
if (rv < 0)
{
    printf("MAIN: ERROR connect() %i:  %s\n", errno, strerror(errno));
    exit(1);
}
else
    printf("connected\n");

fd_set readset;
FD_ZERO(&readset);
FD_ZERO(&info.read_set);
FD_SET(info.sock_fd, &info.read_set);

while(1)
{
    readset = info.read_set;
    printf("MAIN: %i \n", readset);
    ready = select((info.max_fd)+1, &readset, NULL, NULL, &timeout);

    if(ready == -1)
    {
        sleep(2);
        printf("TEST: MAIN: ready = -1. %s \n", strerror(errno));
    }
    else if (ready == 0)
    {
        sleep(2);
        printf("TEST: MAIN: ready = 0. %s \n", strerror(errno));
    }
    else if (ready > 0)
    {
        printf("TEST: MAIN: ready = %i. %s at socket %i \n", ready, strerror(errno), i);
        for(i = 0; i < ((info.max_fd)+1); i++)
        {
            if(FD_ISSET(i, &readset))
            {
                rv = recv(sockfd, &msg, 500, 0);
                if(rv < 0)
                    continue;
                else if(rv > 0)
                    printf("MAIN: TEST: %s %s \n", msg._command[0], msg._payload);
                else if (rv == 0)
                {
                    sleep(3);
                    printf("MAIN: TEST: SOCKET CLOSEDDDDDD \n");
                }

                FD_CLR(i, &readset);
            }
        }

    }
    info.read_set = readset;        

}

// close connection
close(sockfd);
printf("socket closed. BYE! \n");
return(0);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-09-09 20:33:35

如果发生超时,那么您的readset最终将不会设置任何文件描述符。然后,您将 info.read_set 替换为这个空集:

info.read_set = readset;

..因此您之后总是检查空文件描述符集,这意味着每次都会超时。

(顺便说一句,您实际上并不应该像这样分配文件描述符集 - 不能保证它们不包含需要深度复制的结构。可移植的方法是构建文件描述符集每次都刮擦。)

If a timeout ever occurs, then your readset will end up with no file descriptors set. You then replace info.read_set with this empty set:

info.read_set = readset;

..and so you're always checking an empty file descriptor set after that, which means you'll timeout every time.

(By the way, you're not really supposed to assign file descriptor sets around like that - there's no guarantee that they don't contain structures that need to be deep-copied. The portable approach is to build the file descriptor set up from scratch each time.)

孤独患者 2024-09-09 20:33:35

只需做一件事:

要么增加超时值,要么将其保留为 NULL。

我想它会成为阻塞套接字,因为 select 在该调用中等待,直到存在可以从对等方读取的内容。尝试一下,这是选项之一。我认为增加超时值是可行的解决方案。

Just do one thing:

Either increase the time-out value or keep that as NULL.

I guess it will become blocking socket as select waits in that call until some thing is existing to read from peer. Just try this is one of the options. I think increasing the time-out value is the feasible solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文