Socket Programming. Omer Ozarslan

Similar documents
Sockets 15H2. Inshik Song

A Client-Server Exchange

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Transport Layer (TCP/UDP)

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Introduction to Socket Programming

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

SOCKET. Valerio Di Valerio

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Network Programming in Python. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

EECS122 Communications Networks Socket Programming. Jörn Altmann

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

CS 640: Computer Networking

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

A set of processes distributed over a network that communicate via messages. Processes communicate via services offered by the operating system

Socket Programming TCP UDP

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CSCE 463/612 Networks and Distributed Processing Spring 2018

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Network Programming in Python. based on Chun, chapter 2; plus material on classes

Sockets Sockets Communication domains

Elementary TCP Sockets

Socket Programming for TCP and UDP

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

9th Slide Set Computer Networks

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

Internet Connectivity with

ECE 650 Systems Programming & Engineering. Spring 2018

CS321: Computer Networks Introduction to Application Layer

Transport Layer. <protocol, local-addr,local-port,foreign-addr,foreign-port> ϒ Client uses ephemeral ports /10 Joseph Cordina 2005

The Transmission Control Protocol (TCP)

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

CPS 214: Computer Networks. Slides by Adolfo Rodriguez

(Refer Slide Time: 1:09)

UNIX Sockets. COS 461 Precept 1

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

ECE 435 Network Engineering Lecture 2

Redesde Computadores(RCOMP)

Interprocess Communication

EEC-484/584 Computer Networks

Network Communication

Chapter 6. What happens at the Transport Layer? Services provided Transport protocols UDP TCP Flow control Congestion control

Transport layer Internet layer

ECE 650 Systems Programming & Engineering. Spring 2018

CSC 4900 Computer Networks: Transport Layer

The User Datagram Protocol

TSIN02 - Internetworking

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

Network and Security: Introduction

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Redes de Computadores (RCOMP)

Distributed Systems. 02. Networking. Paul Krzyzanowski. Rutgers University. Fall 2017

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Network Programming. Introduction to Sockets. Dr. Thaier Hayajneh. Process Layer. Network Layer. Berkeley API

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Oral. Total. Dated Sign (2) (5) (3) (2)

Joseph Faber Wonderful Talking Machine (1845)

Introduction and Overview Socket Programming Lower-level stuff Higher-level interfaces Security. Network Programming. Samuli Sorvakko/Nixu Oy

Python Networking Chris Seddon

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

Experiential Learning Workshop on Basics of Socket Programming

CSE 461 The Transport Layer

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

QUIZ: Longest Matching Prefix

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Intro to LAN/WAN. Transport Layer

CSEN 503 Introduction to Communication Networks. Mervat AbuElkheir Hana Medhat Ayman Dayf. ** Slides are attributed to J. F.

CS 4390 Computer Networks. Transport Services and Protocols

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Transport Layer. Chapter 3: Transport Layer

Transport Layer. Gursharan Singh Tatla. Upendra Sharma. 1

CSCD 330 Network Programming Spring 2018

ECE 435 Network Engineering Lecture 2

NETWORK AND SYSTEM PROGRAMMING

Transport Layer TCP & UDP Week 7. Module : Computer Networks Lecturers : Lucy White Office : 324

Programming with TCP/IP. Ram Dantu

The TCP Protocol Stack

CSE 461 Module 10. Introduction to the Transport Layer

CSCI Computer Networks

Networked Applications: Sockets. End System: Computer on the Net

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

CSE 461 Module 11. Connections

Transcription:

Socket Programming Omer Ozarslan omer@utdallas.edu 1

Outline Background Using TCP Example: hello_tcp Using UDP Example: hello_udp Blocking Calls and Threads Some Advises Code Examples Questions 2

Socket API Application Programming Interface (API) Interface to a particular service Abstract away implementation details A set of functions, data structures and constants In the same manner, Socket API Network Programming Interface Interface to Transport Layer protocols 3

Socket API (cont d) Socket API Network programming interface Application Transport TCP UDP Socket API Network IP 4

TCP & UDP TCP Connection-oriented Requires connection establishment & termination Reliable delivery In-order delivery Retransmission No duplicates High variance in latency Cost of the reliable service File-like interface (streaming) E.g., HTTP, SSH, FTP, UDP Connectionless Best-effort delivery Possible out-of-order delivery No retransmission Possible duplicates Low variance in latency Packet-like interface Requires packetizing E.g., DNS, VoIP, VOD, AOD, 5

TCP & UDP (cont d) When sending Hi! and Hope you re well TCP treats them as a single bytes stream Bytes stream l l e w e p o H! i H UDP treats them as separate messages Hope you re well Hi! 6

IP Addresses IPv4 32-bit numbers Ex: 127.0.0.1, 129.110.241.75 Network interfaces (ethernet, wireless etc.) have an IP address assigned to them. IPv6 128-bit numbers Ex: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 By IP address, assume IPv4 address in the scope of these slides. Used for routing, and forwarding packets. 7

Port Numbers Port numbers are used to distinguish different services sharing the same IP address. Port numbers can range (0 65535) Ports (0 1023) are designated as privileged services. Servers/daemons usually use well-known ports Any client can identify the server/service HTTP = 80, FTP = 21, Telnet = 23,... /etc/service defines well-known ports Clients usually use dynamic ports called ephemeral ports Range of ephemeral ports defined by operating system Assigned by the kernel at run time 8

Design Decisions Before you go to implement socket code, decide Do you need a TCP-based reliable, full duplex, connection oriented channel? Or you need a UDP-based, message oriented channel? Will the code you are developing be server or client? Client: We assume there is already a process running on another machine to connect to. Server: We start up and wait to be contacted (idle). What will be your port number for the server? 9

TCP/UDP application examples Pseudo code examples Real code examples 10

Using TCP Server Sockets Created to bind and listen to local endpoints in a server. Endpoint in this context means (IP address:port) A local endpoint can be bound by only one socket (otherwise Address already in use ). Not needed for a client role. Sockets Created to connect to a server in a client. Created by Server Sockets when accepting an connection from a client in a server. 11

Using TCP (cont d) In Java TCP: java.net.serversocket, java.net.socket In C/C++ (POSIX library) socket struct defined in sys/socket.h Set AF_INET, SOCK_STREAM parameters 12

hello_tcp Example 1. Server starts listening to TCP/10000. 2. Client connects to server on TCP/10000. 3. Server says HELLO to client. 4. Server closes its socket. 5. Client closes its socket. 13

hello_tcp Server sock_addr = (ANY_IP_ADDRESS, 10000) server_sock = socket(ip, TCP) bind(server_sock, sock_addr) listen(server_sock) loop: client_sock = accept(server_sock) send(client_sock, HELLO ) close(client_sock) ANY_IP_ADDRESS = 0.0.0.0 (Any IP address for this host) 14

hello_tcp Client sock_addr = (127.0.0.1, 10000) sock = socket(ip, TCP) connect(sock, sock_addr) loop: bytesread = recv(sock, buffer) if bytesread == 0: print( Server closed socket ) break loop print(buffer) close(sock) 127.0.0.1 = Address of the localhost 15

Using UDP Similar to TCP, but Connectionless, so no accept. Use of sendto/recvfrom instead of send/receive. In Java java.net.datagramsocket, java.net.datagrampacket In C/C++ (POSIX library) socket struct defined in sys/socket.h Set AF_INET, SOCK_DGRAM parameters 16

hello_udp Example Same scenario, but client says Hello instead of server. 1. Server starts listening to UDP/10000. 2. Client sends HELLO to server on UDP/10000. 3. Client closes its socket. 4. Server closes its socket. 17

hello_udp Server sock_addr = (ANY_IP_ADDRESS, 10000) sock = socket(ip, UDP) bind(sock, sock_addr) listen(sock) loop: recvfrom(server_sock, buffer, senders_addr) print(senders_addr, buffer) close(sock) 18

hello_udp Client sock_addr = (127.0.0.1, 10000) sock = socket(ip, TCP) sendto(sock, sock_addr, HELLO ) close(sock) 19

Blocking Calls Blocking calls: accept, recv, recvfrom What if we want to do something else while blocked? Send some data Read some user input (which is also a blocking call) Receive data from multiple clients at the same time (also blocking) 20

Revisiting hello_tcp Server Example sock_addr = (ANY_IP_ADDRESS, 10000) server_sock = socket(ip, TCP) bind(server_sock, sock_addr) listen(server_sock) loop: client_sock = accept(server_sock) send(client_sock, HELLO ) do some long task close(client_sock) Problem: Server can t accept another client waiting to be accepted while it s serving to an accepted client. 21

Multi-threading Threads Thread can be considered as a procedure that runs independently from the main program. Sequential program: a single stream of instructions in a program. Multi-threaded program: a program with multiple streams starts Thread 1 starts Thread 2 Main Thread Program flow 22

Solution One thread for user input, one thread for accept, one thread for receiving from each client, one thread to rule them all (main thread) 23

Solution (cont d) Spawn a thread for each blocking call. No need to spawn a thread for non-blocking calls, because, well, they don t block. You can use one thread for all non-blocking calls. Use concurrency primitives and be careful about race conditions, mutual exclusions etc. Alternative solution (beyond our scope) Asynchronous programming Make blocking calls non-blocking and poll over them Ex: select, poll, epoll for POSIX, java.nio package for Java Better for tuning performance Less intuitive, more complex 24

Some Advises POSIX library is written in C, so no Object Oriented Programming. If you re a C/C++ person, I recommend using C++11 or above with boost::asio library instead of POSIX functions. Java works. Python is simple and neat. Needs to be optimized using C/C++ for performance critical applications Don t optimize unless you need 25

Some Advises (cont d) Manpages are your friends. A common term: RTFM Read The Friendly Manual Read The Fabulous Manual Read The Frickin' Manual Read The Flaming Manual Read The Fine Manual Read The Fascinating Manual Docs are your friends for Java or python. StackOverflow is friend of everyone! 26

Questions Any questions? My office hours Thursdays 11:30am-1:30pm @ ECSS 2.104A1 Email omer@utdallas.edu 27

Code Examples https://github.com/ozars/socket-examples hello_tcp (C) hello_udp (C) quote (Java, using threads) 28