UDP Socket Programming “No frills,” “bare bones” Internet transport protocol “Best effort” service, UDP segments may be: Lost Delivered out of order to app Connectionless: No handshaking between UDP sender, receiver Each UDP segment handled independently of others
Why is there a UDP? No connection establishment (which can add delay) Simple: no connection state at sender, receiver Small header No congestion control: UDP can blast away as fast as desired
UDP (User Datagram protocol) Often used for streaming multimedia apps Loss tolerant Rate sensitive Other UDP uses (why?): DNS, SNMP Reliable transfer over UDP Must be at application layer Application-specific error recovery
Application data (message) UDP segment format Source port # Dest port # 32 bits Application data (message) UDP segment format Length Checksum Length, in bytes of UDP segment, including header
UDP Checksum Sender: Treat segment contents as sequence of 16-bit integers Checksum: addition (1’s complement sum) of segment contents Sender puts checksum value into UDP checksum field Receiver: Compute checksum of received segment Check if computed checksum equals checksum field value: NO - error detected YES - no error detected But maybe errors nonethless?
Praktek Aplikasi UDP Sama seperti TCP, UDP memerlukan jaringan client server atau dengan bahasa yang lain Sender dan receiver Form Sender (Server)
' aplikasi UDP Timer dirancang Oleh Tri Daryanto tgl 03/07/2006 Option Explicit 'prosedur perintah untuk keluar dari aplikasi Private Sub Command1_Click() End End Sub 'prosedur untuk menggunkan setting protokol dan port Private Sub Form_Load() scksender.Protocol = sckUDPProtocol scksender.LocalPort = 44331
'prosedur menerima data dari client, jika remote host IP tidak sama dengan local IP Private Sub scksender_DataArrival(ByVal bytesTotal As Long) Dim s As String scksender.GetData s If scksender.RemoteHostIP <> scksender.LocalIP Then Caption = s 'tampilkan di caption label form End If End Sub Private Sub Timer1_Timer() With scksender .RemoteHost = "127.0.0.1" .RemotePort = 44332 .SendData Text1.Text 'kirim data berupa data jam dan tanggal ke receiver Text1 = Now 'ambil data jam dan tanggal bios pada hari ini End With
Form Receiver (Client)
' aplikasi UDP Timer di rancangcang Oleh Tri Daryanto tgl 03/07/2006 Option Explicit 'prosedur untuk mengirim balik data yang diterima client ke server Private Sub Command1_Click() receiver.RemoteHost = "127.0.0.1" receiver.RemotePort = 44331 receiver.SendData Text1.Text End Sub 'prosedur keluar aplikasi UDP Client Private Sub Command2_Click() End
'prosedur memberitahukan kepada server bahwa client (receiver)siap menerima data Private Sub Form_Load() receiver.Protocol = sckUDPProtocol receiver.LocalPort = 44332 receiver.Bind ' menunggu data dari server End Sub 'prosedur mengambil data dari jaringan dan ditampilkan di textbox Private Sub receiver_DataArrival(ByVal bytesTotal As Long) Dim s As String receiver.GetData s 'mengambil data yang dikirim sender Text1.Text = s 'tampilkan data kedalam text box