segmentation fault

10 pts.
Tags:
Linux
Segmentation Fault
Socket programming
i am doing socket programming in linux with the help of gcc compiler .the server code is giving error. Segmentation fault (core dumped) the code is: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/socket.h> #include<unistd.h> //#define message 255   int main(void)/*(int argc,char* argv[])*/ { int sock,sock1; struct sockaddr_in server; struct sockaddr_in client; char buffer[255]; char buff[20]={"message recieved"}; int port=6666; unsigned int elen,clen,slen,x;   //if(argc!=2) //{ //fprintf(stderr,"USAGE:%s <port>", argv[0]); //exit(1); //}   sock=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);   if(sock<0)     {     perror("socket not created");     exit(1);     } else     printf("socket created");   memset(&server,0,sizeof(server));   server.sin_family=AF_INET; server.sin_port=htons(port);//(atoi(argv[1]); server.sin_addr.s_addr=inet_addr(INADDR_ANY);   if(bind(sock,(struct sockaddr*)&server,sizeof(server)) < 0)     {     perror("failed to bind server");     exit(1);     } else     printf("server bound");   listen(sock,1);   clen=sizeof(client);   sock1=accept(sock,(struct sockaddr*)&client,&clen);   if(sock1<0)     {     perror("failed to accept");     exit(1);     } else     printf("connection accepted");   memset(&buffer,0,256);   x=read(sock1,buffer,256);   if(x<0)     {     perror("read failed");     exit(1);     } else     printf("reading......");   printf("client says: %s",buffer);   x=write(sock1,buff,20);   if(x<0)     {     perror("failed to write");     exit(1);     } else     printf("written to client");   close(sock1);   exit(0); }    

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    Have you debugged the program ? what's the line of code causing the error ?
    69,510 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following