With TCP and UDP Christoffer BroddReijer Zaruhi Aslanyan Socket High level connection between two hosts Identified by portIP Transport layer UDP TCP etc or raw IP Socket Similar to operating on a file ID: 406514
Download Presentation The PPT/PDF document "Socket Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Socket Programming
With TCP and UDP
Christoffer
Brodd-Reijer
Zaruhi
AslanyanSlide2
Socket
High level connection between two hosts
Identified by port+IP
Transport layer (UDP, TCP, etc) or raw IPSlide3
Socket
Similar to operating on a file:
open
read/write
closeSlide4
Socket
Systematic overview:Slide5
TCP
Transmission Control Protocol
Created in 1974
Operates on of a stream of octets (bytes)Slide6
TCP
ProcConnection orientated
Reliable
Ordered
Cons
Slow
Complex
Good for
World Wide
Web
Remote administration
File transfer
(FTP)
Electronic
mail (SMTP)
Slide7
TCP
Using TCP sockets in C on UNIX:
// create socket
s = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP)
// receive data
recvfrom(s, buf, len, flags, &scr_addr, &addrlen)
// send data
sendto(s, buf, len, flags, &dect_addr, addrlen)Slide8
TCP
Using TCP sockets in Java:
// create socket
Socket s = new Socket(address, port);
// receive data
InputStream in = s.getInputStream();
// send data
OutputStream out = s.getOutputStream();Slide9
TCP
Using TCP sockets in Ruby:
// create socket
s = TCPSocket.new(host, port)
// receive data
str = s.recv(len[, flags])
// send data
s.send(mesg, flags[, to])Slide10
TCP
Using TCP sockets in Python:
// create socket
s = socket.socket([socket.AF_INET[,
socket.SOCK_STREAM]])
// receive data
socket.recv(bufsize[, flags])
// send data
socket.sendall(string[, flags])Slide11
UDP
User Datagram Protocol
Created in 1980
Operates on whole datagrams (messages); not bytesSlide12
UDP
Pros:
Simple
Fast
Good for:
Streaming
Gaming
Name lookup (DNS)
Time lookup (NTP)
Cons
:
Unreliable delivery
Unreliable order
Possible duplicationSlide13
UDP
Using UDP sockets in C on UNIX:
// create socket
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
// receive data
recvfrom(s, buf, LEN, 0, &opt, &optlen)
// send data
sendto(s, buf, LEN, 0, &opt, optlen)Slide14
UDP
Using UDP sockets in Java:
// create socket
s = new DatagramSocket(PORT);
// receive data
p = new DatagramPacket(buf, buf.length);
s.receive(p);
// send data
p = new DatagramPacket(buf, buf.length, IP, PORT)
s.send(p);Slide15
UDP
Using UDP sockets in Ruby:
// create socket
s = UDPSocket.new
// receive data
msg, sender = s.recv_from MAXLEN
// send data
s.send msg, OPTIONSSlide16
UDP
Using UDP sockets in Python:
// create socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
// receive data
msg = s.recv(MAXLEN)
// send data
s.sendall(msg)Slide17
Summary
Socket: End-to-End handler
TCP: connection, bytes, reliable
UDP: datagrams, fast, simple
Code is similar between languages (same principles applies)Slide18
Thank you!