Problem with client server program...

This is a discussion on Problem with client server program... within the C Programming forums, part of the General Programming Boards category; Hi Guys... I am trying to run client server program, but the server program is giving me problem... after the ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Problem with client server program...

    Hi Guys... I am trying to run client server program, but the server program is giving me problem... after the client connects to it.. it says Illegal instruction and logs out... i am relatively new to this.. so please bare with me.. i have done lot of research on net, could not find anything that was useful to me... please bail me out!!!!! :-(

    Server program

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<sys/types.h> 
    #include<errno.h>
    #include<sys/socket.h> 
    #include<netdb.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    #define PORT 6090
    
    
    int main()
    {
           int client_lenght;
           struct sockaddr_in client,server;
           struct hostent *ptrh;
           int sd,sd2;
           char message[50];
           int len;
           memset((char *)&server,0,sizeof(server));
           server.sin_family=AF_INET;
           server.sin_addr.s_addr=INADDR_ANY;
           server.sin_port = htons(PORT);
           sd = socket(AF_INET,SOCK_STREAM,0);
           if(sd<0)
          {
              fprintf(stderr,"cannot create socket\n");
              exit(1);
           }
    
          if(bind(sd,(struct sockaddr *)&server, sizeof(server))<0)
          {
             fprintf(stderr,"bind failed\n");
             exit(1);
           }
    
           listen(sd,5);
          while(1)
         {
           len = sizeof(client);
    
           if((sd2=accept(sd,(struct sockaddr *)&client, &len))<0)
                  {
                       fprintf(stderr,"Accept failed\n");
                       exit(1);
                  }
    
           printf("Waiting for message from client\n");
           recv(sd2,message,sizeof(message),0);
           printf("Meesage is %s\n",message);
           close(sd2);
         }
        return 0;
    }

    Client Program

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<sys/types.h> 
    #include<errno.h>
    #include<sys/socket.h> 
    #include<netdb.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    #define PORT 6090
    
    int main()
    {
         int client_lenght;
         struct sockaddr_in client,server;
         struct hostent *ptrh;
         int sd;
        char message[50];
        memset((char *)&server,0,sizeof(server));
        memset((char *)&client,0,sizeof(client));
        server.sin_family = AF_INET;
    
    
    /*
    ptrh = gethostbyname(host);
    if(((char *)ptrh)==NULL)
    {
    fprintf(stderr,"invalid host: %s\n", host);
    exit(1);
    }
    memcpy(&server.sin_addr,ptrh->h_addr,ptrh->h_length);*/
    
           server.sin_addr.s_addr=INADDR_ANY;
           server.sin_port		= htons(PORT);
           if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	{
    		fprintf(stderr,"Cannot create socket\n");
    		exit(1);
    	}
            if(connect(sd,(struct sockaddr *)&server, sizeof(server))<0)
            {
               fprintf(stderr, "connect failed\n");
               exit(1);
           }
    	printf("Enter the message to be sent to server: ");
    	scanf("%s",&message);
    
            send(sd, message,50,0);
    	fflush(stdout);
    
    	/* Closing the socket*/	
    	close(sd);
    }
    Any help is appreciated........
    Last edited by cprogczar; 04-21-2009 at 08:21 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What says illegal instruction? Are you getting a seg fault? Also, please fix your indentation. Hard to read means no one bothers to read it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Yup.. sometimes it says seg fault.. and sometimes it says bus error...


    [will fix the indentation for next post.. thanks..]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server and client in the same program
    By smithx in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-25-2008, 08:30 PM
  2. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 09:33 AM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  4. Server Client Messaging Program
    By X PaYnE X in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-04-2004, 04:20 PM
  5. Server client program help please.
    By XiReDDeViLiX in forum C++ Programming
    Replies: 15
    Last Post: 04-03-2002, 08:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21