gpt4 book ai didi

c - recvfrom() 收到一些消息后挂起?

转载 作者:行者123 更新时间:2023-11-30 14:45:19 26 4
gpt4 key购买 nike

我正在用 C 语言编写一个简单的多线程客户端-服务器 UDP 程序,其中我一次向服务器发送多条消息,并在接收线程中等待回复。我这样做是为了测试发送和接收数据包的时间。

我已经用我的服务器在本地测试了 10 个数据包,并且我正确接收了所有 10 个数据包,此时我的程序终止。

但是,当我使用远程服务器测试客户端时,我的程序在收到大约 6 个数据包后挂起。我知道程序卡在 recvfrom() 调用上,但我不知道为什么。

我尝试更改调用中的参数并将调用放入 while 循环本身,但无济于事。

这是我的客户端代码:

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

#define SERVER_IP "127.0.0.1"
#define PORT 7851
#define DATA_SIZE 99
#define NUM_MSGS 10

// function declaration for connection handler
void *connection_handler(void *);
struct timeval times[NUM_MSGS][2];

struct sockaddr_in serverAddress;

int main() {
int socketFd;
char buf[DATA_SIZE];
//struct sockaddr_in serverAddress;
char msg[DATA_SIZE];
int size, numSent;
time_t timeSent;
pthread_t threadId;

// Create the socket
if ((socketFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failure");
exit(-1);
}

// Initialize server information
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddress.sin_port = htons(PORT);

// Print server information
printf("IP address of server is: %s\n", inet_ntoa(serverAddress.sin_addr));
printf("Server port is: %d\n", (int) ntohs(serverAddress.sin_port));

// Create new thread to handle received messages from server
if (pthread_create(&threadId, NULL, connection_handler, (void *)&socketFd) < 0) {
perror("Thread creation failure");
exit(-1);
}

numSent = 0;
int i, j, n;
for (i = 0; i < NUM_MSGS; i++) {
// Get current time and create message
gettimeofday(&times[i][0], NULL);
n = snprintf(msg, DATA_SIZE, "%d", numSent);
msg[n] = '\0';
if (n < 0 || n > DATA_SIZE) {
perror("Message creation failure");
}

// Send msg to server
size = sendto(socketFd, (char *)msg, strlen(msg), 0, (struct sockaddr *) &serverAddress, sizeof(serverAddress));

// Check for sendto error
if (size == -1) {
perror("sendto failure");
exit(-1);
}

printf("Client message %d sent.\n", numSent);
numSent++;
}
// Wait for straggler replies
sleep(2);

pthread_join(threadId, NULL);

//print out times
for (i = 0; i < NUM_MSGS; i++){
for (j = 0; j < 2; j++){
printf("[%d][%d] = [%ld.%06ld]\n", i, j, (long int)(times[i][j].tv_sec), (long int)(times[i][j].tv_usec));
}
}

close(socketFd);
return 0;
}


// Connection handler function for new thread created to receive server replies
void *connection_handler(void *newSocketPtr) {
// cast newSocketPtr to integer ptr then dereference to get the socketFd
int socketFd = *(int *)newSocketPtr;
char buf[DATA_SIZE];
int addrLen, size;
int numReceived = 0;
time_t timeSent, timeReceived, diff, totalTime, avgRTT;

addrLen = sizeof(serverAddress);
while ((size = recvfrom(socketFd, (char *)buf, DATA_SIZE, 0, (struct sockaddr *) &serverAddress, &addrLen))!= -1){ // What about when packets get dropped???

printf("Expecting packet %d\n", numReceived+1);

// Check for recvfrom error
if (size == -1){
perror("recvfrom failure");
exit(-1);
}

buf[size] = '\0';
// Get current time
gettimeofday(&times[atoi(buf)][1], NULL);

printf("Message received from server: %s\n", buf);

if (numReceived == NUM_MSGS - 1)break;
numReceived++;
printf("num received is %d\n", numReceived);


}

close(socketFd);
pthread_exit(NULL);
}

最佳答案

[...] tested locally with my server with 10 packets [...] test my client with a remote server, my program hangs after I receive about 6 packets.

UDP 根本不保证数据包一定会被传送。您的远程服务器速度变慢并丢失了四个数据包。

关于c - recvfrom() 收到一些消息后挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53127709/

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