linux c语言udp 多线程socket接受192.168.1.1的信息发送给127.0.0.1,接受192.168.131.1的信息发送给127.0.0.2
时间: 2023-08-02 22:06:52 浏览: 152
C# UDP多线程发送接收
好的,这个问题需要使用多线程来同时接收来自192.168.1.1和192.168.131.1的数据包,并将它们分别发送到127.0.0.1和127.0.0.2。以下是一个简单的多线程示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#define BUF_SIZE 1024
#define SRC_IP1 "192.168.1.1"
#define SRC_IP2 "192.168.131.1"
#define DST_IP1 "127.0.0.1"
#define DST_IP2 "127.0.0.2"
#define SRC_PORT 8080
#define DST_PORT1 8081
#define DST_PORT2 8082
// 线程参数结构体
struct thread_param {
int sock;
struct sockaddr_in addr;
};
// 接收数据并发送的线程函数
void* recv_send_thread(void* arg) {
struct thread_param* param = (struct thread_param*)arg;
int sock = param->sock;
struct sockaddr_in addr = param->addr;
char buf[BUF_SIZE];
int ret;
// 从指定地址接收数据并发送到指定地址
while (1) {
memset(buf, 0, sizeof(buf));
ret = recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL);
if (ret < 0) {
perror("recvfrom error");
break;
}
if (addr.sin_addr.s_addr == inet_addr(SRC_IP1)) {
ret = sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&(struct sockaddr_in){AF_INET, htons(DST_PORT1), inet_addr(DST_IP1)}, sizeof(struct sockaddr_in));
} else if (addr.sin_addr.s_addr == inet_addr(SRC_IP2)) {
ret = sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&(struct sockaddr_in){AF_INET, htons(DST_PORT2), inet_addr(DST_IP2)}, sizeof(struct sockaddr_in));
}
if (ret < 0) {
perror("sendto error");
break;
}
}
// 释放线程参数内存
free(param);
return NULL;
}
int main() {
int sock1, sock2, ret;
struct sockaddr_in addr1, addr2;
// 创建接收端UDP socket1
sock1 = socket(AF_INET, SOCK_DGRAM, 0);
if (sock1 < 0) {
perror("socket create error");
return -1;
}
// 绑定接收端IP1和端口
memset(&addr1, 0, sizeof(addr1));
addr1.sin_family = AF_INET;
addr1.sin_port = htons(SRC_PORT);
addr1.sin_addr.s_addr = inet_addr(SRC_IP1);
if (bind(sock1, (struct sockaddr*)&addr1, sizeof(addr1)) < 0) {
perror("bind error");
close(sock1);
return -1;
}
// 创建接收端UDP socket2
sock2 = socket(AF_INET, SOCK_DGRAM, 0);
if (sock2 < 0) {
perror("socket create error");
close(sock1);
return -1;
}
// 绑定接收端IP2和端口
memset(&addr2, 0, sizeof(addr2));
addr2.sin_family = AF_INET;
addr2.sin_port = htons(SRC_PORT);
addr2.sin_addr.s_addr = inet_addr(SRC_IP2);
if (bind(sock2, (struct sockaddr*)&addr2, sizeof(addr2)) < 0) {
perror("bind error");
close(sock1);
close(sock2);
return -1;
}
// 创建线程1用于接收来自192.168.1.1的数据包并发送到127.0.0.1
pthread_t tid1;
struct thread_param* param1 = (struct thread_param*)malloc(sizeof(struct thread_param));
param1->sock = sock1;
param1->addr = addr1;
ret = pthread_create(&tid1, NULL, recv_send_thread, param1);
if (ret < 0) {
perror("pthread_create error");
close(sock1);
close(sock2);
free(param1);
return -1;
}
// 创建线程2用于接收来自192.168.131.1的数据包并发送到127.0.0.2
pthread_t tid2;
struct thread_param* param2 = (struct thread_param*)malloc(sizeof(struct thread_param));
param2->sock = sock2;
param2->addr = addr2;
ret = pthread_create(&tid2, NULL, recv_send_thread, param2);
if (ret < 0) {
perror("pthread_create error");
close(sock1);
close(sock2);
free(param1);
free(param2);
return -1;
}
// 等待线程退出
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
// 关闭socket
close(sock1);
close(sock2);
return 0;
}
```
该示例代码中,我们首先创建了两个UDP socket用于分别接收来自192.168.1.1和192.168.131.1的数据包,并绑定了IP地址和端口。然后,我们创建了两个线程,分别用于接收数据并发送到指定地址。最后,我们等待线程退出并关闭socket。
注意:本示例代码仅供参考,实际应用中还需要进行错误处理、超时处理等。
阅读全文