Socket programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

Socket Programming PowerPoint PPT Presentation


  • 53 Views
  • Uploaded on
  • Presentation posted in: General

Socket Programming. With TCP and UDP. Christoffer Brodd-Reijer Zaruhi Aslanyan. Socket. High level connection between two hosts Identified by port+IP Transport layer (UDP, TCP, etc) or raw IP. Socket. Similar to operating on a file: open read/write close. Socket.

Download Presentation

Socket Programming

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Socket programming

Socket Programming

With TCP and UDP

  • ChristofferBrodd-Reijer

  • ZaruhiAslanyan


Socket

Socket

  • High level connection between two hosts

  • Identified by port+IP

  • Transport layer (UDP, TCP, etc) or raw IP


Socket1

Socket

Similar to operating on a file:

  • open

  • read/write

  • close


Socket2

Socket

Systematic overview:


Socket programming

TCP

  • Transmission Control Protocol

  • Created in 1974

  • Operates on of a stream of octets (bytes)


Socket programming

TCP

Proc

  • Connection orientated

  • Reliable

  • Ordered

Cons

  • Slow

  • Complex

Good for

  • World Wide Web

  • Remote administration

  • File transfer (FTP)

  • Electronic mail (SMTP)


Socket programming

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)


Socket programming

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


Socket programming

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])


Socket programming

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])


Socket programming

UDP

  • User Datagram Protocol

  • Created in 1980

  • Operates on whole datagrams (messages); not bytes


Socket programming

UDP

Cons:

  • Unreliable delivery

  • Unreliable order

  • Possible duplication

Pros:

  • Simple

  • Fast

    Good for:

  • Streaming

  • Gaming

  • Name lookup (DNS)

  • Time lookup (NTP)


Socket programming

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)


Socket programming

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


Socket programming

UDP

Using UDP sockets in Ruby:

// create socket

s = UDPSocket.new

// receive data

msg, sender = s.recv_from MAXLEN

// send data

s.send msg, OPTIONS


Socket programming

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)


Summary

Summary

  • Socket: End-to-End handler

  • TCP: connection, bytes, reliable

  • UDP: datagrams, fast, simple

  • Code is similar between languages (same principles applies)


Thank you

Thank you!


  • Login