60 likes | 80 Vues
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. Socket programming with UDP. Why UDP?.
E N D
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 Socket programming with UDP
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 Can receive from multiple clients, and send to them. Client Recv Send Server Socket Client Socket Create socket, Bind the socket to an IPEndPoint Create socket, Bind the socket to an IPEndPoint Send Recv Client Socket Client Socket
//create socket: //Create datagram with serverIP and port=x; send datagram viaclient->Send( data, data->Length, L“serverIP", 12345 ); UdpClient ^ client = gcnew UdpClient( 0 ) ; //read datagram from IPEndPoint ^receivePoint = gcnew IPEndPoint( IPAddress::Any, 0 ); array<Byte>^ data = client->Receive( receivePoint ); Close client Client/server socket interaction: UDP server (running on serverIP) client //create socket, port= x: UdpClient ^ client = gcnew UdpClient( 12345 ); IPEndPoint ^receivePoint = gcnew IPEndPoint( IPAddress::Any, 0 ); //read datagram from array<Byte>^ data = client->Receive( receivePoint ); write reply to client->Send( data, data->Length, receivePoint ); specifying client address, port number
UDP Sockets in C++/CLI Server Client • UdpClient ^ client = gcnew UdpClient( 12345 ) • IPEndPoint ^receivePoint = gcnew IPEndPoint( IPAddress::Any, 0 ); • array<Byte>^ data = client->Receive( receivePoint ); • client->Send( data, data->Length, receivePoint ); • UdpClient ^ client = gcnew UdpClient( 0 ) ; • client->Send( data, data->Length, L“127.0.0.1", 12345 ); • IPEndPoint ^receivePoint = gcnew IPEndPoint( IPAddress::Any, 0 ); • array<Byte>^ data = client->Receive( receivePoint );