Re: sockets: connecting several UDP clients
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 20 Jan 2009 17:00:17 +0100
Gernot Frisch wrote:
sockaddr is the generic term for a variety of sockaddr_xx.
As such, a "struct sockaddr" isn't really something you would declare (or "instantiate").
OK, great.
Now, I'm quite uncertain if the using of the ports for UDP is correct in my mind. Can you tell if this pseudocode has a design flaw?
Also - do I need all bind() calls?
-server:
// s=server, t=TCP, u=UDP
// make a TCP socket, to allow clients to connect to
st=socket(TCP)
bind(st, 5555)
listen(st)
// open a UDP port to get some UDP messages
su=socket(UDP)
bind(su, 6666)
Rainer already mentioned this one.
// server main loop
for(;;)
{
// client connected via TCP to server
if(accept(st, remote_addr) != SOCKET_INVALID)
the accept() call will return a new socket descriptor. Even if all you are interested in is the IP address of the client, you must take the sd and close it:
if((newsd = accept(st, remote_addr)) != SOCKET_INVALID)
{
close(newsd);
{
// open a UDP connection to this
// client on port 6666.
// I want to write to the client, later
su1 = socket(UDP);
bind(su1, 6666);
If you don't have to send from port 6666, you don't need the new endpoint. Rather save the IP address and then send_to all the saved addresses later.
// store the new socket in a list
put_in_list(su1, list_su);
Maybe: "store the IP address of the new client in a list".
}
// get all UDP messages
// sent from any client to the server.
recv_from(su, buffer);
recv_from has lots'n'lots more arguments!
// send this package to all know clients
foreach(sock in list_su)
{
// my "sock" has a copy of the remote_addr
// I got from accept() above.
sent_to(sock, buffer);
}
}
One thing strikes me most:
your code will hang in the accept() call until a client connects. During this time, it will not receive any packets sent to it.
You may want to take a look at select() (or poll(), but don't ask me about that, I usually use select()) and it's FD friends (FD_CLR, FD_ISSET, FD_SET, FD_ZERO):
You need an fd_set of all the socket descriptors you want to communicate over, including "st". Every time you create a new endpoint, you FD_SET(su1, &fdset). Then, rather than call accept(), you call select(...) (note that the sets are modified!). If "st" is set in the readfd set, then you call accept(). If any other descriptor is set, you recv_from() that descriptor and distribute the message.
Depending upon the load, you also have to take care of congestion on the write side ...
Also: you only add new clients to the list. You should think about a mechanism to expire clients in the list.
If you wouldn't use UDP but used TCP, then you would be notified if either the client dies or it closes its endpoint.
Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
.
- Follow-Ups:
- Re: sockets: connecting several UDP clients
- From: Gernot Frisch
- Re: sockets: connecting several UDP clients
- References:
- sockets: connecting several UDP clients
- From: Gernot Frisch
- Re: sockets: connecting several UDP clients
- From: Josef Moellers
- Re: sockets: connecting several UDP clients
- From: Gernot Frisch
- Re: sockets: connecting several UDP clients
- From: Josef Moellers
- Re: sockets: connecting several UDP clients
- From: Gernot Frisch
- Re: sockets: connecting several UDP clients
- From: Josef Moellers
- Re: sockets: connecting several UDP clients
- From: Gernot Frisch
- sockets: connecting several UDP clients
- Prev by Date: Re: sockets: connecting several UDP clients
- Next by Date: Re: sockets: connecting several UDP clients
- Previous by thread: Re: sockets: connecting several UDP clients
- Next by thread: Re: sockets: connecting several UDP clients
- Index(es):
Relevant Pages
|