UDP通讯实例,关于非阻塞设置

http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73d678b975f298fd81c84642c101a39feeb797f4519c4c4086403ac4359e8ed652371587deb8ccffc0e86ecd7696ddb7663671bf21a548446f49d5122b5209b59e8b81990e0b66dcdba978e9f025c9408593dcae78b2b47428878f06560f4a6e85f655a07cbe827648f4e012c88544aa136f3f7461e&p=8b2a9753ce9833e403bd9b7d0a148b&newp=8e6fc54ad5c443fe4badde2d0214cb231610db2151d6d7172ec7d302&user=baidu&fm=sc&query=linux+udp+C+%B7%C7%D7%E8%C8%FB&qid=&p1=10

2008-04-29 15:30:05 / 个人分类:linux C编程

UDP协议的几点好处:
1. UDP不要求保持一个连接;
2. UDP没有因接收方认可收到数据包(或者当数据包没有正确抵达而自动重传)而带来的开销;
3.设计 UDP的目的是用于短应用和控制信息;
4.在一个数据包接一个数据包的基础上, UDP要求的 网络带宽比TCP更小。
UDP的几个缺点:
1.程序员必须创建代码检测传输错误并进行重传(如果应用程序要求这样做);
2.程序员必须把大数据包分片。

code:
<1>
/*
* sender. c-- UDP protocol example
*/
#include
#include
#include
#include
#include
#include
#include

int port = 6789;

int main()
{
    int socket_descrīptor;
    int iter = 0;
    char buf[80];
    struct sockaddr_in address;

    /* Initialize socket address structure for Interner Protocols */
    bzero(&address, sizeof(address)); // empty data structure
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(port);

    /* Create a UDP socket */
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);

    //Loop 20 times (a nice round number ) sending data
    for(iter = 0; iter <= 20; iter++)
    {
        sprintf(buf, "data packet with ID %d\n", iter);
        sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address));
    }   

    /* send a termination message */
    sprintf(buf, "stop\n");
    sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address)); //address is the target of the message send
   
    close (socket_descrīptor);
    printf("Message Sent, Terminating\n");
    return 0;
}

<2>
/*
* receiver. c--received of the UDP protocol
*/
#include
#include
#include
#include
#include
#include
#include

int port = 6789;

int main()
{
    int sin_len;
    char message[256];
    int socket_descrīptor;
    struct sockaddr_in sin;
    printf("Waiting for data from sender\n");

    // Initialize socket address structure for Internet Protocols
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(port);
    sin_len = sizeof(sin);

    //Create a UDP socket and bind it to the port
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    bind(socket_descrīptor, (struct sockaddr *)&sin, sizeof(sin));
   
    //Loop forever (or until a termination message is received)
    // Received data through the socket and process it.The processing in this program is really simple --printing
    while(1)
    {
        recvfrom(socket_descrīptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
        printf("Response from server : %s\n", message);
        if(strncmp(message, "stop", 4) == 0)
        {
            printf("sender has told me to end the connection\n");
            break;
        }
    }
    close(socket_descrīptor);
    return 0;
}

分别编译产生sender、receiver两个可执行文件。
然后在两个终端中分别一次运行,可以得到结果如下:
[hyj@localhost udp_]$ ./receiver
Waiting for data from sender
Response from server : data packet with ID 0

Response from server : data packet with ID 1

Response from server : data packet with ID 2

Response from server : data packet with ID 3

Response from server : data packet with ID 4

Response from server : data packet with ID 5

Response from server : data packet with ID 6

Response from server : data packet with ID 7

Response from server : data packet with ID 8

Response from server : data packet with ID 9

Response from server : data packet with ID 10

Response from server : data packet with ID 11

Response from server : data packet with ID 12

Response from server : data packet with ID 13

Response from server : data packet with ID 14

Response from server : data packet with ID 15

Response from server : data packet with ID 16

Response from server : data packet with ID 17

Response from server : data packet with ID 18

Response from server : data packet with ID 19

Response from server : data packet with ID 20

Response from server : stop

sender has told me to end the connection
发送端结果:
[hyj@localhost udp_]$ ./sender
Message Sent, Terminating


说明:
    sendto函数同send函数,该函数通过套接口发送一条消息。此时使用了无连接的套接口;消息发送的目的地址由address指定;
    recfrom函数同sendto。注意:对于TCP和 UDP你都可以使用recvfrom函数,但是recv只能用于TCP;
    此外,如果在编译时提示找不到socket函数,那么在编译时是需要指定Internet库,在gcc命令中加入-lxnet选项,如:CC -o sender sender. c -lxnet

    bind函数给套接口分配一个地址;
    函数recvfrom等待来自于无连接套接口的一个数据包。它也在地址数据结构中返回发送方的地址。这个源地址在示例程序中没有用,但是更高级的程序能够用它打开一个返回发送方的套接口。如果你不关心这个地址,可以给参数传递NULL。

    以上代码未加入任何错误处理程序,加入最少错误检测后的代码如下:
/*
* receiver2. c--received of the UDP protocol,add the error check
*/
#include
#include
#include
#include
#include
#include
#include
#include

int port = 6789;

int main()
{
    int sin_len;
    char message[256];
    int socket_descrīptor;
    struct sockaddr_in sin;

    int bind_rc, close_rc;
    ssize_t recv_rc;

    printf("Waiting for data from sender\n");

    // Initialize socket address structure for Internet Protocols
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(port);
    sin_len = sizeof(sin);

    //Create a UDP socket and bind it to the port
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    bind_rc = bind(socket_descrīptor, (struct sockaddr *)&sin, sizeof(sin));
    if(bind_rc == -1)
    {
        perror("bind call failed");
        exit(errno);
    }
   
    //Loop forever (or until a termination message is received)
    // Received data through the socket and process it.The processing in this program is really simple --printing
    while(1)
    {
        recv_rc = recvfrom(socket_descrīptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
        if(recv_rc == -1)
        {
            perror("bind call failed");
            exit(errno);
        }
        printf("Response from server : %s\n", message);
        if(strncmp(message, "stop", 4) == 0)
        {
            printf("sender has told me to end the connection\n");
            break;
        }
    }
    close_rc = close(socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }
    return 0;
}


/*
* sender2. c-- UDP protocol example, add the erro check
*/
#include
#include
#include
#include
#include
#include
#include
#include

int port = 6789;

int main()
{
    int socket_descrīptor;
    int iter = 0;

    ssize_t sendto_rc;
    int close_rc;

    char buf[80];
    struct sockaddr_in address;
    struct hostent *hostbyname;

    //Translate a host name to IP address
    hostbyname = gethostbyname("127.0.0.1");
    if(hostbyname == NULL)
    {
        perror("Gethostbyname failed");
        exit(errno);
    }

    /* Initialize socket address structure for Interner Protocols.The address comes from the datastructure returned by gethostbyname() */
    bzero(&address, sizeof(address)); // empty data structure
    address.sin_family = AF_INET;

    memcpy(&address.sin_addr.s_addr, hostbyname->h_addr, sizeof(address.sin_addr.s_addr));

    //print the host IP
    printf("The host IP is %s\n", inet_ntoa(*(struct in_addr*)hostbyname->h_addr));   

    //address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(port);

    /* Create a UDP socket */
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    //Loop 20 times (a nice round number ) sending data
    for(iter = 0; iter <= 20; iter++)
    {
        sprintf(buf, "data packet with ID %d\n", iter);
        sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address));
        if(sendto_rc == -1)
        {
            perror("sendto call failed");
            exit(errno);
        }
    }   

    /* send a termination message */
    sprintf(buf, "stop\n");
    sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address)); //address is the target of the message send
    if(sendto_rc == -1)
    {
        perror("sendto  STOP call failed");
        exit(errno);
    }
   

    //Most people don''t bother to check the return code returned by the close function
    close_rc = close (socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }

    printf("Message Sent, Terminating\n");
    return 0;
}
   下面使用 非阻塞I/O来对上面的接收程序做一些修改:
/*
* sender3. c-- UDP protocol example, add the erro check,use sleep
*/
#include
#include
#include
#include
#include
#include
#include
#include

int port = 6789;

int main()
{
    int socket_descrīptor;
    int iter = 0;

    ssize_t sendto_rc;
    int close_rc;

    char buf[80];
    struct sockaddr_in address;
    struct hostent *hostbyname;

    //Translate a host name to IP address
    hostbyname = gethostbyname("127.0.0.1");
    if(hostbyname == NULL)
    {
        perror("Gethostbyname failed");
        exit(errno);
    }

    /* Initialize socket address structure for Interner Protocols.The address comes from the datastructure returned by gethostbyname() */
    bzero(&address, sizeof(address)); // empty data structure
    address.sin_family = AF_INET;

    memcpy(&address.sin_addr.s_addr, hostbyname->h_addr, sizeof(address.sin_addr.s_addr));

    //print the host IP
    printf("The host IP is %s\n", inet_ntoa(*(struct in_addr*)hostbyname->h_addr));   

    //address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(port);

    /* Create a UDP socket */
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    //Loop 20 times (a nice round number ) sending data
    for(iter = 0; iter <= 20; iter++)
    {
        sprintf(buf, "data packet with ID %d\n", iter);
        sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address));
        if(sendto_rc == -1)
        {
            perror("sendto call failed");
            exit(errno);
        }
       
        sleep(3); //this is the only difference form sender2. c
       
    }   

    /* send a termination message */
    sprintf(buf, "stop\n");
    sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address)); //address is the target of the message send
    if(sendto_rc == -1)
    {
        perror("sendto  STOP call failed");
        exit(errno);
    }
   

    //Most people don''t bother to check the return code returned by the close function
    close_rc = close (socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }

    printf("Message Sent, Terminating\n");
    return 0;
}

/*
* receiver3. c--received of the UDP protocol,add the error check,this have some I/O no-blocking
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include

int port = 6789;

int main()
{
    int sin_len;
    char message[256];
    int socket_descrīptor;
    struct sockaddr_in sin;

    int bind_rc, close_rc;
    ssize_t recv_rc;

    long save_file_flags;

    printf("Waiting for data from sender\n");

    // Initialize socket address structure for Internet Protocols
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(port);
    sin_len = sizeof(sin);

    //Create a UDP socket and bind it to the port
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    bind_rc = bind(socket_descrīptor, (struct sockaddr *)&sin, sizeof(sin));
    if(bind_rc == -1)
    {
        perror("bind call failed");
        exit(errno);
    }

    //set socket to non-blocking
    save_file_flags = fcntl(socket_descrīptor, F_GETFL);
    printf("file flags are %ld\n", save_file_flags);
    save_file_flags |= O_NONBLOCK;

    if(fcntl(socket_descrīptor, F_SETFL, save_file_flags) == -1)
    {
        perror("trying to set input socket to non-blocking");
        exit(errno);
    }

    printf("file flags are now %ld\n", save_file_flags);
   
    //Loop forever (or until a termination message is received)
    // Received data through the socket and process it.The processing in this program is really simple --printing
    while(1)
    {
        sleep(1);  //wait a moment ...

        recv_rc = recvfrom(socket_descrīptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
        if(recv_rc == -1 && errno != EAGAIN)
        {
            fprintf(stderr, "errno %d ", errno);
            perror("recvform call failed");
            exit(errno);
        }
        else if(recv_rc == 0 | errno == EAGAIN) //no data
        {
            printf("no data yet\n");
            errno = 0; // clear the error
            continue;
        }
   
        errno = 0; // clear the error
        printf("Response from server : %s\n", message);
        if(strncmp(message, "stop", 4) == 0)
        {
            printf("sender has told me to end the connection\n");
            break;
        }
    }
    close_rc = close(socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }
    return 0;
}

    receiver3. c中最重要的新内容是fcntl调用,它允许对打开文件的控制标志做修改。带有参数F_GETEL的第一个调用用来取得当前控制标志的状态。然后O_NONBLOCK标志被添加到当前标志中。带有参数F_SETFL的跌入个调用实际上改变了标志。一旦这一步成功完成,对套接口的读取(recvfrom)操作就不再等候数据了。当然,程序要可是fcntl的第二次调用是成功的。
    最终执行的结果如下:
[hyj@localhost udp_]$ ./sender3
The host IP is 127.0.0.1
Message Sent, Terminating


[hyj@localhost udp_]$ ./receiver3
Waiting for data from sender
file flags are 2
file flags are now 2050
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
Response from server : data packet with ID 0

no data yet
no data yet
Response from server : data packet with ID 1

no data yet
no data yet
Response from server : data packet with ID 2

no data yet
no data yet
Response from server : data packet with ID 3

no data yet
no data yet
Response from server : data packet with ID 4

no data yet
no data yet
Response from server : data packet with ID 5

no data yet
no data yet
Response from server : data packet with ID 6

no data yet
no data yet
Response from server : data packet with ID 7

no data yet
no data yet
Response from server : data packet with ID 8

no data yet
no data yet
Response from server : data packet with ID 9

no data yet
no data yet
Response from server : data packet with ID 10

no data yet
no data yet
Response from server : data packet with ID 11

no data yet
no data yet
Response from server : data packet with ID 12

no data yet
no data yet
Response from server : data packet with ID 13

no data yet
no data yet
Response from server : data packet with ID 14

no data yet
no data yet
Response from server : data packet with ID 15

no data yet
no data yet
Response from server : data packet with ID 16

no data yet
no data yet
Response from server : data packet with ID 17

no data yet
no data yet
Response from server : data packet with ID 18

no data yet
no data yet
Response from server : data packet with ID 19

no data yet
no data yet
Response from server : data packet with ID 20

no data yet
no data yet
Response from server : stop

sender has told me to end the connection

 

你可能感兴趣的:(UDP通讯实例,关于非阻塞设置)