10 pts.
 segmentation fault
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); }    

Software/Hardware used:
ASKED: April 30, 2010  5:45 AM
UPDATED: April 30, 2010  2:20 PM

Answer Wiki:
Last Wiki Answer Submitted:  Be the first to answer this question.
All Answer Wiki Contributors:  Be the first to answer this question.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Have you debugged the program ? what’s the line of code causing the error ?

 63,535 pts.