套接字中的分段错误错误

时间:2011-08-14 20:37:02

标签: c sockets

我有一个非常奇怪的问题。我的代码运行了一段时间,然后我在linux中关闭了我的终端并再次打开它来运行代码,但现在我得到了分段错误。无法解读究竟发生了什么...任何帮助都将不胜感激。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "unistd.h"
#include <syslog.h>
#include <math.h>
#define MAXPROFILES  2

float Pearson(int mySum, int recSum, int multSum);

int main(int argc, char *argv[])
{      
   int sockfd, newsockfd, portno, clilen;
   struct sockaddr_in serv_addr, cli_addr;
   unsigned char buf[1024];

   /*int my_data2[10] = {1,3,9,10};

   int my_data[10] = {0};    //  = {1,2,3,4,5};
   int myDataBinary[500] = {0};
   int myDataBinary2[500] = {0};
   int recData[500] = {0};*/

   int index1=0;
// char* pointer = buf;

   struct profile_t
   {
      unsigned char length;
      unsigned char type;
      unsigned char *data;
   };

   typedef struct profile_datagram_t
   {
      unsigned char *src;
      unsigned char *dst;
      unsigned char ver;
      unsigned char n;
      struct profile_t profiles[MAXPROFILES];
   } header;


  header outObj;

  int j =0;
  int i =0;


  if (argc < 2) {
       fprintf(stderr,"usage: %s port_number1",argv[0]);
       exit(1);
  }

  sockfd = socket(AF_INET, SOCK_STREAM, 0);

  if (sockfd < 0)
    error("ERROR DETECTED !!! Problem in opening socket");

  bzero((char *) &serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(portno);

  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
          error("ERROR DETECTED !!! There was a problem in binding");

  listen(sockfd, 10);
  clilen = sizeof(cli_addr);



  printf("Server listening on port number %d...\n", serv_addr.sin_port);

  while(1){
       newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

       if (newsockfd < 0)
         error("ERROR DETECTED !!! the connection request was not accepted");

       int rc = read(newsockfd,buf,100);
       if(rc < 0){
              printf("error");
       }
       else {
            printf("success %d",rc);
       }


     outObj.src = malloc(4);// ***Editor's comment: try gdb/valgrind next time + examine your memory allocating arguments***
     outObj.dst = malloc(4);
    // printf(pointer);
     memcpy(outObj.src,buf+0,4);
     memcpy(outObj.dst,buf+4,4);
     memcpy(&outObj.ver,buf+8,1);
     memcpy(&outObj.n,buf+9,1);

      printf("\nSource IP = ");
      for(int i=0;i<4;i++){
         printf("%d ",outObj.src[i]);
      }

      printf("\nDestination IP = ");
      for(int i=0;i<4;i++){
         printf("%d ",outObj.dst[i]);
      }

      printf("\nVersion = %d",outObj.ver);
      printf("\nNumber of messages = %d",outObj.n);

      int k = 10;

      for(i=0;i<outObj.n;i++){
         memcpy(&outObj.profiles[i].length,buf+k,1);
         memcpy(&outObj.profiles[i].type,buf+k+1,1);
         outObj.profiles[i].data = malloc(outObj.profiles[i].length);
         memcpy(outObj.profiles[i].data,buf+k+2,5);
         k +=7;
      }

      for(int i=0;i<outObj.n;i++){
          printf("\n------- Message %d --------",i+1);
          printf("\nLength : %d",outObj.profiles[i].length);
          printf("\nType : %d\n",outObj.profiles[i].type);
          for(int j=0;j<5;j++){
              printf(" Data[%d] : %d",j,outObj.profiles[i].data[j]);
          }
      }

     /*
     for(int i=0; i<sizeof(my_data)/sizeof(int);i++)
     {
         if(my_data[i] > 0){
            index1 = my_data[i];
            myDataBinary[index1] = 1;
            printf("my data %d = %d\n",index1,myDataBinary[index1]);
            }
     }

     for(int i=0; i<sizeof(my_data2)/sizeof(int);i++){
         if(my_data2[i] > 0){
            index1 = my_data2[i];
            myDataBinary2[index1] = 1;
            printf("my data %d = %d\n",index1,myDataBinary2[index1]);
          }
      }*/


     float rho;

     for(int i=0;i<outObj.n;i++){
         printf("\n\n---------- Values for Data Profile %d ------------",i+1);
         index1=0;
         int sumRecievedData = 0;
         int sumMyData = 0;
         int sumMultpliedData = 0;
         int my_data[10] = {0};//  = {1,2,3,4,5};
         int myDataBinary[500] = {0};
         int recData[500] = {0};

         if(i==0){
            my_data[0] = 1;
            my_data[1] = 3;
            my_data[2] = 9;
            my_data[3] = 10;
         } 
         else if(i==1){
            my_data[0] = 1;
            my_data[1] = 2;
            my_data[2] = 3;
            my_data[3] = 4;
            my_data[4] = 5;
         }

         for(int i=0; i<sizeof(my_data)/sizeof(int);i++){
             if(my_data[i] > 0){
                index1 = my_data[i];
                myDataBinary[index1] = 1;
                //printf("my data %d = %d\n",index1,myDataBinary[index1]);
             }
         }

         for (int j=0; j<outObj.profiles[i].length;j++) {
              if(outObj.profiles[i].data[j] > 0){
                 index1 = outObj.profiles[i].data[j];
                 recData[index1] = 1;
              }
         }

         for(int i=0;i<500;i++){
             sumRecievedData += recData[i];
             sumMyData += myDataBinary[i];
             sumMultpliedData += recData[i] * myDataBinary[i];
         }

         printf("\nrecSum = %d, \nmySum = %d, \nmultSum = %d\n",sumRecievedData,sumMyData,sumMultpliedData);

         rho = Pearson(sumMyData,sumRecievedData,sumMultpliedData);
         printf("\nPearson Coefficient for Data Profile %d= %f\n",i+1,rho);
   }









    }
    return 0;
   }


  float Pearson(int mySum, int recSum, int multSum){
     float Cov =0;
     float sdMyData = 0;
     float sdRecievedData =0;
     float rho;
     int n = 500;

     Cov = (1.0/(n-1))*(multSum - (1.0/n)*mySum*recSum);
     sdMyData = sqrt((1.0/(n-1))*(mySum - (1.0/n)*mySum*mySum));
     sdRecievedData = sqrt((1.0/(n-1))*(recSum - (1.0/n)*recSum*recSum));
     printf("\nCovariance = %f, \nVarianceMyData = %f, \nVarianceRecData = %f",Cov,sdMyData,sdRecievedData);
     if (sdMyData == 0.0 || sdRecievedData == 0.0){
         rho = 0.0;
     }
     else{
          rho = Cov/(sdMyData*sdRecievedData);
     }

     return(rho);
   }

1 个答案:

答案 0 :(得分:3)

我真的不想发帖回答,但有些事情我想大肆宣传。

首先,请学习使用gdb。在编译gcc -o ex ex.c -g时,查看scene.Add -g选项背后的内容非常方便。调用gdb ./ex如果编译成功。然后在gdb命令界面中键入run [-all sorts of arguments and options you have initially set up in your program],gdb将显示进程/线程调用以及您需要的所有内容。轻松愉快。

其次,在您的代码中,显然您以不正确的方式分配内存。这就是我们在手册页中所拥有的内容:

 #include <stdlib.h>

 void *malloc(size_t size);
 void free(void *ptr);
 void *realloc(void *ptr, size_t size);
 void *calloc(size_t nmemb, size_t size);

请注意,在您的情况下,您必须像header_imgonnause = (header*)malloc(sizeof(header)*memoryiwannahave)一样执行此操作。

第三,可能是最后一个,就此代码中的分段错误而言,您是否已查看过accept() read()write()?如果连接中存在损坏的管道, bash会提示你出错。如果使用gdb,你就会知道是不是这样。

总而言之,请使用gdb / valgrind。