使用 select() 函数发送和接收数据

问题描述 投票:0回答:1

我正在编写与服务器通信、发送和接收数据的客户端代码。 我使用 select 函数来监视两个文件描述符 - 0 作为标准输入 fd,4(socket_dect) 作为套接字 fd。 主要思想是找到当前哪个 fd 处于活动状态,如果 STDING 处于活动状态,则将读取它并将数据发送到服务器,如果套接字 fd 处于活动状态,则将接收数据并打印它。 我不知道为什么,但当我运行这个客户端时没有任何反应。 这是我的代码:

//聊天客户端 // 奥姆里写的

#include <stdio.h>
#include <sys/socket.h> 
#include <sys/select.h>
#include <arpa/inet.h> //inet_addr
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>


#define PORT 9034 // defined port like the server
#define STDIN 0

void main()
{
    struct timeval tv; // time values for select function
    int activity = 0 ;
    int ready; // contains the result of the select function
    char buf[256];
    int len = strlen(buf);
    int byte_sent,i;
    int socket_dect; // creating socket descriptor
    struct sockaddr_in ServerInfo;
    fd_set readfds;
    
    // creating a new socket, its a number represent a file descriptor
    // socket args : 1)ip protocol ipv4,second tcp/udp, third is number of protocol used
    socket_dect = socket(AF_INET,SOCK_STREAM,0);

    if(socket_dect == -1){
        perror("error creating socket");
    }
    // fill the values of the server
    ServerInfo.sin_family = AF_INET; // ipv4
    ServerInfo.sin_port = htons(PORT); // port number
    //ServerInfo.sin_addr = 127.0.0.1; 
    inet_pton(AF_INET, "127.0.0.1", &ServerInfo.sin_addr);//insert the ip to the sin addr
   
    //fill the values of the time for the select function
    tv.tv_sec = 5; // 5 sec
    tv.tv_usec = 0; // 0 mili sec
    FD_ZERO(&readfds); // clear the socket set
    for(i = 0; i<= socket_dect; i++){
        FD_SET(i,&readfds);// set the sockets
    }
    
    // making the connection to the server
    //ServerInfo.sin_addr.s_addr = inet_addr("127.0.0.1"); // another way to put ip addr
    connect(socket_dect,(struct sockaddr *)&ServerInfo,sizeof(ServerInfo)); // connected to the server
    printf("after connect");
    while(1){
        ready = select(socket_dect,&readfds,NULL,NULL,&tv); 
        printf("%d is ready",ready);
        if(ready < 0){
            perror("select error");
            printf("select error");
        }
        // reading from STDIN to buff
        else if(ready == STDIN_FILENO){
            if(fread(buf,256,1,&readfds) < 0){
                printf("read error");
                perror("read error");
            }   
            if(send(socket_dect,buf,len,NULL) < 0){
                perror("send connection");
                printf("send error");
            }//end if send
        }//end elseif STDIN
        else if(ready == socket_dect){
            if(recv(socket_dect,buf,len,0) < 0 ){
                printf("recv error");
            }
            printf("data recived is :%s",buf);   
        }//end elseif
        else{
            printf("none is ready");
        }
    }//end while
}//end main
c linux sockets posix-select
1个回答
3
投票

对于初学者来说,有两个以上的问题。

这个

ready = select(socket_dect,&readfds,NULL,NULL,&tv); 

应该是

ready = select(socket_dect + 1, &readfds, NULL, NULL, &tv); 

来自

man select

nfds 是三个集合中编号最高的文件描述符,再加 1。


而不是

if(ready == STDIN_FILENO)

使用

if (FD_ISET(STDIN_FILENO, &readfds)

select()
修改了传递的参数,因此每次调用
select()
之前都需要重新初始化它们,即在循环内部。


此外,您必须测试所有系统调用的结果。这包括

connect()


此外^2您可能不会假设

recv()
返回与所告知的一样多的字节(此处为
len
)。它可能会收到很少的回报。为了确保你得到了你想要的数量,请循环
recv()
数数。

© www.soinside.com 2019 - 2024. All rights reserved.