![]() |
| | #1 |
| Registered User Join Date: Aug 2004
Posts: 146
| Getting info on a client when they connect to the server
__________________ |
| Finchie_88 is offline | |
| | #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. |
| kuphryn is offline | |
| | #3 |
| Registered User Join Date: Jan 2005
Posts: 847
| You can also have accept() fill a buffer with the address of the connecting client. |
| Quantum1024 is offline | |
| | #4 |
| #include<xErath.h> Join Date: Jun 2004
Posts: 724
| 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. |
| xErath is offline | |
| | #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 |
| rajatkochhar is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can't get to connect clients to server | pyngz | C# Programming | 2 | 03-11-2009 04:46 AM |
| Client: Failed to connect | Galvatron | Networking/Device Communication | 7 | 05-12-2008 03:40 PM |
| Client / Server protocol | Witchfinder | C Programming | 1 | 05-05-2008 04:59 PM |
| Programming chat client, need some help (sockets & threads) | lalilulelo17 | Linux Programming | 1 | 04-19-2008 04:01 AM |
| socket newbie, losing a few chars from server to client | registering | Linux Programming | 2 | 06-07-2003 11:48 AM |