aboutsummaryrefslogtreecommitdiffstats
path: root/testClient.c
blob: bbd2529a0142b83310277379b0938ff5858f3713 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
// 编写一段C程序,使用pthread创建两个线程
// 发送线程将数据发送到127.0.0.1的5001端口,每0.5s一次
// 接收线程接收127.0.0.1的5001端口的数据并打印

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <termios.h>
#include <unistd.h>

pthread_t udpSend, udpRecv;
pthread_mutex_t mutex;
int port = 5001;
char clientIP[20] = "127.0.0.1";

void udpSenderThread() {
    struct sockaddr_in serverAddr;
    int sockfd = -1;

    // 创建UDP套接字
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        fprintf(stderr, "Error: Failed to create socket\n");
        return;
    }

    memset((char *)&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(port);
    serverAddr.sin_addr.s_addr = inet_addr(clientIP);

    while (1) {
        char values[100] = {0};
        // strcpy(values, "Hello, world!");
        pthread_mutex_lock(&mutex);
        printf("Input the data you want to send: ");
        scanf("%s", values);
        pthread_mutex_unlock(&mutex);
        sendto(sockfd, values, strlen(values), 0,
               (struct sockaddr *)&serverAddr, sizeof(serverAddr));
        // printf("Send: %s\n", values);
        sleep(1);
    }
}

void udpReceiverThread() {
    int sockfd = -1;
    struct sockaddr_in serverAddr;
    struct sockaddr_in clientAddr;
    socklen_t clientAddrLen = sizeof(clientAddr);
    char buffer[1024] = {0};

    // 创建UDP套接字
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        fprintf(stderr, "Error: Failed to create socket\n");
        return;
    }

    memset((char *)&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(port - 1);
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) ==
        -1) {
        fprintf(stderr, "Error: Failed to bind socket\n");
        close(sockfd);
        return;
    }

    while (1) {
        pthread_mutex_lock(&mutex);
        int len = recvfrom(sockfd, buffer, 1024, 0,
                           (struct sockaddr *)&clientAddr, &clientAddrLen);
        pthread_mutex_unlock(&mutex);
        if (len == -1) {
            fprintf(stderr, "Error: Failed to receive data\n");
            close(sockfd);
            return;
        }
        buffer[len] = '\0';
        printf("Received: %s\n", buffer);
    }
}

int main() {
    pthread_create(&udpSend, NULL, udpSenderThread, NULL);
    // pthread_create(&udpRecv, NULL, udpReceiverThread, NULL);
    pthread_join(udpSend, NULL);
    // pthread_join(udpRecv, NULL);
}