gpt4 book ai didi

c - 在同一 C 程序中的两个线程之间发送 UDP 消息 (Linux)

转载 作者:行者123 更新时间:2023-11-30 17:28:09 25 4
gpt4 key购买 nike

对于一个类(class),我的小组被分配编写一个具有两个线程的程序,其中一个线程向另一个线程发送 20 条 UDP 消息。传输线程发送消息的 IP 地址、端口号和速率作为命令行参数传入。当我们尝试运行该程序时,我们收到错误“发送失败:错误地址”。我们一直试图找出原因,但我们被难住了。我们认为这与recAddr结构有关。有人有什么想法吗?

#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/time.h>

const int ipTx = 1, ipRx = 2, portTx = 3, portRx = 4, TxRate = 5;
const int wait_five = 5, num_msgs = 20, wait_ten = 10, wait_twenty = 20;
const int SEC_TO_MILLI = 1000;
int *message;

void *send_msg( void * );
void *receive_msg( void * );

int main( int argc, char *argv[] )
{
message = 0;
pthread_t sendThread, recieveThread;
int sendFail, recFail;

sendFail = pthread_create(&sendThread, NULL, send_msg, (void*) argv);
if(sendFail)
{
printf("Error creating send thread\n");
}

recFail = pthread_create(&recieveThread, NULL, receive_msg, (void*) argv);
if(recFail)
{
printf("Error creating receive thread\n");
}

pthread_join( sendThread, NULL);
pthread_join( recieveThread, NULL);

printf("Send thread and receive thread done. Program terminating.\n");

return 0;
}

void *send_msg( void *argv)
{
char **args = (char **)argv;
int sendSocket;
if((sendSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("cannot create send socket");
return;
}
struct sockaddr_in myAddr;
memset((char *)&myAddr, 0, sizeof(myAddr));
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = inet_addr(args[ipTx]);
int port = htons(atoi(args[portTx]));
myAddr.sin_port = htons(port);
if (bind(sendSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)) < 0)
{
perror("send socket bind failed");
return;
}

struct sockaddr_in recAddr;
memset((char*)&recAddr, 0, sizeof(recAddr));
recAddr.sin_family = AF_INET;
recAddr.sin_port = htons(atoi(args[portRx]));
recAddr.sin_addr.s_addr = inet_addr(args[ipRx]);

printf("Sleeping for 5 seconds\n");
sleep(wait_five);

int sendRate = (intptr_t) args[TxRate];
sendRate /= SEC_TO_MILLI;
int i;
for(i = 0; i < num_msgs; i++)
{
printf("I am TX and I am going to send a %i\n", message);
if(sendto(sendSocket, message, sizeof(message), 0, (struct sockaddr *)&recAddr, sizeof(recAddr) ) < 0)
{
perror("sendto failed");
return;
}
*message++;
sleep(sendRate);
}
printf("Sleeping for 10 seconds\n");
sleep(wait_ten);
}

void *receive_msg( void *argv)
{
const int BUFF_SIZE = 2048;

char **args = (char **)argv;

struct sockaddr_in myAddress;
struct sockaddr_in remoteAddress;
socklen_t addressLength = sizeof(myAddress);
int recvLength;
int receiveSocket;
unsigned int buf[BUFF_SIZE];

if((receiveSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("cannot create receive socket\n");
return;
}

memset((char*)&myAddress, 0, sizeof(myAddress));
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr(args[ipRx]);;
myAddress.sin_port = htons(atoi(args[portRx]));

struct timeval rec_timeout;
rec_timeout.tv_sec = wait_twenty;
rec_timeout.tv_usec = 0;

if(setsockopt(receiveSocket, SOL_SOCKET, SO_RCVTIMEO, (const void *)&rec_timeout, sizeof(rec_timeout)) < 0)
{
perror("cannot set timeout option\n");
return;
}

if(bind(receiveSocket, (const struct sockaddr *)&myAddress, sizeof(myAddress)) < 0)
{
perror("cannot bind receive socket\n");
return;
}

for(;;)
{
printf("waiting on port %d\n", atoi(args[portRx]));
recvLength = recvfrom(receiveSocket, buf, BUFF_SIZE, 0, (struct sockaddr*)&remoteAddress, &addressLength);
printf("received %d bytes\n", recvLength);
if (recvLength > 0)
{
buf[recvLength] = 0;
printf("I am RX and I got a \"%d\"\n", buf);
}
}
}

这是我们使用的脚本文件中的代码:

#!/bin/bash
gcc -pthread -o prototype1.out prototype1.c
./prototype1.out 137.104.21.4 137.104.21.4 7084 7085 50

谢谢!

最佳答案

sendto(sendSocket, message, sizeof(message), 0, (struct sockaddr *)&recAddr, sizeof(recAddr) )

这里的问题是message为NULL。

这个指针是

1) 初始化为 NULL。 sendto() 的第二个参数应指向有效内存,其大小至少为第三个参数指定的大小。

2) 代码假定该指针初始化为至少 sizeof(message) 字节的内存。

关于c - 在同一 C 程序中的两个线程之间发送 UDP 消息 (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26110732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com