Socket programming with UDP no connection between client
Socket programming with UDP: no “connection” between client & server • sender explicitly attaches IP destination address and port # to each packet • receiver extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order
Why UDP? • When receiving a msg, all the data can be extracted directly. • Can extract sender’s IP and Port#. • UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server – loss, out of order • Applications (tolerant loss and mis-order )? – Games? Audio/Video? – Additional code maybe needed.
UDP Socket Communications Server Client Recv Server Socket Send Recv Client Socket
Client/server socket interaction: UDP server (running on server. IP) client create socket, port= x: Udp. Client ^ client = gcnew Udp. Client( 12345 ); IPEnd. Point ^receive. Point = gcnew IPEnd. Point( IPAddress: : Any, 0 ); create socket: Udp. Client ^ client = gcnew Udp. Client( 0 ) ; read datagram from array<Byte>^ data = client>Receive( receive. Point ); write reply to client->Send( data, data->Length, receive. Point ); specifying client address, port number Create datagram with server. IP and port=x; send datagram via client->Send( data, data->Length, L“server. IP", 12345 ); read datagram from IPEnd. Point ^receive. Point = gcnew IPEnd. Point( IPAddress: : Any, 0 ); array<Byte>^ data = client->Receive( receive. Point ); Close client
UDP Sockets in C++/CLI Server • Udp. Client ^ client = gcnew Udp. Client( 12345 ) • IPEnd. Point ^receive. Point = gcnew IPEnd. Point( IPAddress: : Any, 0 ); • array<Byte>^ data = client>Receive( receive. Point ); • client->Send( data, data>Length, receive. Point ); Client • Udp. Client ^ client = gcnew Udp. Client( 0 ) ; • client->Send( data, data>Length, L“ 127. 0. 0. 1", 12345 ); • IPEnd. Point ^receive. Point = gcnew IPEnd. Point( IPAddress: : Any, 0 ); • array<Byte>^ data = client>Receive( receive. Point );
An Example
- Slides: 6