使用多个线程在c中的非套接字错误上进行套接字操作

时间:2017-05-05 08:41:06

标签: c multithreading sockets udp

我正在尝试使用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;
}

1 个答案:

答案 0 :(得分:0)

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

但是这里还有其他问题。

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

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

同样,您不能仅仅忽略来自socket(), bind(), listen(), connect(), send(), ...的错误。