UDP

时间:2015-11-22 04:35:37

标签: c sockets segmentation-fault udp sendto

当我在UDP中编写客户端 - 服务器项目时,我遇到了分段故障问题。它发生在服务器端,当我从客户端收到数据包并发送回ACK时。我试图搜索解决方案并得到UDP Server giving Segmentation FaultC concurrent UDP socket , weird segmentation fault,但似乎这些都不是我正在寻找的答案。

这是我的服务器端代码

#include <ctype.h>          /* for toupper */
#include <stdio.h>          /* for standard I/O functions */
#include <stdlib.h>         /* for exit */
#include <string.h>         /* for memset */
#include <sys/socket.h>     /* for socket, sendto, and recvfrom */
#include <netinet/in.h>     /* for sockaddr_in */
#include <unistd.h>         /* for close */

#define STRING_SIZE 1024

#define SERV_UDP_PORT 12311



int main(void) {

   int sock_server; 

   struct sockaddr_in server_addr;  

   unsigned short server_port;  

   struct sockaddr_in client_addr; 

   unsigned int client_addr_len;  

   char sentence[STRING_SIZE];  

   char modifiedSentence[STRING_SIZE]; 

   unsigned int msg_len;  

   int bytes_sent, bytes_recd;

   unsigned int i;  

   struct Pkt
   {
     short length;
     short seqnum;
     char databytes[80];
   }; 
   struct Pkt* pkt;

    int  j ;      //for loop
    int seq;
    short num_of_bytes;
    //char ack_num[2];


   struct Ack
   {
    short ack_num;
   };
   struct Ack* ack;




   /* open a socket */

   if ((sock_server = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
      perror("Server: can't open datagram socket\n");
      exit(1);
   }

   /* initialize server address information */

   memset(&server_addr, 0, sizeof(server_addr));
   server_addr.sin_family = AF_INET;
   server_addr.sin_addr.s_addr = htonl (INADDR_ANY);  
   server_port = SERV_UDP_PORT; 
   server_addr.sin_port = htons(server_port);

   /* bind the socket to the local server port */

   if (bind(sock_server, (struct sockaddr *) &server_addr,
                                    sizeof (server_addr)) < 0) {
      perror("Server: can't bind to local address\n");
      close(sock_server);
      exit(1);
   }

   /* wait for incoming messages in an indefinite loop */

   printf("Waiting for incoming messages on port %hu\n\n", 
                           server_port);

   client_addr_len = sizeof (client_addr);

   for (;;) {

      bytes_recd = recvfrom(sock_server, pkt, sizeof(*pkt), 0, (struct sockaddr *) &client_addr, &client_addr_len);

      ack->ack_num = pkt->seqnum;
      printf("%02d\n", ack->ack_num);
      num_of_bytes = pkt->length;
      printf("The sequence number is: %d\n", ack->ack_num);
      printf("Received Sentence is: %s\n     with length %d\n\n", pkt->databytes, num_of_bytes);
      msg_len = 3;


      /* send message */

      bytes_sent = sendto(sock_server, (struct Ack*)&ack, msg_len, 0, (struct sockaddr*) &client_addr, client_addr_len);      //Here is the segmentation fault comes from

   }
}

我不擅长C,所以如果代码很傻,请原谅我。 请指出任何错误或只是看起来很奇怪。 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

pkt是指向Pkt的指针,但您尚未对其进行初始化。与ack同样如此。您可以a)malloc一个Pkt并将结果分配给pkt,或b)将pkt更改为Pkt结构(而不​​是指针)。第二个选项看起来像:

struct Pkt pkt;
struct Ack ack;

bytes_recd = recvfrom(sock_server, &pkt, sizeof(pkt), 0, (struct sockaddr *) &client_addr, &client_addr_len);

ack.ack_num = pkt.seqnum;
printf("%02d\n", ack.ack_num);
num_of_bytes = pkt.length;
printf("The sequence number is: %d\n", ack.ack_num);
printf("Received Sentence is: %s\n     with length %d\n\n", pkt.databytes, num_of_bytes);

/* send message */

bytes_sent = sendto(sock_server, &ack, sizeof(ack), 0, (struct sockaddr*) &client_addr, client_addr_len);