基于windows实现UDP通信

windows下的套接字编程:

>导入头文件winsock2.h

>链接ws2_32.lib库

client端: 

/*uecho_client_win.c*/
#include 
#include 
#include 
#include 

#define BUF_SIZE 30

void ErrorHandling(char *message);

int main(int argc, char *argv[])
{
    WSADATA wsa_data;
    SOCKET sock;
    char message[BUF_SIZE];
    int str_len;
    int clnt_addr_size;

    SOCKADDR_IN serv_addr;
    if(argc != 3)
    {
        printf("Usage: %s  \n", argv[0]);
        exit(1);
    }
    if(WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
    {
        ErrorHandling("WSAStartup() error!");
    }

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock == INVALID_SOCKET)
    {
        ErrorHandling("UDP socket create error");

    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons(atoi(argv[2]));
    connect(sock, (SOCKADDR*)&serv_addr, sizeof(serv_addr));

    while(1)
    {
        fputs("insert message(q to quit):",stdout);
        fgets(message, sizeof(message), stdin);
        if(!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
        {
            break;
        }
        send(sock, message, strlen(message), 0);
        str_len = recv(sock, message, sizeof(message) - 1, 0);
        message[str_len] = 0;
        printf("Message from server:%s",message);
    }
    closesocket(sock);
    WSACleanup();
    return 0;
}
void ErrorHandling(char *message)
{
    fputs(message, stderr);
    fputc('\n', stderr);
}

server端:

/*uecho_server_win.c*/
#include 
#include 
#include 
#include 

#define BUF_SIZE 30

void ErrorHandling(char *message);

int main(int argc, char *argv[])
{
    WSADATA wsa_data;
    SOCKET serv_sock;
    char message[BUF_SIZE];
    int strlen;
    int clnt_addr_size;

    SOCKADDR_IN serv_addr, clnt_addr;
    if(argc != 2)
    {
        printf("Usage: %s \n", argv[0]);
        exit(1);
    }
    if(WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
    {
        ErrorHandling("WSAStartup() error!");
    }

    serv_sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(serv_sock == INVALID_SOCKET)
    {
        ErrorHandling("UDP socket create error");

    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(atoi(argv[1]));

    if(bind(serv_sock, (SOCKADDR*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR)
    {

        ErrorHandling("bind() error");
    }

    while(1)
    {
        clnt_addr_size = sizeof(clnt_addr);
        strlen = recvfrom(serv_sock, message, BUF_SIZE, 0,
                           (SOCKADDR*)&clnt_addr, &clnt_addr_size);
        printf("message from client:%s\n",message);
        sendto(serv_sock, message, strlen, 0,(SOCKADDR*)&clnt_addr, sizeof(clnt_addr));
    }
    closesocket(serv_sock);
    WSACleanup();
    return 0;
}
void ErrorHandling(char *message)
{
    fputs(message, stderr);
    fputc('\n', stderr);
}

由于需要给主函数传入参数,无法在ide中运行,因此在dos下编译运行:

windows下的GCC编译器可安装mingw,安装过程可参考https://blog.csdn.net/qq_38080117/article/details/78022390

编译时需要链接ws2_32

如下为client端的运行结果:

E:\codeblock_code\windows_udp>gcc uecho_client_win.c -o client -lws2_32

E:\codeblock_code\windows_udp>dir
 驱动器 E 中的卷是 DATA
 卷的序列号是 E84C-F75C

 E:\codeblock_code\windows_udp 的目录

2019/07/09  17:26              .
2019/07/09  17:26              ..
2019/07/09  17:26            32,664 client.exe
2019/07/09  17:26            31,741 server.exe
2019/07/09  17:23             1,530 uecho_client_win.c
2019/07/09  17:25             1,543 uecho_server_win.c
               4 个文件         67,478 字节
               2 个目录 323,571,404,800 可用字节

E:\codeblock_code\windows_udp>client.exe 127.0.0.1 5000
insert message(q to quit):hello
Message from server:hello
insert message(q to quit):server
Message from server:server
insert message(q to quit):i am client
Message from server:i am client
insert message(q to quit):q

下面是与client对应的server的运行结果:

E:\codeblock_code\windows_udp>gcc uecho_server_win.c -o server -lws2_32

E:\codeblock_code\windows_udp>dir
 驱动器 E 中的卷是 DATA
 卷的序列号是 E84C-F75C

 E:\codeblock_code\windows_udp 的目录

2019/07/09  17:26              .
2019/07/09  17:26              ..
2019/07/09  17:24            32,664 client.exe
2019/07/09  17:26            31,741 server.exe
2019/07/09  17:23             1,530 uecho_client_win.c
2019/07/09  17:25             1,543 uecho_server_win.c
               4 个文件         67,478 字节
               2 个目录 323,571,404,800 可用字节

E:\codeblock_code\windows_udp>server.exe 5000
message from client:hello

message from client:server

message from client:i am client

 

你可能感兴趣的:(TCP/IP网络编程)