1 / 18

Socket Programming

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.

juro
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programming With TCP and UDP • ChristofferBrodd-Reijer • ZaruhiAslanyan

  2. Socket • High level connection between two hosts • Identified by port+IP • Transport layer (UDP, TCP, etc) or raw IP

  3. Socket Similar to operating on a file: • open • read/write • close

  4. Socket Systematic overview:

  5. TCP • Transmission Control Protocol • Created in 1974 • Operates on of a stream of octets (bytes)

  6. TCP Proc • Connection orientated • Reliable • Ordered Cons • Slow • Complex Good for • World Wide Web • Remote administration • File transfer (FTP) • Electronic mail (SMTP)

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

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

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

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

  11. UDP • User Datagram Protocol • Created in 1980 • Operates on whole datagrams (messages); not bytes

  12. UDP Cons: • Unreliable delivery • Unreliable order • Possible duplication Pros: • Simple • Fast Good for: • Streaming • Gaming • Name lookup (DNS) • Time lookup (NTP)

  13. 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)

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

  15. 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

  16. 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)

  17. Summary • Socket: End-to-End handler • TCP: connection, bytes, reliable • UDP: datagrams, fast, simple • Code is similar between languages (same principles applies)

  18. Thank you!

More Related