server client problem

This is a discussion on server client problem within the Networking/Device Communication forums, part of the General Programming Boards category; hi all, i started learning socket's tried to write a simple client server under unix and got a runtime error ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    server client problem

    hi all,

    i started learning socket's
    tried to write a simple client server under unix
    and got a runtime error

    i run the server and client on to shells (internal network \ localhost \ whatever)
    after sending the message the client (sending) throws this: Illegal instruction (core dumped)
    and the server receiving throws: Segmentation fault (core dumped)

    the server code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    
    int main ()
    {
      int socketfd , incomfd, err;
      struct sockaddr_in myip;
      struct sockaddr_in hisip;
      int sin_size;
      int bytes_recv, len=20, backlog=5;
      char buff[40];
      int myport=55555;
    
    socketfd = socket(PF_INET , SOCK_STREAM , 0);
    
    if(socketfd==-1)
    {printf("unable to creat socket!!");
     exit(1);
    }
    
      
    // intilaize myip address structer
      
    myip.sin_family = AF_INET;
    myip.sin_port = htons(myport);
    myip.sin_addr.s_addr =INADDR_ANY; //inet_addr("127.0.0.1"); // this ip address is for the loopback network (internal ip address).
    memset(myip.sin_zero, '\0', sizeof (myip.sin_zero));
    
    
    err=bind ( socketfd , (struct sockaddr *) &myip , sizeof(myip));
    
    if(err==-1)
    {printf("unable to use port... bind()");
     exit(1);
    }    
    
    
    err=listen(socketfd , backlog);
    
    if(err==-1)
    {printf("error listening");
     exit(1);
    }
    
    sin_size = sizeof (hisip);
    incomfd= accept(socketfd , (struct sockaddr *) &hisip , &sin_size);
    
    if(incomfd==-1)
    {printf("error accepting!");
     exit(1);
    }
    
    bytes_recv=recv(incomfd , buff , len , 0);
    
    printf("bytes recived: %d",bytes_recv);
    
    if(bytes_recv==-1)
    {printf("error while recieving");
     exit(1);
    }
    
    close(socketfd);
    
    printf("The message recieved: %s \n", *buff);
    
    return 0;
    }
    the client code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    
    
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    
    int main ()
    {
      int socketfd;
      struct sockaddr_in destip;
      int destport = 55555;
      int err ,sent_bytes, len;
      char *msg;
    
    socketfd = socket( PF_INET , SOCK_STREAM , 0);
    
    if(socketfd==-1)
    {printf("couldn't creat socket!!");
     exit(1);
    }
    
    
    destip.sin_family = AF_INET;
    destip.sin_port = htons(destport);
    destip.sin_addr.s_addr =INADDR_ANY; //inet_addr("127.0.0.1");
    memset(destip.sin_zero, '\0', sizeof (destip.sin_zero));
    
    err=connect( socketfd , (struct sockaddr *) &destip , sizeof(destip));
    
    if (err==-1)
    {printf("couldn't connect!");
     exit(1);
    }
    
    printf("Enter the message to send: \n");
    gets(msg);
    
    len=strlen(msg);
    printf("The message is %s \n and it is %d charecters long \n",msg , len);
    sent_bytes= send( socketfd , msg , len , 0);
    printf("bytes sent %d\n", sent_bytes);
    close(socketfd);
    
    return 0;
    }
    i really cant figure what am i doing wrong
    please direct me to the write direction

    thanx

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Some remarks (not related to your problem):

    Code:
    myip.sin_addr.s_addr =INADDR_ANY; //inet_addr("127.0.0.1"); // this ip address is for the loopback network (internal ip address).
    The comment is wrong, INADDR_ANY indicates to accept data from any (network) interface,
    which is very different of only the loopback interface.

    You also forget to convert the local encoding to the network encoding:
    Code:
    ...s_addr=htonl(INADDR_ANY);
    or
    Code:
    ...s_addr=htonl(0x7F000001); // localhost only.
    you did it correctly for the port (htons(port)).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Things to fix (responsible for the SEGV):

    server-side:
    Code:
      char buff[40];
      ...
      printf("The message recieved: &#37;s \n", *buff);
    client-side:
    Code:
      char *msg;
      ...
      gets(msg);
    Besides my compiler is nice enough to say:
    "warning: the `gets' function is dangerous and should not be used."

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    2

    thnx

    thank you for your help

    root4:
    my comment is on the command inet_addr("127.0.0.1");
    if this is the case is it still wrong to say its an internal ip address?

    and the reason i didnt convert the INADDR_ANY is because i understood it is actually a zero value but i will add it

    also i add the fix as you said and it works!!
    i thought my problem is in the send\recv
    lol i allways miss the simple things... (shame on me) 8^/

    thank you

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    the reason i didnt convert the INADDR_ANY is because i understood it is actually a zero value
    It is indeed zero, but -despite I was not able to find a concrete reference about that- I think the value of this symbol is implementation-defined as the actual definition of s_addr may vary.
    Omitting the encoding conversion on INADDR_ANY (which may be something else than 0) may lead to unexpected errors/behavior. However if you're sure your implementation uses integer as addresses field, the conversion is indeed useless. Someone else may be able to provide pointers on the subject.

    my comment is on the command inet_addr("127.0.0.1");
    Ok, please ignore my remark on this line.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Couple of other notes:
    Code:
    printf("The message recieved: &#37;s \n", *buff);
    Your format string indicates a string (%s), but you pass a character (*buff). Probably don't want the * there.
    Additionally, you have no guarantee that buff is a string - you should null terminate the data you just recv'd to be sure that it's null terminated. A simple:
    Code:
    buff[bytes_recv] = '\0';
    ...will append a null terminator. Do this before passing it to printf(). Be sure buff is at least bytes_recv + 1 in size (it is in your code, as long as you pass 39 or less to recv().)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Commerical MMORPG Developer Opp
    By Th3Guy in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 01-22-2007, 10:28 AM
  2. client -server problem
    By hegdeshashi in forum C Programming
    Replies: 1
    Last Post: 06-13-2006, 11:13 PM
  3. TCP/IP client & echo server
    By Jordban in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2005, 06:39 PM
  4. Client - Server
    By sCandal2 in forum Windows Programming
    Replies: 5
    Last Post: 11-16-2004, 05:24 AM
  5. Help with Server / Client App
    By Thantos in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-29-2003, 04:47 PM

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