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