socket programming with udp

6
Socket programming with UDP 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

Upload: sukey

Post on 05-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

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?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Socket programming  with UDP

Socket programming with UDP

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

Page 2: 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.

Page 3: Socket programming  with UDP

UDP Socket Communications

Server Socket

Client Socket

Server Client

RecvSend

SendRecv

Client Socket

Client Socket

Page 4: Socket programming  with UDP

Client/server socket interaction: UDP

Close client

read datagram from

IPEndPoint ^receivePoint = gcnew IPEndPoint( IPAddress::Any, 0 );

array<Byte>^ data = client->Receive( receivePoint );

create socket:UdpClient ^ client = gcnew UdpClient( 0 ) ;

Create datagram with serverIP and

port=x; send datagram viaclient->Send( data, data->Length, L“serverIP", 12345 );

create socket, port= x:

UdpClient ^ client = gcnew UdpClient( 12345 );IPEndPoint ^receivePoint = gcnew IPEndPoint( IPAddress::Any, 0 );

read datagram fromarray<Byte>^ data = client->Receive( receivePoint );

write reply toclient->Send( data, data->Length, receivePoint );specifying client address,port number

server (running on serverIP) client

Page 5: Socket programming  with UDP

UDP Sockets in C++/CLI

• 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 );

Server Client

Page 6: Socket programming  with UDP

An Example