C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 05-23-2005, 12:22 PM   #1
Registered User
 
Finchie_88's Avatar
 
Join Date: Aug 2004
Posts: 146
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.
__________________

Finchie_88 is offline   Reply With Quote
Old 05-31-2005, 05:38 PM   #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   Reply With Quote
Old 05-31-2005, 05:57 PM   #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   Reply With Quote
Old 05-31-2005, 10:43 PM   #4
#include<xErath.h>
 
xErath's Avatar
 
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   Reply With Quote
Old 06-01-2005, 07:12 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22