【问题标题】:core dump at client side after bcopy()bcopy() 后客户端的核心转储
【发布时间】:2015-09-22 16:01:33
【问题描述】:

我正在尝试从服务器端的文件 /tmp/k 读取数据并将数据复制到消息中并发送到客户端。在这里,我可以将数据发送到客户端并将其显示在客户端上。但是当我在客户端和服务器中添加标头字段时,客户端显示 core dump 。任何人都可以帮助我解决这个问题,或者可以提出更好的解决方案来在客户端显示数据

谢谢

client.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> 
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <netdb.h>

#define MAX_SIZE 100000
#define NUM_CLIENT 5
void *connection_handler(void *);

int main(int argc , char *argv[])
{
   int sockfd;
   long i;
   pthread_t sniffer_thread;
   printf("memory allocated for the creation of socket \n");
   for (i=1; i<=NUM_CLIENT; i++) {
        if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void *) i) < 0)
    {
        perror("could not create thread");
        return 1;
    } 
    printf("Thread created \n");
    sleep(2); 
} 
pthread_exit(NULL);
return 0;
}
void *connection_handler(void *threadid)
{   
    struct head
    {
       float version;
       int   msg_length;
       int   header_length;
       char  msg_type[50];
    };

    long etid; /*each thread id */
    etid = (long)threadid;
    pthread_t tid;
    tid = pthread_self();
    int sockfd , n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    void *buf;
    buf=(struct head *)malloc((sizeof(struct head)));    
    char sbuff[MAX_SIZE] , rbuff[MAX_SIZE];                             
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
    printf("Failed creating socket\n");
    perror("could not create thread");
    return 0;
    }
    memset(&serv_addr, 0 , sizeof (serv_addr));
    printf("the bytes of memory area pointed by serv_addr is filled with constant '0' \n");
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr,
     server->h_length);
    serv_addr.sin_port = htons(8888);

    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0) {
         perror("Failed to connect to server. Error");
         return 0;
         }
    n = read(sockfd,buf,((sizeof(struct head))));
    printf("The socket contains %d \n ",n);
    if (n < 0) 
     perror("ERROR reading from socket");
     printf("version -->%f \n msg length -->%d \n header length %d \n  msg type--> %s  \n ",*(float*)buf,*(int*)((unsigned int)buf+sizeof(float)),*(int*)((unsigned int)buf+sizeof(float)+sizeof(int)),(char*)((unsigned int)buf+sizeof(float)+3*sizeof(int)));
    printf("Connected successfully client:%ld \n", tid);
    printf("before calling pthread_create getpid: %d getpthread_self: %lu tid:%lu\n",getpid(), pthread_self(), syscall(SYS_gettid)); 
    while(1)
    {
       printf("For thread : %ld\n", etid);
       printf("thread id given by kernel: %ld\n", tid);
       fgets(sbuff, MAX_SIZE , stdin);
       send(sockfd,sbuff,strlen(sbuff),0);

        if(recv(sockfd,rbuff,MAX_SIZE,0)==0)
           printf("Error");
       else
          fputs(rbuff,stdout);
       bzero(rbuff,MAX_SIZE);
       sleep(2);
       } 
       close(sockfd);
       return 0;
}

服务器.c

 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <pthread.h>
 #include <sys/syscall.h>
 #include<pthread.h> //for threading , link with lpthread
 #include <fcntl.h>

 #define PROCESSLIST "/tmp/k"
 struct head
 {
     float version;
     int msg_length;
     int header_length;
     char msg_type[50];
 };
 void *connection_handler(void *);
 int main(int argc , char *argv[])
 {
     int sockfd , new_socket , c , *new_sock;
     struct sockaddr_in serv_addr , client;
     char *message;
     void *msgptr;
     struct head * hdptr;
     int n;
     hdptr=(struct head *)malloc(sizeof(struct head));
     msgptr=malloc((sizeof(struct head)));
     printf("msgptr1 %u  size = %d\n",(unsigned int) msgptr,sizeof(struct head));
     ((struct head *)msgptr)->version=5.01;
     ((struct head *)msgptr)->msg_length=1234;
     ((struct head *)msgptr)->header_length=2;
     strcpy((((struct head *)msgptr)->msg_type),"process list, directory list, OS Name and Version");
     ((struct head *)msgptr)->flag=0;
     printf("intialized \n");
     printf("version -->%f \n msg length -->%d \n header length %d \n  msg type--> %s  \n   ",*((float *)msgptr),*((int *)((unsigned int)msgptr+sizeof(float))),*((int *)((unsigned int)msgptr+2*sizeof(int))),(char *)((unsigned int)msgptr+3*sizeof(int)));

     //Create socket
     sockfd = socket(AF_INET , SOCK_STREAM , 0);
     if (sockfd == -1)
     {
         printf("Could not create socket");
     }

     //Prepare the sockaddr_in structure
     server.sin_family = AF_INET;
     server.sin_addr.s_addr = INADDR_ANY;
     server.sin_port = htons( 8888 );

     //Bind
     if( bind(sockfd,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
       puts("bind failed");
       return 1;
     }
     puts("bind done");

     //Listen
     listen(sockfd , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    while( (new_socket = accept(sockfd, (struct sockaddr *)&client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");

        pthread_t sniffer_thread;
        new_sock = malloc(1);
       *new_sock = new_socket;

      if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
      {
        perror("could not create thread");
        return 1;
       }
    puts("Handler assigned");
   }

   if (new_socket<0)
   {
    perror("accept failed");
    return 1;
    }

    return 0;
}

  /* This will handle connection for each client */
void *connection_handler(void *sockfd)
{
       //Get the socket descriptor
      int sock = *(int*)sockfd;
      int read_size;
      char message[100000], client_message[2000];
      int fd;
      void *buff;
      //Receive a message from client
     while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
     {
        system ("ps -ef > /tmp/k");
        puts("temp file getting created");
        fd = open (PROCESSLIST, O_RDONLY);
        puts("file open has taken place");
        read(fd, buff, sizeof(*buff));
        puts("reading the file");
        write(sock, buff, 1);
        puts("writing to the buffer");
        puts("copied data from buffer to message");
        //Send the message back to client
        send(sock,message,strlen(message),0);
        puts("data has been sent");
        }

       if(read_size == 0)
       {
       puts("Client disconnected");
       fflush(stdout);
       }
       else if(read_size == -1)
       {
          perror("recv failed");
        }

//Free the socket pointer
free(sockfd);

return 0;
}

【问题讨论】:

  • 你不需要cast malloc()。您可以将msgptr 声明为strcut head,如果您返回void *,您甚至不需要强制转换它。这个((struct head *)msgptr)-&gt;version=5.01 完全没有必要。而且您知道,编译器会忽略空格,因此您可以根据需要编写更具可读性的代码。
  • (struct head *)malloc(sizeof(struct head)) -> malloc(sizeof(*hdptr)) 没问题。
  • 您是否从client.c 分配了server 指针指向的connection_handler 中的任何空间?
  • 但问题出在客户端@iharob 服务器运行正常。
  • 一般来说,只要保持一致,* 靠近类型名称,还是在中间或靠近标识符? Y 有一个组合。保持一致,否则你会长期后悔。此外,这有助于您快速发现自己的错误。

标签: c sockets unix


【解决方案1】:

主要问题是您在未初始化的指针上使用了间接。

这个

struct hostent *server;

紧随其后

bcopy(server->whatever
/*           ^ this is wrong, because `server' is not initialized */

在你的混乱中很难看到这一点(对不起,我的意思是代码)。您需要更好地组织代码。并检查错误以使其健壮。

这里对你的代码做了一点改进,我也修复了这个问题

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <netdb.h>

#define MAX_SIZE 100000
#define NUM_CLIENT 5
void *connection_handler(void *);

int main(int argc , char *argv[])
{
    long i;
    pthread_t sniffer_thread;

    printf("memory allocated for the creation of socket \n");
    for (i = 1 ; i <= NUM_CLIENT ; i++) /* Be consistent, either break braces or don't */
    {
        if (pthread_create(&sniffer_thread , NULL,  connection_handler, (void *) &i) < 0)
        {
            perror("could not create thread");
            return 1;
        }
        printf("Thread created \n");
        sleep(2);
    }
    pthread_exit(NULL);

    return 0;
}
void *connection_handler(void *threadid)
{
    struct head
    {
        float version;
        int   msg_length;
        int   header_length;
        char  msg_type[50];
    };
    long etid; /*each thread id */
    pthread_t tid;
    int sockfd;
    int n;
    struct sockaddr_in serv_addr;
    struct hostent server;
    /*            ^ you don't need a pointer */
    void *buf;
    char sbuff[MAX_SIZE];
    char rbuff[MAX_SIZE];

    tid = pthread_self();
    etid = *(long *) threadid;
    buf = malloc((sizeof(struct head)));
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("Failed creating socket\n");
        perror("could not create thread");
        return 0;
    }
    memset(&serv_addr, 0 , sizeof (serv_addr));

    printf("the bytes of memory area pointed by serv_addr is filled with constant '0' \n");

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(8888);

    bcopy((char *) server.h_addr, (char *) &serv_addr.sin_addr.s_addr, server.h_length);

    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0)
    {
        perror("Failed to connect to server. Error");
        return 0;
    }

    n = read(sockfd,buf,((sizeof(struct head))));
    printf("The socket contains %d \n ",n);
    if (n < 0)
        perror("ERROR reading from socket");
    printf(
        "version -->%f \n msg length -->%d \n header length %d \n  msg type--> %s  \n ",
        *(float *) buf,
        *(int *) ((unsigned int *) buf + sizeof(float)),
        *(int *) ((unsigned int *) buf + sizeof(float) + sizeof(int)),
        (char *) ((unsigned int *) buf + sizeof(float) + 3 * sizeof(int))
         /*                    ^ you need to cast to a pointer type */
    );

    printf("Connected successfully client:%ld \n", tid);
    printf("before calling pthread_create getpid: %d getpthread_self: %lu tid:%lu\n",
        getpid(), pthread_self(), syscall(SYS_gettid));

    while (1)
    {
        printf("For thread : %ld\n", etid);
        printf("thread id given by kernel: %ld\n", tid);

        fgets(sbuff, MAX_SIZE , stdin);
        send(sockfd,sbuff,strlen(sbuff),0);

        if(recv(sockfd,rbuff,MAX_SIZE,0)==0)
            printf("Error");
        else
            fputs(rbuff,stdout);
        bzero(rbuff,MAX_SIZE);
        sleep(2);
    }
    close(sockfd);
    return 0;
}

server 变量被定义为一个指针,但在您取消引用它时它还没有指向任何东西。

由于它是未初始化的,当你取消引用它时行为是未定义的。另外,在投射指针时要小心,这不是微不足道的,而且你已经犯过这样的错误

*(int *) ((unsigned int) buf + sizeof(float) + sizeof(int))

您正在将buf 转换为整数,并且转换可能会截断指针,例如在 x86_64 平台上。启用编译器警告后,编译器会警告指向整数转换的指针。

【讨论】:

  • @iharob 是的,编译器已显示警告,但我只是忽略了它们,因为我不知道该怎么做。
  • 不要忽视警告,而是从他们那里学习语言规则。如果它警告严格别名违规,请不要忽略 -> Google“严格别名违规”。以此类推。
  • @iharob 即使在您编辑之后,我再次面临核心转储的相同问题。
  • @nandan 使用调试器并检查它到底发生在哪里。
  • 它恰好发生在客户端的 bcopy() 行。
猜你喜欢
  • 2016-05-10
  • 2018-04-07
  • 2018-02-28
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多