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.
This is a discussion on Getting info on a client when they connect to the server within the Networking/Device Communication forums, part of the General Programming Boards category; I'm writing some server code at the moment, and was wondering if there was a function that will give me ...
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.
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.
You can also have accept() fill a buffer with the address of the connecting client.
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.
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