美文网首页
UDP简单例子

UDP简单例子

作者: sxs7 | 来源:发表于2019-06-01 10:00 被阅读0次

    预期结果:客户端发送hello,服务端响应world

    代码:
    服务端

    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <pthread.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    
    int main(int argc, char * *argv)
    {
        struct sockaddr_in addr;
        addr.sin_family     = AF_INET;
        addr.sin_port       = htons(9876);
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        char buff_recv[512] = {0};
        char buff_send[512] = "world";
    
        struct sockaddr_in clientAddr;
        int n;
        int len = sizeof(clientAddr);
    
        int sock;   
    
        printf("Welcome! This is a UDP server.\n");
    
        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            printf("socket error.\n");
            exit(1);
        }
    
        if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
        {
            printf("bind error.\n");
            exit(1);
        }
    
        while (1)
        {
            n = recvfrom(sock, buff_recv, 511, 0, (struct sockaddr *) &clientAddr, &len);
            if (n > 0)
            {
                buff_recv[n] = 0;
                printf("recv data from client:%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff_recv);
    
                n = sendto(sock, buff_send, n, 0, (struct sockaddr *) &clientAddr, sizeof(clientAddr));
                if (n < 0)
                {
                    printf("sendto error.\n");
                    break;
                }
            }
            else 
            {
                printf("recv error.\n");
                break;
            }
        }
    
        return 0;
    }
    
    
    

    客户端

    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <pthread.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    
    int main(int argc, char * *argv)
    {   
        struct sockaddr_in addr;
        int sock;
    
        addr.sin_family     = AF_INET;
        addr.sin_port       = htons(9876);
        addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
        char buff_send[512] = "Hello";
        char buff_recv[512] = {0};
        int len = sizeof(addr);
    
        int n = 0;
    
        printf("This is a UDP client\n");
    
        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            printf("socket error.\n");
            exit(1);
        }
    
        if (addr.sin_addr.s_addr == INADDR_NONE)
        {
            printf("Incorrect ip address!");
            close(sock);
            exit(1);
        }
    
        n = sendto(sock, buff_send, strlen(buff_send), 0, (struct sockaddr *) &addr, sizeof(addr));
        if (n < 0)
        {
            printf("sendto error.\n");
            close(sock);
            return 0;
        }
    
        n = recvfrom(sock, buff_recv, 512, 0, (struct sockaddr *) &addr, &len);
        if (n > 0)
        {
            buff_recv[n] = 0;
            printf("received from sever:");
            puts(buff_recv);
        }
        else if (n == 0)
            printf("server closed.\n");
        else if (n == -1)
            printf("recvfrom error.\n");
    
        close(sock);
        
        return 0;
    }
    
    
    

    运行结果:


    运行结果.png

    相关文章

      网友评论

          本文标题:UDP简单例子

          本文链接:https://www.haomeiwen.com/subject/sxsotctx.html