Socket Programming with UDP - PowerPoint PPT Presentation

Loading...

PPT – Socket Programming with UDP PowerPoint presentation | free to view - id: a67b9-YjJkM



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Socket Programming with UDP

Description:

Data = variable length. multiple of 4 bytes, padding done by ... new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket) ... – PowerPoint PPT presentation

Number of Views:197
Avg rating:3.0/5.0
Slides: 19
Provided by: JimKurosea263
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Socket Programming with UDP


1
  • Socket Programming with UDP

2
What is UDP?
  • User Datagram Protocol
  • Unreliable Datagram Protocol?
  • UDP is
  • unreliable
  • no guarantee on delivery sequence
  • no connection (less overhead)?

3
When would we use UDP?
  • Quick short bursts of communication
  • Connection overhead would be inefficient
  • e.g. DNS queries
  • Real time applications
  • No flow control, sending rate unregulated
  • e.g. audio/video streaming, loss of some packets
    is not critical
  • No connection state needed
  • More connections can be handled at endpoint

4
UDP Packet Format
IP Header
MAC Header
UDP Header
Data
Data Packet
32 bits
Source Port
Dest Port
Length
Checksum
Data
  • 2 bytes for source/destination ports (0-65536)?
  • Length (bytes) header data
  • Checksum of header and data
  • Data variable length
  • multiple of 4 bytes, padding done by kernel

5
Socket programming with UDP
  • UDP no connection between client and server
  • no handshaking
  • sender explicitly attaches IP address and port of
    destination to each packet
  • server must extract IP address, port of sender
    from received packet
  • UDP transmitted data may be received out of
    order, or lost

6
UDP Sockets in C
7
Socket programming with UDP
  • Socket creation with SOCK_DGRAM
  • int s socket(AF_INET, SOCK_DGRAM,0)
  • No explicit connection
  • No listen()?
  • No accept()?

8
Client-Server Flow
Client
Server
Connectionless! No listen(), accept(), or
connect() calls
socket()
socket()
sendto()
bind()
recvfrom()
wait for next packet from arbitrary client
sendto()
recvfrom()
close()
close()
9
UDP socket methods - sendto()?
ssize_t sendto( int sockfd,void buff,
size_t nbytes, int flags, const
struct sockaddr to, socklen_t addrlen)
  • sendto() instead of send() must specify client
    to send packet to
  • Returns bytes sent to the kernel network stack,
    not what was sent to the client!
  • Possible to send 0 bytes of data!
  • No error message to indicate non-receipt of data
  • Possible errors
  • EBADF, ENOTSOCK bad socket descriptor
  • EFAULT bad buffer address
  • EMSGSIZE message too large
  • ENOBUFS system buffers are full

10
UDP socket methods - recvfrom()?
ssize_t recvfrom( int sockfd, void
buff, size_t nbytes,int flags, struct
sockaddr from, socklen_t fromaddrlen)
  • recvfrom() instead of recv() can receive from
    anyone
  • Buffer must be large enough, else data is lost
    forever
  • recvfrom() is blocking, returns only on packet
    receipt
  • Same errors as sendto, with addition of
  • EINTR System call interrupted by signal
  • Useful if recvfrom() should timeout after a while

11
Issues with UDP packets
  • Datagram size
  • Should not be too large, else IP fragmentation
    occurs
  • increases the chance of lost packets
  • Connected UDP sockets
  • Calling connect on a socket specifies the
    destination
  • Can use send()/recv() instead of
    sendto()/recvfrom
  • If server calls connect, will not receive packets
    from anyone else
  • ICMP error messages
  • If server is not listening on port, it returns
    ICMP error message to the sending host
  • If ICMP error received, sendto() returns
    ECONNREFUSED
  • sendto() may not return this error until next
    call to socket!

12
UDP Sockets in Java
13
Example Java client (UDP)
Client process
Input receives packet (recall thatTCP received
byte stream)
Output sends packet (recall that TCP sent byte
stream)
client UDP socket
14
Example Java client (UDP)
import java.io. import java.net. class
UDPClient public static void main(String
args) throws Exception
BufferedReader inFromUser new
BufferedReader(new InputStreamReader(System.in))
DatagramSocket clientSocket new
DatagramSocket() InetAddress IPAddress
InetAddress.getByName("hostname")
byte sendData new byte1024 byte
receiveData new byte1024 String
sentence inFromUser.readLine() sendData
sentence.getBytes()
Create input stream
Create client socket
Translate hostname to IP address using DNS
15
Example Java client (UDP), cont.
Create datagram with data-to-send, length, IP
addr, port
DatagramPacket sendPacket new
DatagramPacket(sendData, sendData.length,
IPAddress, 9876) clientSocket.send(send
Packet) DatagramPacket receivePacket
new DatagramPacket(receiveData,
receiveData.length) clientSocket.receiv
e(receivePacket) String
modifiedSentence new
String(receivePacket.getData())
System.out.println("FROM SERVER"
modifiedSentence) clientSocket.close()

Send datagram to server
Read datagram from server
16
Example Java server (UDP)
import java.io. import java.net. class
UDPServer public static void main(String
args) throws Exception
DatagramSocket serverSocket new
DatagramSocket(9876) byte
receiveData new byte1024 byte
sendData new byte1024 while(true)
DatagramPacket
receivePacket new
DatagramPacket(receiveData, receiveData.length)
serverSocket.receive(receivePacket)
Create datagram socket at port 9876
Create space for received datagram
Receive datagram
17
Example Java server (UDP), cont
String sentence new
String(receivePacket.getData())
InetAddress IPAddress receivePacket.getAddress()
int port receivePacket.getPort()
String
capitalizedSentence sentence.toUpperCase()
sendData capitalizedSentence.getBytes()
DatagramPacket sendPacket
new DatagramPacket(sendData,
sendData.length, IPAddress,
port) serverSocket.send(s
endPacket)
Get IP addr port , of sender
Create datagram to send to client
Write out datagram to socket
End of while loop, loop back and wait for another
datagram
18
Questions?
About PowerShow.com