C语言网络编程——udp通信

网络编程——udp通信
客户端:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
typedef struct message{
    char mtext[1024];
    int mtype;
}Message;
void do_user(void *arg)
{
    int ret;
    char buf[1024];
    int sockfd = *(int *)arg;
    struct sockaddr_in peer_addr;
    int peer_addr_len = sizeof(peer_addr);
    while (1)
    {
        printf("Wait to recive...\n");
        ret = recvfrom(sockfd, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&peer_addr, &peer_addr_len);
        if (ret < 0)
        {
            perror("Fail to recvfrom...");
        }
        buf[ret] = '\0';
        printf("收到来自<%s><%d>%d字节的消息 > %s\n", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port), ret, buf);
        bzero(&peer_addr, sizeof(peer_addr));
    }
}
char *message(char *msg, int msg_len)
{
    bzero(msg, sizeof(msg_len));
    printf("input msg >");
    fgets(msg, msg_len, stdin);
    msg[strlen(msg) - 1] = '\0';
    return msg;
}
int main(int argc, char const *argv[])
{
    int ret;
    int sockfd;
    pthread_t pid;
    char msg[1024];
    int msg_len = sizeof(msg);
    struct sockaddr_in self_addr;
    struct sockaddr_in dest_addr;
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("fail to create sockfd");
        exit(-1);
    }
    self_addr.sin_family = AF_INET;
    self_addr.sin_port = htons(8888);
    self_addr.sin_addr.s_addr = inet_addr("192.168.2.193");
    ret = bind(sockfd, (const struct sockaddr *)&self_addr, sizeof(self_addr));
    if (ret < 0)
    {
        perror("fail to bond");
        exit(-1);
    }
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_addr.s_addr = inet_addr("192.168.2.193");
    dest_addr.sin_port = htons(7777);
    ret = pthread_create(&pid, NULL, (void *)do_user, &sockfd);
    while (1)
    {
        getchar();
        message(msg, msg_len);
        printf("msg > %s,len > %ld\n", msg, strlen(msg));
        ret = sendto(sockfd, msg, strlen(msg), 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr));
        if (ret < 0)
        {
            perror("fail to sendto..");
        }
    }

    return 0;
}

服务器端:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
char *message(char *msg, int msg_len)
{

    bzero(msg, sizeof(msg_len));
    printf("input msg >");
    fgets(msg, msg_len, stdin);
    msg[strlen(msg) - 1] = '\0';
    return msg;
}
void do_user(void *arg)
{
    int ret;
    char msg[1024];
    int msg_len = sizeof(msg);
    int sockfd = *(int *)arg;
    struct sockaddr_in dest_addr;
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_addr.s_addr = inet_addr("192.168.2.193");
    dest_addr.sin_port = htons(8888);

    //printf("msg > %s,len > %ld\n", msg, strlen(msg));
    while (1)
    {
        getchar();
        message(msg, msg_len);
        ret = sendto(sockfd, msg, strlen(msg), 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr));
        if (ret < 0)
        {
            perror("fail to sendto..");
        }
    }
}
int main(int argc, char const *argv[])
{
    int ret;
    int sockfd;
    char buf[1024];
    pthread_t pid;
    struct sockaddr_in self_addr;
    struct sockaddr_in peer_addr;
    int peer_addr_len = sizeof(peer_addr);
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("fail to create sockfd");
        exit(-1);
    }
    self_addr.sin_family = AF_INET;
    self_addr.sin_port = htons(7777);
    self_addr.sin_addr.s_addr = inet_addr("192.168.2.193");
    ret = bind(sockfd, (const struct sockaddr *)&self_addr, sizeof(self_addr));
    if (ret < 0)
    {
        perror("fail to bind");
        exit(-1);
    }
    ret = pthread_create(&pid, NULL, (void *)do_user, &sockfd);
    while (1)
    {
        printf("Wait to recive...\n");
        ret = recvfrom(sockfd, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&peer_addr, &peer_addr_len);
        if (ret < 0)
        {
            perror("Fail to recvfrom...");
        }
        buf[ret] = '\0';
        printf("收到来自<%s><%d>%d字节的消息 > %s\n", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port), ret, buf);
        bzero(&peer_addr, sizeof(peer_addr));
    }

    return 0;
}


结果:
服务器与客户端可双向通信
在这里插入图片描述
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值