使用select()函数发送和重新传输数据

时间:2016-05-15 06:30:40

标签: c linux sockets select

我正在编写与服务器通信的客户端代码,发送和接收数据。 我正在使用select函数来监视两个文件描述符 - 0作为stdin fd,4(socket_dect)作为socket fd。 主要的想法是找到当前fd的活动,如果STDING处于活动状态,那么读取它并将数据发送到服务器,如果套接字fd处于活动状态,则接收数据并打印出来。 我不知道为什么,但是当我经营这个客户时,我很高兴。 这是我的代码:

//聊天客户端 //由omri撰写

#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

1 个答案:

答案 0 :(得分: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()计数。

相关问题