Thread: Getting info on a client when they connect to the server

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Getting info on a client when they connect to the server

    I'm writing some server code at the moment, and was wondering if there was a function that will give me some information on the client connected, like the IP and connection speed they are running at.


  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution to determining the remote socket IP is via getpeername() and getnameinfo().

    Kuphryn
    Last edited by kuphryn; 05-31-2005 at 06:04 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You can also have accept() fill a buffer with the address of the connecting client.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    you can't know the peer's connection speed unless you try to flood the peer and check the amount of data sent (very bad !) plus, that result wouldn't be, in any way, accurate, because speed always varies a bit, and the client may have other programs using his line.
    when you do connect() you know the ip and port, of course. when accepting connections where's a small and accurate example, to the get the ip of the peer
    Code:
    //you may want this for better portability
    #ifndef WIN32
    typedef int SOCKET;
    #define closesocket close
    #endif
    
    
    //get your socket
    SOCKET sock = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);//you can and should use 0 instead, in the 3rd parameter
    
    //setup for listening incoming connections
    struct sockaddr_in my_info;
    my_info.sin_family = AF_INET;
    my_info.sin_addr.s_addr = htonl(INADDR_ANY);	
    my_info.sin_port = htons(80);//HTTP perhaps ? :P
    bind(sock,(struct sockaddr *)&my_info,sizeof(struct sockaddr_in));
    listen(sock,4);
    
    //now try to accept a peer's connection
    socklen_t sizeof_sockaddr = sizeof(struct sockaddr_in);
    sockaddr_in peer_info;
    SOCKET new_sock = accept(sock,(struct sockaddr*)&peer_info,&sizeof_sockaddr)
    
    printf("the peer's adress is %s\n",inet_ntoa(peer_info.sin_adr.s_addr));
    
    closesocket(sock);
    closesocket(new_sock);
    Last edited by xErath; 05-31-2005 at 10:48 PM.

  5. #5
    the c-dil
    Join Date
    May 2005
    Posts
    12

    host information

    For getting host Ip address , port number,name and other
    parameters one can use functions like:

    1.gethostbyaddr
    2.gethostbyname
    3.getservbyname
    4.getservbyport

    These database functions are defined in the interface header
    file netdb.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get to connect clients to server
    By pyngz in forum C# Programming
    Replies: 2
    Last Post: 03-11-2009, 04:46 AM
  2. Client: Failed to connect
    By Galvatron in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-12-2008, 03:40 PM
  3. Client / Server protocol
    By Witchfinder in forum C Programming
    Replies: 1
    Last Post: 05-05-2008, 04:59 PM
  4. Programming chat client, need some help (sockets & threads)
    By lalilulelo17 in forum Linux Programming
    Replies: 1
    Last Post: 04-19-2008, 04:01 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM