C套接字编程中的recvfrom错误?

时间:2017-07-22 10:58:48

标签: c sockets udp

对不起我的英语

我必须使用UDP协议实现可靠的通信;首先,我想要实现一个简单的程序;客户端向缓冲区中的NULL发送消息;服务器将其理解为请求,然后发送给客户端响应,这是一个数字;

代码客户端:

/*
 * newClient.c
 *
 *  Created on: 22 lug 2017
 *      Author: claudio
 */


#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SERV_PORT   5193
#define MAXLINE     1024


void err_exit(char* str)
{
    perror(str);
    exit(EXIT_FAILURE);
}




int request_to_server(int sockfd,int* x,struct sockaddr_in addr)
{
    int n;
    if(sendto(sockfd, NULL, 0, 0, (struct sockaddr *) &addr,    sizeof(addr)) < 0)
        err_exit("sendto\n");
    n = recvfrom(sockfd,(char*)x,sizeof(int),0,NULL,NULL);
    if (n < 0) {
        perror("errore in recvfrom");
        exit(1);
    }
    if(n > 0) {
       printf("client received: %d\n",*(int*)x);
    }
    return 1;
}




int main(int argc, char *argv[ ]) {
  int   sockfd;
  struct    sockaddr_in   servaddr;
  int x;

  if (argc != 2) { 
    fprintf(stderr, "utilizzo: daytime_clientUDP <indirizzo IP  server>\n");
    exit(1);
  }

  if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { /* crea il socket   */
      perror("errore in socket");
      exit(1);
    }

    memset((void *)&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);

    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
        err_exit("error in inet_pton for %s");
    }



  if(request_to_server(sockfd,&x,servaddr))
      printf("client received %d from server\n",x);
  exit(EXIT_SUCCESS);
 }

这是服务器代码:

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

#define SERV_PORT   5193
#define MAXLINE     1024



void err_exit(char* str)
{
    perror(str);
    exit(EXIT_FAILURE);
}


int generate_casual()
{
    int x = random()%1000 + 1;      //number between 1 and 1000
    return x;
}



void listen_request(int sockfd,char* buff,struct sockaddr_in* addr,socklen_t* len)
{
    struct sockaddr_in servaddr = *addr;
    socklen_t l = *len;
    printf("listening request\n");
     if ( (recvfrom(sockfd, buff, MAXLINE, 0, (struct sockaddr *)&servaddr, &l)) < 0){
         printf("errno code: %d\n",errno);
         err_exit("recvfrom\n");
    }
     *addr = servaddr;
     *len = l;
     return;
}



int main(int argc, char **argv)
{
  (void) argc;
  (void) argv;
  int sockfd;
  socklen_t len;
  struct sockaddr_in addr;
  char buff[MAXLINE];
  srand(time(NULL));


  memset((void *)&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY); 
  addr.sin_port = htons(SERV_PORT); /* numero di porta del server */


  if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
      err_exit("errore in socket");

   if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
     perror("errore in bind");
     exit(1);
   }

  listen_request(sockfd,buff,&addr,&len);

  int n_ack = generate_casual();
  //char* p =(char*)&n_ack;
  if (sendto(sockfd, (char*)&n_ack, sizeof(int), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
      err_exit("sendto");
  }
  printf("server: client is connected\n");
  return 0;
}

当我跑步时,有时客户收到正确的号码;但有时我在recvfrom上有一个错误,msg“无效参数”ed errno代码是22; 为什么呢?我不知道它在哪种情况下运行,当它不起作用时,代码是相同的..

0 个答案:

没有答案