ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have compiled the below code successfully,
I have download this code from a site.
This code is related to client-server socket.
But when I run it, I got segmentation error.
Please anyone help.
Thanks
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc, char **argv)
{
int sockfd,newsockfd;
int c;
char buf[100];
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(atoi(argv[1]));
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(sockfd,5);//5 standsfor the max connections it can take.in linux 5 is for 8 connections.
c=sizeof(cliaddr);
newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&c);
if(read(newsockfd,buf,100)<0)
printf("Error Recv Data\n");
else
printf("Server: Data Recvd\n");
if(write(newsockfd,"Hello!!!",100)<0)
printf("Error Sending Data\n");
else
printf("Server: Replying\n");
return 0;
}
Last edited by colucix; 08-24-2011 at 09:49 AM.
Reason: Added code tags
I'm assuming it happens at read or write and that accept fails because listen does, which fails because bind does. What port number are you using? Please rewrite your code to check the return values of socket, bind, listen, and accept. If any are -1, print out strerror(errno) and exit from the program. If you still get a segfault, compile with -g and run it with gdb.
Kevin Barry
If you are running your application as a regular user, your port number must fall somewhere between 1024 and 65535. If you are running as root, then port numbers less than 1024 are available.
Check if bind() fails; it is does, it could indicate that the port you have chosen is already in use!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.