在BCOPY后客户端核心转储() [英] core dump at client side after bcopy()

查看:146
本文介绍了在BCOPY后客户端核心转储()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取从服务器端的文件/ tmp / k的数据和将数据复制到该消息并发送至客户端。在这里,我能够将数据发送到客户端和客户端上显示。但是,当我在客户端和服务器将头字段,客户端显示的核心转储。可谁能帮助我的问题还是可以提出更好的解决方案在客户端显示的数据。

感谢

client.c

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>
#包括LT&; ARPA / inet.h>
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&pthreads.h中GT;
#包括LT&; SYS / syscall.h>
#包括LT&;&netdb.h中GT;#定义MAX_SIZE 100000
#定义NUM_CLIENT 5
无效* connection_handler(无效*);INT主(INT ARGC,CHAR *的argv [])
{
   INT的sockfd;
   我长;
   的pthread_t sniffer_thread;
   的printf(内存分配给创造插座的\\ n);
   对于(i = 1; I< = NUM​​_CLIENT;我++){
        如果(在pthread_create(安培; sniffer_thread,NULL connection_handler,(无效*)ⅰ)℃的)
    {
        PERROR(无法创建线程);
        返回1;
    }
    的printf(线程创建\\ n);
    睡眠(2);
}
了pthread_exit(NULL);
返回0;
}
无效* connection_handler(void *的主题ID)
{
    头结构
    {
       浮版本;
       INT msg_length;
       INT header_length;
       焦炭MSG_TYPE [50];
    };    长ETID; / *每个线程ID * /
    ETID =(长)主题ID;
    TID的pthread_t;
    TID = pthread_self();
    INT的sockfd,N;
    结构SOCKADDR_IN serv_addr;
    结构hostent *服务器;
    无效* BUF;
    BUF =(头结构*)malloc的((的sizeof(结构头)));
    烧焦sbuff [MAX_SIZE],rbuff [MAX_SIZE];
    如果((的sockfd =插座(AF_INET,SOCK_STREAM,0))小于0){
    的printf(无法创建套接字\\ n);
    PERROR(无法创建线程);
    返回0;
    }
    memset的(安培; serv_addr,0,sizeof的(serv_addr));
    输出(存储区域的由serv_addr指向的字节填充有恒定'0的'\\ n);
    serv_addr.sin_family = AF_INET;
    BCOPY((字符*)服务器 - > h_addr,
     (字符*)及serv_addr.sin_addr.s_addr,
     服务器 - >长度h_length);
    serv_addr.sin_port = htons(8888);    如果(连接(的sockfd,(结构sockaddr *)及serv_addr,sizeof的(serv_addr))小于0){
         PERROR(无法连接到服务器时出现错误。);
         返回0;
         }
    N =读取(的sockfd,BUF,((的sizeof(结构头))));
    的printf(套接字包含%d个\\ N,N);
    如果(正℃,)
     PERROR(错误从套接字读取);
     的printf(版本 - >%F \\ n信息长度 - >%d个\\ n头长度%d个\\ n味精型 - >%S \\ n,*(浮点*)BUF,*(INT *)((无符号整数)BUF +的sizeof(浮动)),*(INT *)((无符号整数)BUF +的sizeof(浮动)+的sizeof(INT)),(字符*)((unsigned int类型)BUF +的sizeof (浮点)+ 3 *的sizeof(int)的));
    的printf(连接成功的客户端:%LD \\ N,TID);
    的printf(调用pthread_create的GETPID前:鲁TID%:%d个getpthread_self%lu个\\ N,GETPID()pthread_self()系统调用(SYS_gettid));
    而(1)
    {
       的printf(螺纹:%ld个\\ N,ETID);
       的printf(线程ID由内核给出:%ld个\\ N,TID);
       与fgets(sbuff,MAX_SIZE,标准输入);
       送(的sockfd,sbuff,strlen的(sbuff),0);        如果(的recv(的sockfd,rbuff,MAX_SIZE,0)== 0)
           的printf(错误);
       其他
          的fputs(rbuff,标准输出);
       bzero(rbuff,MAX_SIZE);
       睡眠(2);
       }
       接近(的sockfd);
       返回0;
}

server.c

 的#include<&stdio.h中GT;
 #包括LT&;&string.h中GT;
 #包括LT&; SYS / types.h中>
 #包括LT&; SYS / socket.h中>
 #包括LT&; netinet / in.h中>
 #包括LT&; ARPA / inet.h>
 #包括LT&;&stdlib.h中GT;
 #包括LT&;&unistd.h中GT;
 #包括LT&;&pthreads.h中GT;
 #包括LT&; SYS / syscall.h>
 #包括LT&;&pthreads.h中GT; //为线程,链接与lpthread
 #包括LT&;&fcntl.h GT; #定义PROCESSLIST的/ tmp / K
 头结构
 {
     浮版本;
     INT msg_length;
     INT header_length;
     焦炭MSG_TYPE [50];
 };
 无效* connection_handler(无效*);
 INT主(INT ARGC,CHAR *的argv [])
 {
     INT的sockfd,new_socket,C,* new_sock;
     结构SOCKADDR_IN serv_addr,客户端;
     字符*消息;
     无效* msgptr;
     结构头* hdptr;
     INT N;
     hdptr =(头结构*)malloc的(的sizeof(结构头));
     msgptr =的malloc((的sizeof(结构头)));
     的printf(msgptr1%U大小=%d个\\ N(无符号整数)msgptr,sizeof的(结构头));
     ((头结构*)msgptr) - GT;版本= 5.01;
     ((头结构*)msgptr) - GT; msg_length = 1234;
     ((头结构*)msgptr) - GT; header_length = 2;
     的strcpy((((头结构*)msgptr) - GT; MSG_TYPE),进程列表,目录列表,操作系统的名称和版本);
     ((头结构*)msgptr) - GT;旗= 0;
     的printf(intialized \\ n);
     的printf(版本 - >%F \\ n信息长度 - >%d个\\ n头长度%d个\\ n味精型 - >%S \\ n,*((浮点*)msgptr),* (为(int *)((unsigned int类型)msgptr +的sizeof(浮动)))*((INT *)((unsigned int类型)msgptr + 2 * sizeof的(INT))),(字符*)((无符号整数) msgptr + 3 *的sizeof(int)的));     //创建插座
     的sockfd =插座(AF_INET,SOCK_STREAM,0);
     如果(的sockfd == -1)
     {
         的printf(无法创建套接字);
     }     // prepare的SOCKADDR_IN结构
     server.sin_family = AF_INET;
     server.sin_addr.s_addr = INADDR_ANY;
     server.sin_port = htons(8888);     //绑定
     如果(绑定(的sockfd,(结构sockaddr *)及服务器的sizeof(服务器))小于0)
    {
       看跌期权(绑定失败);
       返回1;
     }
     看跌期权(绑定完成);     //听
     听(的sockfd,3);    //接受并传入的连接
    看跌期权(等待进入连接...);
    C = sizeof的(结构SOCKADDR_IN);
    而((new_socket =接受(的sockfd,(结构sockaddr *)及客户端,(socklen_t的*)及C)))
    {
        看跌期权(连接已接受);        的pthread_t sniffer_thread;
        new_sock = malloc的(1);
       * new_sock = new_socket;      如果(在pthread_create(安培; sniffer_thread,NULL connection_handler,(无效*)new_sock)℃的)
      {
        PERROR(无法创建线程);
        返回1;
       }
    看跌期权(处理器分配);
   }   如果(new_socket℃,)
   {
    PERROR(接受失败);
    返回1;
    }    返回0;
}  / *这将处理每个客户端连接* /
无效* connection_handler(无效*的sockfd)
{
       //获取套接字描述符
      INT袜子= *为(int *)的sockfd;
      INT read_size;
      字符信息[100000],client_message [2000];
      INT的fd;
      无效* BUFF;
      //接收来自客户机的消息
     而((read_size =的recv(袜子,client_message,2000,0))0)
     {
        系统(PS -ef>的/ tmp / K);
        看跌期权(获得创建临时文件);
        FD =开(PROCESSLIST,O_RDONLY);
        看跌期权(文件打开已经发生);
        阅读(FD,浅黄色,sizeof的(* BUFF));
        看跌期权(读文件);
        写(袜子,浅黄色,1);
        看跌期权(写入缓冲区);
        看跌期权(复制从缓冲区消息数据);
        //发送消息给客户端
        发送(袜子,消息,strlen的(消息),0);
        卖出期权(数据已被发送);
        }       如果(read_size == 0)
       {
       看跌期权(客户端断开连接);
       fflush(标准输出);
       }
       否则,如果(read_size == -1)
       {
          PERROR(recv的失败);
        }//自由插座指针
免费(的sockfd);返回0;
}


解决方案

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

 结构hostent *服务器;

几乎紧接着

  BCOPY(服务器 - >凡是
/ * ^这是错误的,因为'服务器'没有初始化* /

这真的很难看到,在你的乱七八糟(对不起,我的意思是,code)。你需要组织你的code更好。并检查错误,使其健壮。

下面是你的code略有好转,我也解决了该问题。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>
#包括LT&; ARPA / inet.h>
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&pthreads.h中GT;
#包括LT&; SYS / syscall.h>
#包括LT&;&netdb.h中GT;#定义MAX_SIZE 100000
#定义NUM_CLIENT 5
无效* connection_handler(无效*);INT主(INT ARGC,CHAR *的argv [])
{
    我长;
    的pthread_t sniffer_thread;    的printf(内存分配给创造插座的\\ n);
    对于(i = 1; I< = NUM​​_CLIENT;我++)/ *是一致的,无论是突破大括号或不* /
    {
        如果(在pthread_create(安培; sniffer_thread,NULL connection_handler,(无效*)及ⅰ)℃的)
        {
            PERROR(无法创建线程);
            返回1;
        }
        的printf(线程创建\\ n);
        睡眠(2);
    }
    了pthread_exit(NULL);    返回0;
}
无效* connection_handler(void *的主题ID)
{
    头结构
    {
        浮版本;
        INT msg_length;
        INT header_length;
        焦炭MSG_TYPE [50];
    };
    长ETID; / *每个线程ID * /
    TID的pthread_t;
    INT的sockfd;
    INT N;
    结构SOCKADDR_IN serv_addr;
    结构hostent服务器;
    / * ^你不需要一个指针* /
    无效* BUF;
    焦炭sbuff [MAX_SIZE];
    烧焦rbuff [MAX_SIZE];    TID = pthread_self();
    ETID = *(*长)主题ID;
    BUF =的malloc((的sizeof(结构头)));
    如果((的sockfd =插座(AF_INET,SOCK_STREAM,0))小于0)
    {
        的printf(无法创建套接字\\ n);
        PERROR(无法创建线程);
        返回0;
    }
    memset的(安培; serv_addr,0,sizeof的(serv_addr));    输出(存储区域的由serv_addr指向的字节填充有恒定'0的'\\ n);    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(8888);    BCOPY((字符*)server.h_addr,(字符*)及serv_addr.sin_addr.s_addr,server.h_length);    如果(连接(的sockfd,(结构sockaddr *)及serv_addr,sizeof的(serv_addr))小于0)
    {
        PERROR(无法连接到服务器时出现错误。);
        返回0;
    }    N =读取(的sockfd,BUF,((的sizeof(结构头))));
    的printf(套接字包含%d个\\ N,N);
    如果(正℃,)
        PERROR(错误从套接字读取);
    的printf(
        的版本 - >%F \\ n信息长度 - >%d个\\ n头长度%d个\\ n型味精 - >%S \\ n,
        *(*浮动)BUF,
        *为(int *)((unsigned int类型*)BUF +的sizeof(浮动))
        *为(int *)((unsigned int类型*)BUF +的sizeof(浮动)+的sizeof(INT))
        (字符*)((unsigned int类型*)BUF +的sizeof(浮动)+ 3 * sizeof的(INT))
         / * ^你需要转换为指针类型* /
    );    的printf(连接成功的客户端:%LD \\ N,TID);
    的printf(调用pthread_create的前GETPID:%d个getpthread_self:%lu个TID:鲁%\\ n
        GETPID()pthread_self()系统调用(SYS_gettid));    而(1)
    {
        的printf(螺纹:%ld个\\ N,ETID);
        的printf(线程ID由内核给出:%ld个\\ N,TID);        与fgets(sbuff,MAX_SIZE,标准输入);
        送(的sockfd,sbuff,strlen的(sbuff),0);        如果(的recv(的sockfd,rbuff,MAX_SIZE,0)== 0)
            的printf(错误);
        其他
            的fputs(rbuff,标准输出);
        bzero(rbuff,MAX_SIZE);
        睡眠(2);
    }
    接近(的sockfd);
    返回0;
}

服务器变量delcared为指针,但在当时,你解引用它没有指向任何东西。

由于它是未初始化的行为是不确定的,当你取消对它的引用。此外,你应该投三分球时一定要小心,这不是小事,你已经有过这样的错误

  *为(int *)((无符号整数)BUF +的sizeof(浮动)+的sizeof(INT))

您是铸造 BUF 整数,并有机会,转换截断例如在x86_64平台上的指针。随着编译器警告启用,编译器会发出警告指向整数的转换。

I am trying to read the data from the file /tmp/k on the server side and copying the data to the message and sending to the client side. Here i am able to send the data to the client and display it on the client. But when i am adding header fields in the client and server, client is showing core dump .Can anyone help me with the problem or can propose a better solution to display the data at the client side

Thanks

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;
}

server.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;
}

解决方案

The main problem is you have used indirection on an uninitialized pointer.

This

struct hostent *server;

is almost immediately followed by

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

It's really hard to see that in your mess (sorry I mean, code). You need to organize your code better. And check for errors to make it robust.

Here is a slight improvement on your code, and I also fixed the issue

#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;
}

The server variable is delcared as a pointer, but at the time you dereference it it's not yet pointing to anything.

Since it's uninitialized the behavior is undefined when you dereference it. Also, you should be careful when casting pointers, it's not trivial and you already had mistakes like this

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

You are casting buf to an integer and there is a chance that the conversion truncates the pointer for example on x86_64 platforms. With compiler warnings enabled the compiler would warn about pointer to integer conversion.

这篇关于在BCOPY后客户端核心转储()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆