Thread: getting client info

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    getting client info

    I wish to get the IP address of a client so that I can connect a socket for the client to send data to the server. This is the code for the server program:

    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main (int argc, char **argv)
    {
     WSADATA WsaDat;
     SOCKET ServSocket, ClientSocket, TempSocket = SOCKET_ERROR;
     SOCKADDR_IN ServAddr, ClientAddr;
     SOCKADDR PeerAddr;
     char recv_str[6];
     int RetVal = SOCKET_ERROR, paddr_sz;
    
     if (WSAStartup (MAKEWORD (1, 1), &WsaDat) != 0)
     {
      cout << "Error in WSAStartup!!!";
      return -1;
     }
     
     ServSocket = socket(AF_INET, SOCK_STREAM, 0);
     if (ServSocket == INVALID_SOCKET)
     {
      cout << "Error making socket!!!";
      return -1;
     }
     ClientSocket = socket(AF_INET, SOCK_STREAM, 0);
     if (ClientSocket == INVALID_SOCKET)
     {
      cout << "Error making client socket!!!";
      return -1;
     }
    
     ServAddr.sin_port = htons(100);
     ServAddr.sin_family = AF_INET;
     ServAddr.sin_addr.s_addr = inet_addr("192.168.1.100");
     
     if (bind (ServSocket, (SOCKADDR *)(&ServAddr), sizeof (ServAddr)) == SOCKET_ERROR)
     {
      cout << "Error binding socket!!!";
      return -1;
     }
     
     listen (ServSocket, 1);
     paddr_sz = sizeof(SOCKADDR);
    
     while (TempSocket == SOCKET_ERROR)
     {
      TempSocket = accept (ServSocket, &PeerAddr, &paddr_sz);
     }
     ServSocket = TempSocket;
    
     cout << "Made it this far!!!";
    
     send(ServSocket, "Hello", 6, 0);
    
     cout << inet_addr(PeerAddr.sa_data);
    
     if (connect (ClientSocket, &PeerAddr, sizeof (PeerAddr)) != 0)
     {
      cout << "Error connecting to client!!!";
      system ("pause");
      return -1;
     }
     
     while (RetVal == SOCKET_ERROR)
     {
      RetVal = recv (ClientSocket, recv_str, 6, 0);
     }
     cout << recv_str;
    
     system ("pause");
     return 0;
    }
    As you can see, I'm trying to return the address in a SOCKADDR structure. I have no idea why this is going wrong. This is more or less just a project to help me learn how to program with winsock, so im not worried about checks on every problem like is port 100 always open. i know that these ports may be in use, but this is just to learn. if anyone has any advice on why i cant seem to get the IP address, please help!! Thanks in advance!!! if you need the client code, I'll be more than happy to post it!!!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    I must admit im a bit confused about the logic of your code. Are you just trying to print the IP address of a client that you have accepted a connection to? Or get the Ip address of the client so you can connect to it to start communication?

    I have only had a quick look at the code so i could have interpreted it wrong but it seems to me that the server address is binded to a port on your local ip address (you should use INADDR_ANY by the way rather than typing your ip address manually) and waits for the client to connect to it which it then accepts. The server then seems to be trying to connect to the client who just connected to your server. Which is incorrect because once the server accepted the client then communciation has already been made and you dont need to connect to the client.

    The way the server should be programmed is:

    1. Create the socket
    2. Bind the socket
    3. Listen on the socket
    4. Accept the connection made to the socket
    5. Stat sending and receiving data
    6. Close the socket

    Code:
     WSADATA WsaDat;
     SOCKET ServSocket, ClientSocket  = SOCKET_ERROR;
     SOCKADDR_IN ServAddr, ClientAddr;
    
    char recv_str[6];
     int RetVal = SOCKET_ERROR, paddr_sz;
    
    if (WSAStartup (MAKEWORD (1, 1), &WsaDat) != 0)
     {
      cout << "Error in WSAStartup!!!";
      return -1;
     }
    
    ServSocket = socket(AF_INET, SOCK_STREAM, 0);
    
    ServAddr.sin_port = htons(100);
     ServAddr.sin_family = AF_INET;
     ServAddr.sin_addr.s_addr = INADDR_ANY;
     
     if (bind (ServSocket, (SOCKADDR *)(&ServAddr), sizeof (ServAddr)) == SOCKET_ERROR)
     {
      cout << "Error binding socket!!!";
      return -1;
     }
    
    listen (ServSocket, 1);
     paddr_sz = sizeof(ClientAddr);
    
     while (ClientSocket== SOCKET_ERROR)
     {
      ClientSocket= accept (ServSocket, (struct sockaddr *)&ClientAddr, &paddr_sz);
     }
    send(ClientSocket, "Hello", 6, 0);
    
    recv (ClientSocket, recv_str, 6, 0);
    
    // if you want to print the ip do the following
    inet_ntoa(ClientAddr.sin_addr));
    
    close(ClientSocket) ;
    close(ServerSocket) ;
    I havent compiled the above code, i just copied and pasted from your post.

    Like i said i may of completely misunderstood your post but if not then the above should be what you're looking for.

    Regards
    Last edited by Codeslinger; 12-24-2007 at 07:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  2. WSAAsyncSelect Socket Model. She's Not Hot.
    By Tonto in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-24-2007, 08:34 AM
  3. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  4. Getting info on a client when they connect to the server
    By Finchie_88 in forum Networking/Device Communication
    Replies: 4
    Last Post: 06-01-2005, 07:12 AM
  5. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM