gpt4 book ai didi

c - 使用多线程对C中的非套接字错误进行套接字操作

转载 作者:行者123 更新时间:2023-11-30 16:46:21 24 4
gpt4 key购买 nike

我正在尝试使用 udp 在同一套接字上实现自主发送和接收行为。我面临的问题是,一旦我完成发送并等待第二个线程继续从服务器接收数据,它就会抛出“非套接字错误上的套接字操作”。但是,我成功收到前两个数据包,但随后收到一些垃圾,然后出现此错误。我尝试过使用静态 sockfd 以防套接字在第二个线程上接收无效,但问题仍然存在。如有任何帮助,我们将不胜感激。

#include "client.h"

struct global_table{
struct sockaddr_in *serveraddr;
int sockID;
};

void *recvFromServer(struct global_table *rec){
char recBuf[RECVBUFSIZE];
int serverSize = sizeof(rec->serveraddr);
// It successfully starts the thread and recieves the first two packets but then socket operation on non socket error comes.
while(1)
{
int n = recvfrom(rec->sockID, recBuf, RECVBUFSIZE, 0, &rec->serveraddr, &serverSize);
if (n < 0)
perror("ERROR in recvfrom");
decryptData(recBuf);
printf("Recieved: %s\n", recBuf);
}

}

void pingServer(char *hostname, int portno)
{
int sockfd; // Already Tried with static sockfd in case it gets destroyed if the main thread finsihes.
int serverlen, n;
static struct sockaddr_in serveraddr;
struct sockaddr_in client_addr;
struct hostent *server;
char *buf;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
perror("ERROR opening socket");

server = gethostbyname(hostname);
if (server == NULL)
perror("ERROR, no host found");

bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);

client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
client_addr.sin_port = htons(5500);

if (bind(sockfd,(struct sockaddr *)&client_addr, sizeof(struct sockaddr)) == -1)
perror("Socket could not be binded");

if(setsockopt(sockfd,IPPROTO_IP,IP_TOS,&tos,sizeof(tos)))
perror("Could not set socket option");

pthread_t tid;
static struct global_table server_info;
server_info.sockID = sockfd;
server_info.serveraddr = &serveraddr;

pthread_create(&tid,NULL,recvFromServer, &server_info);

serverlen = sizeof(serveraddr);

while(1)
{
//Reads the entire file and sends it through udp packets. No problem in sending.
}
pthread_join(tid, NULL); // Once finishes sending, it should wait for the second thread to continue recieving data from the server.

}

int main(int argc, char **argv) {
char *hostname;
int portno;
if (argc != 3)
perror("usage: <hostname> <port>\n");

hostname = argv[1];
portno = atoi(argv[2]);
pingServer(hostname, portno);
return 0;
}

最佳答案

您提到的错误来自于使用无效的 FD,例如关闭的 FD。

但是这里还有其他问题。

如果recv()返回-1,仅仅打印错误消息并继续是不够的。您必须关闭套接字并退出读取循环。

当它返回零时,您完全忽略了这种情况,这意味着对等方已关闭连接,您还必须关闭套接字并退出读取循环。

类似地,您不能简单地忽略来自 socket()、bind()、listen()、connect()、send() 等的错误

关于c - 使用多线程对C中的非套接字错误进行套接字操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43800323/

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