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



我正在用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: %sn", inet_ntoa(serverAddress.sin_addr));
printf("Server port is: %dn", (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] = ''; 
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 %dn", numReceived+1);    
// Check for recvfrom error
if (size == -1){
perror("recvfrom failure");
exit(-1);
}
buf[size] = ''; 
// Get current time
gettimeofday(&times[atoi(buf)][1], NULL);
printf("Message received from server: %sn", buf);
if (numReceived == NUM_MSGS - 1)break;
numReceived++;
printf("num received is %dn", numReceived);   

}
close(socketFd);
pthread_exit(NULL);
}

[…]在我的服务器上用10个数据包进行了本地测试[…]用远程服务器测试我的客户端,我的程序在收到大约6个数据包后挂起

UDP根本不能保证数据包将被传递。您的远程服务器速度减慢,丢失了四个数据包。

相关内容

  • 没有找到相关文章

最新更新