Linux-UDP通信

服务端

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

#define SERV_PORT 8000
#define SERV_IP_ADDR "127.0.0.1"
#define QUIT_STR "quit"
#define BUFSIZE 128

using namespace std;

int main()
{
    int fd = -1;
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)//DGRAM为UDP
    {
        perror("socket");//创建socket失败
        exit(1);
    }
    struct sockaddr_in sin;
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(SERV_PORT);
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        perror("bind");//绑定失败
        exit(1);
    }
    char buf[BUFSIZE];
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);

    while (1)
    {
        bzero(buf, BUFSIZE);
        if (recvfrom(fd, buf, BUFSIZE - 1, 0, (struct sockaddr *)&cin, &addrlen) < 0)
        {
            perror("recvfrom");
            exit(1);
        }
        char ipv4_addr[16];
        if (!inet_ntop(AF_INET, (void *)&cin.sin_addr, ipv4_addr, sizeof(cin)))
        {
            perror("inet_ntop");
            exit(1);
        }
        printf("Received from(%s:%d),data:%s\n", ipv4_addr, ntohs(sin.sin_port), buf);
        if (!strncasecmp(buf, QUIT_STR, strlen(QUIT_STR)))
        {
            printf("Client(%s:%d) is exiting!\n", ipv4_addr, ntohs(sin.sin_port));
        }
        bzero(buf, BUFSIZE);
        sprintf(buf, "%s", "test udp");
        sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&cin, sizeof(cin));
    }
    close(fd);
    return 0;
}

客户端

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define MYPORT 8000    //服务器监听端口号

int main()
{
    cout << "客户端已启动~" << endl;
    sockaddr_in Server_addr, Client_addr;
    socklen_t addrlen = sizeof(Server_addr);
    int Client_Socket = socket(AF_INET, SOCK_DGRAM, 0);
    if (Client_Socket == -1)
    {
        cerr << "Socket创建失败!!!" << endl;
        return -1;
    }
    Client_addr.sin_family = AF_INET;
    Client_addr.sin_addr.s_addr = htonl(INADDR_ANY); // IP地址,需要进行网络序转换,INADDR_ANY:本地地址
    Client_addr.sin_port = htons(8001);              //端口号,需要网络序转换
    if (bind(Client_Socket, (struct sockaddr *)&Client_addr, sizeof(Client_addr)) < 0)//客户端绑定端口号,也可不绑定
    {
        printf("client socket bind fail!\n");
        return -1;
    }

    memset(&Server_addr, 0, sizeof(Server_addr));
    Server_addr.sin_family = AF_INET;
    Server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    Server_addr.sin_port = htons(SERVER_PORT);

    if (-1 == connect(Client_Socket, (sockaddr *)&Server_addr, sizeof(Server_addr)))
    {
        cerr << "客户端连接失败!!!" << endl;
        return -1;
    }
    cout << "客户端连接成功~" << endl;

    string str;
    char Buffer[1000];
    while (true)
    {
        memset(Buffer, 0, sizeof(Buffer));//清空缓存区
        if (recvfrom(Client_Socket, Buffer, BUFSIZE - 1, 0, (struct sockaddr *)&Server_addr, &addrlen) < 0)
        {
            perror("recvfrom");
            exit(1);
        }
        if (strcmp(Buffer, "exit") == 0)
        {
            break;
        }
        cout << "收到:\t" << Buffer << endl;
    }
    cout << "已断开连接!!!" << endl;
    close(Client_Socket);
    return 0;
}

你可能感兴趣的:(linux,udp,c++,网络协议,网络)