当前位置:   article > 正文

C语言网络编程——udp通信_c语言udp通信代码

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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

服务器端:

#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;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/187175
推荐阅读
相关标签
  

闽ICP备14008679号