UDP client implementation
-
Hi, Can someone tell me how to implement a UDP client. I already did a TCP client (it works fine), and read some of the MSDN on some differences. Is the only difference that I use SOCK_DGRAM instead of SOCK_STREAM ? Where can I find a list of the protocols address family (af) for the function socket? Can some tell me where to find a detailed article or some sample code. Thanks. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
Hi, Can someone tell me how to implement a UDP client. I already did a TCP client (it works fine), and read some of the MSDN on some differences. Is the only difference that I use SOCK_DGRAM instead of SOCK_STREAM ? Where can I find a list of the protocols address family (af) for the function socket? Can some tell me where to find a detailed article or some sample code. Thanks. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
Well, during the creation you just pass the
SOCKET_DGRAM
instead ofSOCKET_STREAM
. Then you might 'bind
' it to the local port (selecting the client side port number - unusual in tcp). If you don't do it it will choose the port automatically as in tcp, but most udp protocol expects also known client port, so usually you have to do it. Then the usage is slightly different - on write side whatever you write it will send a packet immediatelly (no nagle alg. as in tcp) therefore if you write every byte separately, there will be separate packet for every byte (thus big overhead). You also have to specify the destination address - this is in tcp taken from the 'connect
', but there's no connect, because it's connectionless socket ;). Reading part - here you can use therecvfrom
orWSARecvFrom
functions which returns also the address from where the datagram was sent and you can process it. That's the difference between udp and tcp - in udp you can communicate with several computers using one socket. Therefore you have to specify the address in write and receive it in read. Another issue with udp is that it's not reliable - it's only best effort service, so during design of the protocol you have to think about the fact, that the packets (data) can be lost - this is normally controlled by tcp and in that case the data are resend, but it's not valid in udp, here it's up to you. That's just brief description of udp-tcp differences. I hope it's not too much confusing ;-)