Thread: Server Client return IP Adress

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Server Client return IP Adress

    Hello, I have an excersise in my university school. The excersise says that we need a server and a client written in C where they communicate and the server returns to client the IP address of client and print it to the screen. The two codes that I found are those:

    Code:
    Server:
    
    /* tcpserver.c */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    
    
    int main()
    {
            int sock, connected, bytes_recieved , true = 1;  
            char send_data [1024] , recv_data[1024];       
    
            struct sockaddr_in server_addr,client_addr;    
            int sin_size;
            
            if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("Socket");
                exit(1);
            }
    
            if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
                perror("Setsockopt");
                exit(1);
            }
            
            server_addr.sin_family = AF_INET;         
            server_addr.sin_port = htons(5000);     
            server_addr.sin_addr.s_addr = INADDR_ANY; 
            bzero(&(server_addr.sin_zero),8); 
    
            if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))
                                                                           == -1) {
                perror("Unable to bind");
                exit(1);
            }
    
            if (listen(sock, 5) == -1) {
                perror("Listen");
                exit(1);
            }
    		
    	printf("\nTCPServer Waiting for client on port 5000");
            fflush(stdout);
    
    
            while(1)
            {  
    
                sin_size = sizeof(struct sockaddr_in);
    
                connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
    
                printf("\n I got a connection from (%s , %d)",
                       inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
    
                while (1)
                {
                  printf("\n SEND (q or Q to quit) : ");
                  gets(&client_addr);
                  
                  if (strcmp(&client_addr , "q") == 0 || strcmp(&client_addr, "Q") == 0)
                  {
                    send(connected, &client_addr ,strlen(&client_addr), 0); 
                    close(connected);
                    break;
                  }
                   
                  else
                     send(connected, &client_addr ,strlen(&client_addr), 0);  
    
                  bytes_recieved = recv(connected,recv_data,1024,0);
    
                  recv_data[bytes_recieved] = '\0';
    
                  if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
                  {
                    close(connected);
                    break;
                  }
    
                  else 
                  printf("\n RECIEVED DATA = %s " , recv_data);
                  fflush(stdout);
                }
            }       
    
          close(sock);
          return 0;
    }
    Code:
    Client:
    
    /* tcpclient.c */
    
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    
    
    int main()
    
    {
    
            int sock, bytes_recieved;  
            char send_data[1024],recv_data[1024];
            struct hostent *host;
            struct sockaddr_in server_addr;  
    
            host = gethostbyname("127.0.0.1");
    
            if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("Socket");
                exit(1);
            }
    
            server_addr.sin_family = AF_INET;     
            server_addr.sin_port = htons(5000);   
            server_addr.sin_addr = *((struct in_addr *)host->h_addr);
            bzero(&(server_addr.sin_zero),8); 
    
            if (connect(sock, (struct sockaddr *)&server_addr,
                        sizeof(struct sockaddr)) == -1) 
            {
                perror("Connect");
                exit(1);
            }
    
            while(1)
            {
            
              bytes_recieved=recv(sock,recv_data,1024,0);
              recv_data[bytes_recieved] = '\0';
     
              if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
              {
               close(sock);
               break;
              }
    
              else
               printf("\nRecieved data = %s " , recv_data);
               
               printf("\nSEND (q or Q to quit) : ");
               gets(send_data);
               
              if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
               send(sock,send_data,strlen(send_data), 0); 
    
              else
              {
               send(sock,send_data,strlen(send_data), 0);   
               close(sock);
               break;
              }
            
            }   
    return 0;
    }
    Thanks in advance.
    Last edited by bigmen2007; 04-28-2010 at 05:25 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Thanks in advance for what?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Thanks in advance for what?
    I need to put into the code some commands so the server will return to client the ip adress of client, but I don't know what are those commands and where to put them.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by tabstop View Post
    Thanks in advance for what?
    Don't look a gift horse in the mouth.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by bigmen2007 View Post
    I need to put into the code some commands so the server will return to client the ip adress of client, but I don't know what are those commands and where to put them.
    Lets see if I got this right. You copy-pasted some code. Now you want us to adjust it to do what you need. And then you will present it for evaluation as something you've done yourself?

    You're either very bold or very stupid.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you see the IP address of client printed out by the program, somewhere?

    Do you see data sent to the client, somewhere?

    Can you combine those two pieces of information?

    Also, if you found this somewhere, be sure to cite your sources, and for goodness' sake, don't use gets.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by msh View Post
    Lets see if I got this right. You copy-pasted some code. Now you want us to adjust it to do what you need. And then you will present it for evaluation as something you've done yourself?

    You're either very bold or very stupid.
    I've tried several combinations without resault. This code I found was given by my professor, so I 've put the original one because I don't want to confuse you with my stupidities.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Do you see the IP address of client printed out by the program, somewhere?

    Do you see data sent to the client, somewhere?

    Can you combine those two pieces of information?

    Also, if you found this somewhere, be sure to cite your sources, and for goodness' sake, don't use gets.
    Yes, server prints to screen the IP adress of client but I don't see data sent to the client. If I type something to server it returns it to client but only manualy. I tried to make server to send the IP to client without any success. Only a type of ip was returned to client like 236.7.195.191. The IP shown on server is 127.0.0.1

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmen2007 View Post
    Yes, server prints to screen the IP adress of client but I don't see data sent to the client. If I type something to server it returns it to client but only manualy.
    Well, yes, but that command that sends will send no matter where you put it in the program....
    Quote Originally Posted by bigmen2007 View Post
    I tried to make server to send the IP to client without any success. Only a type of ip was returned to client like 236.7.195.191. The IP shown on server is 127.0.0.1
    I'm confused here. What's wrong with 236.7.195.191? For that matter, what's right about 127.0.0.1? That's just a loopback address, and even if you're looking for a local network IP is not what you want.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    I'm confused here. What's wrong with 236.7.195.191? For that matter, what's right about 127.0.0.1? That's just a loopback address, and even if you're looking for a local network IP is not what you want.
    So the IP adress shown on the client is right?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you are 236.7.195.191, then sure. That appears to be a reserved-for-multicast address, but I personally have never been really clear on what's going to be a valid address from that group, so I don't know if that's bogus or not. "ifconfig -a" should tell you what your machine thinks its network address is, so that may give you a start.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by tabstop View Post
    If you are 236.7.195.191, then sure. That appears to be a reserved-for-multicast address, but I personally have never been really clear on what's going to be a valid address from that group, so I don't know if that's bogus or not. "ifconfig -a" should tell you what your machine thinks its network address is, so that may give you a start.
    Thanks for all the info until now. I have another question, how can I find that I am this adress and why it changes every time I run the program?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmen2007 View Post
    Thanks for all the info until now. I have another question, how can I find that I am this adress and why it changes every time I run the program?
    "ifconfig -a" should tell you what your machine thinks its address is (assuming you are on a *nix machine). How are you generating the address in your program?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  2. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM