Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation Fault in Socket Programming
    primarykey
    data
    text
    <p>I am trying to write a simple server-client communication program on C. But I keep getting the error on the server side as soon as a client tries to connect to the server :</p> <p>Segmentation Fault</p> <p>The Server side code is : </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #include&lt;sys/socket.h&gt; #include&lt;sys/types.h&gt; #include&lt;netinet/in.h&gt; void error(char *); int main(int argc, char *argv[]) { int sockfd, newsockfd, n, serv_size, cli_size, port_no, bindfd, readfd, writefd, listenfd; char sbuffer[256]; char *p; p = &amp;sbuffer[0]; struct sockaddr_in serv_addr, cli_addr; if (argc &lt; 2) error("No port no. Specified!\n"); //creating sever side socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) error("Server side listening Socket could not be created!\n"); bzero((char *)&amp;serv_addr, sizeof(serv_addr)); port_no = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port_no); serv_addr.sin_addr.s_addr = INADDR_ANY; //binding socket bindfd = bind(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)); if (bindfd == -1) error("Failed to bind server side socket!\n"); //listening for incoming connections listenfd = listen(sockfd, 5); if (listenfd == -1) error("Failed to lisen to incoming connections!\n"); serv_size = sizeof(serv_addr); cli_size = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *)&amp;cli_addr, &amp;serv_size); if (newsockfd == -1) error("Failed to accept connections from client!\n"); printf("Server received connections from %s \n", (char *)inet_ntoa(cli_addr.sin_addr)); while(1) { bzero(p, sizeof(sbuffer)); readfd = read(newsockfd, p, sizeof(sbuffer)); if (readfd == -1) error("Sorry. Unable to read message from client!\n"); printf("Message Received: "); puts(sbuffer); bzero(p, sizeof(sbuffer)); printf("\nEnter Your Message for client: "); gets(sbuffer); writefd = write(newsockfd, p, sizeof(sbuffer)); if (writefd == -1) error("Sorry. Message Could not be sent to client.\n"); printf("Congratulations! Message has been sent to client.\n"); } return 0; } void error(char *msg) { perror(msg); exit(1); } </code></pre> <p>The Client side code is : </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #include&lt;sys/socket.h&gt; #include&lt;sys/types.h&gt; #include&lt;netinet/in.h&gt; #include&lt;netdb.h&gt; void error(char *); int main(int argc, char *argv[]) { int sockfd, newsockfd, port_no, n, connectfd, readfd, writefd; char cbuffer[256]; char *ptr = &amp;cbuffer[0]; struct sockaddr_in serv_addr; struct hostent *he; if (argc != 3) error("Incomplete arguments!\n"); port_no = atoi(argv[2]); he = gethostbyname(argv[1]); if (he == NULL) error("No Such Host!\n"); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd == -1) error("Sorry. Socket could not be created!\n"); bzero((char *)&amp;serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port_no); serv_addr.sin_addr = *((struct in_addr *)he-&gt;h_addr); connectfd = connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)); if (connectfd == -1) error("Sorry. Could not connect to server.\n"); while(1) { bzero(ptr, sizeof(cbuffer)); printf("\nEnter message for Server: \n"); gets(cbuffer); writefd = write(sockfd, ptr, sizeof(cbuffer)); if (writefd == -1) error("Sorry. Could not send message to Server."); printf("Congratulations! Server Successfully Received your message.\n"); bzero(ptr, sizeof(cbuffer)); readfd = read(sockfd, ptr, sizeof(cbuffer)); if (readfd == -1) error("Sorry. Message from server could not be read.\n"); printf("Message Received: "); puts(cbuffer); } return 0; } void error(char *msg) { perror(msg); exit(1); } </code></pre> <p>This is my compile time warning while compiling serv.c: </p> <pre><code>cc -o s serv.c serv.c: In function ‘main’: serv.c:47:52: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] </code></pre> <p>P.S. : I was using Ubuntu 12.04 where I actually wrote these programs and successfully executed the programs. But i recently migrated to Linux Mint 13 Cinnamon. I recompiled the programs. Probably they successfully executed on Mint too. But now it seems to have caught this bug somehow. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload