Thread: Hello, new here and need some help please

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Unhappy Hello, new here and need some help please

    Hi guys, am new here so nice to meet u all
    i have a problem with a certain client/server program.
    the client sends a request to the server and asks it to add 100 to 1557. the server receives the request, does the computation and sends back the results as a string message. can any one help me do this please
    Client code
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>    	/* defines structure in_addr */
    #include <netdb.h>	   	/* defines structure hostent */
    #include <stdio.h>
    #include <string.h>
    void main( int argc, char **argv)
    {
    
      #define MAXCHAR 80
    
      int size_message, server_len, s; char client_message[MAXCHAR];
    
      struct sockaddr_in   client_addr, server_addr;
    
      struct hostent *server_ptr;
    
    	/* create a connectionless socket with internet domain */
      s = socket(PF_INET,SOCK_DGRAM,0);
    
      	/* fill in data structure of client address */
      client_addr.sin_family = PF_INET;
    
      	/* accept connection on any internet interface */
      client_addr.sin_addr.s_addr = htonl (INADDR_ANY);
    
      	/* assign arbitrary port number */
      client_addr.sin_port= htons(0);
    
      	/* bind local address to the socket */
      bind(s,(struct sockaddr *)&client_addr,sizeof(client_addr)); 
    
    
    
    /* get address of server and fill in its data structure */
      server_ptr = gethostbyname(argv[1]);
      server_addr.sin_family = PF_INET;
      server_addr.sin_addr= *(struct in_addr *)(server_ptr->h_addr); 
      server_addr.sin_port= htons(7785);
      	/* send server a message */
      strcpy (client_message,"ADD 100 to my Id 1557");
      size_message = strlen(client_message);
      printf("The message sent is &#37;s\n", client_message); 
      printf("The server internet address is %s\n",
    				inet_ntoa(*server_ptr->h_addr_list)); 
      sendto(s,client_message,size_message,0,
                  (struct sockaddr *)&server_addr,sizeof(server_addr));
    }
    Server code
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>    	/* defines structure in_addr */
    #include <netdb.h>	  	/* defines structure hostent */
    #include <stdio.h>
    #include <string.h>
    main()
    {
      #define MAXCHAR 80
      int s;
      struct sockaddr_in    client_addr, server_addr;
      char server_message[MAXCHAR];
      int size_message, client_len;
      	/* create a connectionless socket with internet domain */
      s = socket(PF_INET,SOCK_DGRAM,0);
      	/* fill in data structure of server address */
      server_addr.sin_family = PF_INET;
      server_addr.sin_addr.s_addr = htonl (INADDR_ANY);
      server_addr.sin_port= htons(7785);
      	/* bind local address to the socket */
      bind(s,(struct sockaddr *) &server_addr, sizeof(server_addr)); 
    	while(1) {
    
    /* receive message from client */
      	/* recvfrom blocks until a message arrives */
      	/* it permits to retrieve the source address of message */
      client_len = sizeof (client_addr);
      recvfrom(s,server_message,MAXCHAR,0,
                      (struct sockaddr *)&client_addr,&client_len);
      printf("client address: %s\n", inet_ntoa(client_addr.sin_addr));
      printf("message received from client: %s\n",server_message);
    
    	}
    }

    so pleeeeease can any one help me?? i'll be very grateful

    thanks
    Last edited by Dark1557; 03-30-2008 at 05:20 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So is this a
    - communication problem (the message doesn't arrive)
    - parsing problem (you don't know how to get "100" and "1557" out of the string)
    - a math problem (how to do the addition)

    Or something else?

    Like for example
    - does it compile?
    - using ethereal / wireshark, can you see the message in transit at all?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    thanks 4 the reply
    the communication is ok
    but the computation is a problem
    i dont know how to do these(
    parsing problem (you don't know how to get "100" and "1557" out of the string)
    - a math problem (how to do the addition)
    )
    also to reply to the client with the results
    i hope u can help me and thank u very much

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps sscanf ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    how can i use it with my program??
    can u explain please
    and thank u very much 4 your time

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    int main(void)
    {
    	int a,b;
    	char buf[] = "ADD 100 to my Id 1557";
    	sscanf(buf,"ADD &#37;d to my Id %d", &a,&b);
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    thank u very much

    how should i include it within the program???

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Personally, I think you're missing a hell of a lot of basic information on C programming, and you've just launched yourself into the deep end with some network programming.

    Unless you get this in place, then every small step towards whatever you want to do will be a struggle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    actually, am doing this for a friend.
    am not an expert in c programming (sorry!!)

    however, thank you for your help, and vart too.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you should ask your friend to join the forum and ask the questions directly.

    You might also consider that if you're doing your friends' homework what exactly you're both getting out of this, because learning by doing is far more effective than just reading about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Help?!! Please???

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are trying to become a Formula One mechanic, you probably need to learn how to change the wheels on a regular car first. If you don't understand how to use sscanf() and the code-snippet posted, you probably should be studying the basics first. Network programming is not trivial.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed