2

I am trying to create a client server application in linux. Server sends object to client but client write Segmentation fault. Here is the code. Snake is class where is two-dimensional array.

Client send message "p" and server is creating object witch will be send to client.

Server side:

  int main(int argc, char *argv[])
  {

int sockfd, newsockfd;
socklen_t cli_len;
struct sockaddr_in serv_addr, cli_addr;
int n;
char buffer[256];


if (argc < 2) 
{
    fprintf(stderr,"usage %s port\n", argv[0]);
    return 1;
}

bzero((char*)&serv_addr, sizeof(serv_addr)); 
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(atoi(argv[1]));

sockfd = socket(AF_INET, SOCK_STREAM, 0); 
if (sockfd < 0)
{
    perror("Error creating socket");
    return 1;
}

if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) 
{
     perror("Error binding socket address");
     return 2;
}
while(sockfd){
listen(sockfd, 5); 
cli_len = sizeof(cli_addr);

newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &cli_len); 

if (newsockfd < 0)
{
    perror("ERROR on accept");
    return 3;
}

bzero(buffer,256);
n = read(newsockfd, buffer, 255); 
if (n < 0)
{
    perror("Error reading from socket");
    return 4;
}


// if client send 'p'
if(*buffer == 'p'   ){ 
        Snake *snake = new Snake(); 

    send(newsockfd,reinterpret_cast<const char*>(&snake), sizeof(snake),0);
  }
}



//printf("Here is the message: %s\n", buffer); 

const char* msg = "I got your message";
n = write(newsockfd, msg, strlen(msg)+1);
if (n < 0)
{
    perror("Error writing to socket");
    return 5;
}

close(newsockfd);

close(sockfd);

}

Client side:

 int main(int argc, char *argv[])
 {
int sockfd = 0,newsockfd, n;
struct sockaddr_in serv_addr;
struct hostent* server;
socklen_t cli_len, serv_len;

char buffer[999999];
struct sockaddr_in cli_addr;

if (argc < 3) 
{
    fprintf(stderr,"usage %s hostname port\n", argv[0]);
    return 1;
}

server = gethostbyname(argv[1]); 
if (server == NULL)
{
    fprintf(stderr, "Error, no such host\n");
    return 2;
}

bzero((char*)&serv_addr, sizeof(serv_addr)); 
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(atoi(argv[2]));
//while(sockfd >= 0){
sockfd = socket(AF_INET, SOCK_STREAM, 0); 
if (sockfd < 0)
{
    perror("Error creating socket");
    return 3;
}

if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)  
{
     perror("Error connecting to socket");
     return 4;
}   

printf("Please enter a message: "); 
bzero(buffer,256);
fgets(buffer, 255, stdin); 

n = write(sockfd, buffer, strlen(buffer));  

if (n < 0)
{
     perror("Error writing to socket");
     return 5;
}

 Snake *snake ;

recv(sockfd,reinterpret_cast<char*>(&snake), sizeof(snake),0);

if (n < 0)
{
     perror("Error reading from socket");
     return 6;
}

printf("%s\n",buffer);

close(sockfd);
close(newsockfd);
return 0;
 }  

Thank you for your answers.

Sedrik105
  • 21
  • 1
  • If you send an address of an object in the server process to the client process, this address is not valid in the client process. Each process has its own protected memory. (This is granted by Linux as well as any other modern OS.) The exception would be if server and client would use the same _shared memory_. – Scheff's Cat Dec 04 '17 at 10:59
  • 2
    `char buffer[999999];` this is bad idea, create this structure on heap or as global array. – rafix07 Dec 04 '17 at 11:02
  • Please consider revising the code sample you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you; here is a [great resource](http://stackoverflow.com/help/mcve) to get you started on that. -1, don't take it the wrong way. A down vote is how we indicate a content problem around here; improve your formatting and code sample and I (or someone will) gladly revert it. Good luck with your code! – Clijsters Dec 04 '17 at 11:22
  • @rafix07 of better yet, a smaller buffer; there's no reason for a buffer to be that size when doing network transfers at all. – UKMonkey Dec 04 '17 at 11:48

1 Answers1

4

On the client side

 Snake *snake ;

recv(sockfd,reinterpret_cast<char*>(&snake), sizeof(snake),0);

tries to read into a Snake, but you haven't created any. So the uninitialized pointer gives you the segmentation fault.

Also, sizeof(snake) is the size of a pointer (like 4 bytes), not the size of the object pointed to. This occurs both in the server and in the client.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • I'm not sure whether sending the pointer (`snake`) was actually intended or accidental. OP used explicitly `&snake` though `snake` is already a pointer. "server is creating object witch will be send to client." could mean either or... – Scheff's Cat Dec 04 '17 at 11:08
  • True, I didn't notice the `&`. There might be other problems as well, but this stood out to me. – Bo Persson Dec 04 '17 at 11:13
  • However, this doesn't invalidate your answer. ;-) – Scheff's Cat Dec 04 '17 at 11:13
  • On client side: `recv(sockfd,reinterpret_cast(snake), sizeof(snake),0);` server side: `send(newsockfd,reinterpret_cast(snake), sizeof(snake),0);` client isn't writting Segmentation fault now but on client side isn't same object like on the server side – Sedrik105 Dec 04 '17 at 12:00
  • @Sedrik105 - `sizeof(snake)` is still the size of the pointer, not of the object. That would be `sizeof(Snake)`. – Bo Persson Dec 04 '17 at 12:32
  • @BoPersson - Client is writting Segmentation fault – Sedrik105 Dec 04 '17 at 12:45