Advertisement

UDP and winsocket

Started by September 22, 2003 04:22 PM
5 comments, last by logout 21 years, 4 months ago
Okay i got some simple UDP stuff up and going... Now i got 2 questions : 1: In order to send back info from the server do i need to bind() a socket on the client side as well ? 2 ( most inportant ) : How can i identyfy witch client that are sending to me ? Right now im doing some magic on the "cli_addr.sin_addr.S_un.S_addr" But whats a smart way of doing this ? *and ensuring that the correct player gets handled as the correct player*
2.) IP addresses
There is no point in doing anything at all, you'll die pretty soon anyway.
Advertisement
yeah i know that but how should i store/find the correct data ?

i am pretty sure a linear seach tru all clients for EVERY packet will be prett slow on the server. ..
quote:
Original post by logout
i am pretty sure a linear seach tru all clients for EVERY packet will be prett slow on the server. ..


I used an STL map.
Yeah, foofightr''s using a map idea is good enough. You could use a map for address (an unsigned int 32-bit) to client_info (a class object carrying client details) mapping.

Also, you should check out http://www.ecst.csuchico.edu/~beej/guide/net/ to start up with network programming and getting your basic questions answered.
that guide donsent touch the second question...

but whats the diffrence on a map and a hashtable ?
Advertisement
I think Synth0id answered your 2nd question. IP address, under connection-less communication, can be used to determine the source. Check the sockaddr* parameter in the recvfrom function.

Hashtables are a collection of key-value pairs where the key is passed through a process (refered to as a hash-function) to generate a "hash" of that key. This generated hash is then used to locate the value within the data structure.

For example, let us assume that your key is "truman" (a key doesn't necessarily have to be a string), a simple hash function could sum up the characters of this string to generate an index into an array. Hence facilitating lookup.

A map could use a hashtable.

Considering your current problem, you can lookup clients (as I said earlier) based on the incoming data's source IP address.

std::map<unsigned int, client_info*> mapClients ;

recvfrom (...) ;
unsigned int addr = extractipaddr (...) ;
client_info *ci = mapClients [addr] ;

lookup recvfrom, and sockaddr_in (if using IPv4).

[edited by - UdayK on September 23, 2003 10:05:46 AM]

This topic is closed to new replies.

Advertisement