UDP服务器的简单实现

服务器端:

#include 
#include 
#include 
#include 
#include 
#include 



#define ERR_EXIT(m) \
    do\
    {\
        perror(m);\
        exit(EXIT_FAILURE);\
    }while(0)

int main(int argc, char* argv[])
{
    if(argc < 3)
    {
        printf("./server [addr] [port]\n");
        exit(EXIT_SUCCESS);
    }
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock < 0)
        ERR_EXIT("socket");

    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = htons(atoi(argv[2]));
    local.sin_addr.s_addr = inet_addr(argv[1]);

    if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0)
        ERR_EXIT("bind");

    char buf[1024];
    struct sockaddr_in client;
    while(1)
    {
        buf[0] = 0;
        socklen_t len = sizeof(client);
        ssize_t s = recvfrom(sock, buf, sizeof(buf)-1, 0, (struct sockaddr*)&client, &len);
        buf[s] = 0;
        if(strcmp(buf, "quit\n") == 0)
        {
            printf("client quit!\n");
            continue;
        }
        if(s > 0)
        {
            printf("[%s:%d]: %s", inet_ntoa(client.sin_addr), ntohs(client.sin_port), buf);
            sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&client, sizeof(client));
        }
    }


    return 0;
}

客户端:

#include 
#include 
#include 
#include 
#include 
#include 



#define ERR_EXIT(m) \
    do\
    {\
        perror(m);\
        exit(EXIT_FAILURE);\
    }while(0)

int main(int argc, char* argv[])
{
    if(argc < 3)
    {
        printf("./client [addr] [port]\n");
        exit(EXIT_SUCCESS);
    }
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock < 0)
        ERR_EXIT("socket");

    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    server.sin_addr.s_addr = inet_addr(argv[1]);

    char buf[1024];
    struct sockaddr_in peer;
    while(1)
    {
        buf[0] = 0;
        socklen_t len = sizeof(peer);
        write(1, "please input:>", strlen("please input:>"));
        ssize_t s = read(0, buf, sizeof(buf)-1);
        if(s > 0)
        {
            buf[s] = 0;
            sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&server, sizeof(server));
            if(strcmp(buf, "quit\n") == 0)
                exit(EXIT_SUCCESS);
            s = recvfrom(sock, buf, sizeof(buf)-1, 0, (struct sockaddr*)&peer, &len);
            if(s > 0)
            {
                buf[s] = 0;
                printf("server: %s", buf);

            }
        }
    }


    return 0;
}

通过UDP服务器端和客户端程序,可在局域网内进行对话:
UDP服务器的简单实现_第1张图片
由于条件有限,因此这里就在本机两个终端演示。当两台主机处于同一个局域网下时,可用此程序实现两台主机的对话。

你可能感兴趣的:(网络)