Katz, Stoica F04 EECS 122: Introduction to Computer Networks Sockets Programming Computer Science Division Department of Electrical Engineering and Computer.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Intermediate TCP/IP TCP Operation.
Socket Programming in C Slides Adapted on Jörn Altmann‘s Slides.
Socket Programming Application Programming Interface.
Socket Programming.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
EE122: Socket Programming Igor Ganichev Originally prepared by DK Moon.
Introduction to Project 1 Web Client and Server Jan 2006.
EE122: Socket Programming DK Moon
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP.
Gursharan Singh Tatla Transport Layer 16-May
IP Multiplexing Ying Zhang EECS 489 W07.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Process-to-Process Delivery:
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Overview of TCP/IP protocols –Application layer (telnet, ssh, http, ftp, etc) The things that we use daily. –Transport layer (TCP, UDP) Allows processes.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Introduction to Socket
1 Socket Programming EE 122: Intro to Communication Networks Vern Paxson TAs: Lisa Fowler, Daniel Killebrew, Jorge Ortiz Materials with thanks to Sukun.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Introduction to Sockets
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
McGraw-Hill Chapter 23 Process-to-Process Delivery: UDP, TCP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Network Communications A Brief Introduction. 2 Network Communications.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Assignment 3 A Client/Server Application: Chatroom
Sockets and Beginning Network Programming
Socket Programming EE 122: Intro to Communication Networks Vern Paxson
Transport layer API: Socket Programming
Process-to-Process Delivery:
EE 122: Sockets Kevin Lai September 11, 2002.
Socket Programming.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Outline Communications in Distributed Systems Socket Programming
Process-to-Process Delivery: UDP, TCP
Presentation transcript:

Katz, Stoica F04 EECS 122: Introduction to Computer Networks Sockets Programming Computer Science Division Department of Electrical Engineering and Computer Sciences University of California, Berkeley Berkeley, CA

Katz, Stoica F04 2 Outline  Socket API motivation, background  Names, addresses, presentation  API functions  I/O multiplexing  Some “find-the-bug” mini-C-quizzes

Katz, Stoica F04 3 Quiz!  What is wrong with the following code? void alpha () { rtgptr ptr = rtg_headptr; while (ptr != NULL) { rtg_check (ptr); ptr = ptr->nextptr; } void rtg_check (rtgptr ptr) { if (ptr->value == 0) free (ptr); } struct routeptr { int value; struct routeptr *nextptr; } typedef struct routeptr rtgptr;

Katz, Stoica F04 4  Applications need Application Programming Interface (API) to use the network  API: set of function types, data structures and constants Allows programmer to learn once, write anywhere Greatly simplifies job of application programmer Motivation physical data-link network transport application API

Katz, Stoica F04 5 Sockets (1)  What exactly are sockets? -An endpoint of a connection -A socket is associated with each end-point (end-host) of a connection  Identified by IP address and port number  Berkeley sockets is the most popular network API -Runs on Linux, FreeBSD, OS X, Windows -Fed/fed off popularity of TCP/IP

Katz, Stoica F04 6 Sockets (2)  Similar to UNIX file I/O API (provides file descriptor)  Based on C, single threaded model -Does not require multiple threads  Can build higher-level interfaces on top of sockets -e.g., Remote Procedure Call (RPC)

Katz, Stoica F04 7 Types of Sockets (1)  Different types of sockets implement different service models -Stream v.s. datagram  Stream socket (aka TCP) -Connection-oriented (includes establishment + termination) -Reliable, in order delivery -At-most-once delivery, no duplicates -E.g., ssh, http  Datagram socket (aka UDP) -Connectionless (just data-transfer) -“Best-effort” delivery, possibly lower variance in delay -E.g., IP Telephony, streaming audio

Katz, Stoica F04 8 Types of Sockets (2)  How does application programming differ between stream and datagram sockets?  Stream sockets -No need to packetize data -Data arrives in the form of a byte-stream -Receiver needs to separate messages in stream User application sends messages “Hi there!” and “Hope you are well” separately physical data-link network transport application TCP sends messages joined together, ie. “Hi there!Hope you are well”

Katz, Stoica F04 9 Types of Sockets (3)  Stream socket data separation: -Use records (data structures) to partition data stream -How do we implement variable length records? -What if field containing record size gets corrupted? Not possible! Why? ABC4 fixed length record fixed length record variable length record size of record

Katz, Stoica F04 10 Types of Sockets (4)  Datagram sockets -User packetizes data before sending -Maximum size of 64Kbytes -Further packetization at sender end and depacketization at receiver end handled by transport layer -Using previous example, “Hi there!” and “Hope you are well” will definitely be sent in separate packets at network layer

Katz, Stoica F04 11 Naming and Addressing  IP version 4 address -Identifies a single host -32 bits -Written as dotted octets e.g., 0x0a is  Host name -Identifies a single host -Variable length string -Maps to one or more IP address e.g., -Gethostbyname translates name to IP address  Port number -Identifies an application on a host -16 bit unsigned number

Katz, Stoica F04 12 Presentation high-order bytelow-order byte increasing memory addresses address Aaddress A +1 little-endian big-endian low-order bytehigh-order byte 16-bit value (network byte-order)

Katz, Stoica F04 13 Byte Ordering Solution uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t host32bitvalue); uint16_t ntohs(uint16_t net16bitvalue); uint32_t ntohl(uint32_t net32bitvalue);  Use for all numbers (int, short) to be sent across network -Including port numbers, but not IP addresses

Katz, Stoica F04 14 Quiz!  What is wrong with the following code? int factorial (int a) { /* we want to if (a == 0 || a == 1) return factorial return 1; of a */ if (a < 0) return -1; return a * factorial (a-1); }

Katz, Stoica F04 15 Stream Sockets  Implements Transmission Control Protocol (TCP)  Does NOT set up virtual-circuit!  Sequence of actions: socket () bind () listen () accept () recv () close () socket () connect () send () recv () close () time initialize establish data xfer terminate ClientServer reuse ()

Katz, Stoica F04 16 Initialize (Client + Server) int sock; if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket"); printf("Failed to create socket\n"); abort (); }  Handling errors that occur rarely usually consumes most of systems code -Exceptions (e.g., in java) helps this somewhat

Katz, Stoica F04 17 Initialize (Server reuse addr)  After TCP connection closes, waits for 2MSL, which is twice maximum segment lifetime (from 1 to 4 mins)  Segment refers to maximum size of a packet  Port number cannot be reused before 2MSL  But server port numbers are fixed  must be reused  Solution: int optval = 1; if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) { perror ("opening TCP socket"); abort (); } if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval)) <0) { perror (“reuse address"); abort (); }

Katz, Stoica F04 18 Initialize (Server bind addr)  Want port at server end to use a particular number struct sockaddr_in sin; memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = IN_ADDR; sin.sin_port = htons (server_port); if (bind(sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror(“bind"); printf("Cannot bind socket to address\n"); abort(); }

Katz, Stoica F04 19 Initialize (Server listen)  Wait for incoming connection  Parameter BACKLOG specifies max number of established connections waiting to be accepted (using accept() ) if (listen (sock, BACKLOG) < 0) { perror (“error listening"); abort (); }

Katz, Stoica F04 20 Establish (Client) struct sockaddr_in sin; struct hostent *host = gethostbyname (argv[1]); unsigned int server_addr = *(unsigned long *) host->h_addr_list[0]; unsigned short server_port = atoi (argv[2]); memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = server_addr; sin.sin_port = htons (server_port); if (connect(sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror("connect"); printf("Cannot connect to server\n"); abort(); }

Katz, Stoica F04 21 Establish (Server)  Accept incoming connection int addr_len = sizeof (addr); int sock; sock = accept (tcp_sock, (struct sockaddr *) &addr, &addr_len); if (sock < 0) { perror ("error accepting connection"); abort (); }

Katz, Stoica F04 22 Sending Data Stream int send_packets (char *buffer, int buffer_len) { sent_bytes = send (sock, buffer, buffer_len, 0); if (send_bytes < 0) perror (“send”); return 0; }

Katz, Stoica F04 23 Receiving Data Stream int receive_packets(char *buffer, int buffer_len, int *bytes_read) { int left = buffer_len - *bytes_read; received = recv(sock, buffer + *bytes_read, left, 0); if (received < 0) { perror ("Read in read_client"); printf("recv in %s\n", __FUNCTION__); } if (received == 0) { /* occurs when other side closes connection */ return close_connection(); } *bytes_read += received; while (*bytes_read > RECORD_LEN) { process_packet(buffer, RECORD_LEN); *bytes_read -= RECORD_LEN; memmove(buffer, buffer + RECORD_LEN, *bytes_read); } return 0; }

Katz, Stoica F04 24 Quiz!  We have the following packet header format (the numbers denote field size in bytes) :  What is wrong with the code below? typedef struct _pkt_hdr_ { unsigned short length; char type; unsigned int src_addr; unsigned int dest_addr; } pkt_hdr, *pkt_hdrptr;... char buffer[256]; pkt_hdr header; /* assume fields filled in properly here */ memcpy (buffer, &header, sizeof (header)); send (sock, buffer, sizeof (header), 0); lengthtype source addr dest addr 2144 lengthtype???? source addr dest addr words aligned in memory: begins at address that is a multiple of bytes in word 21144

Katz, Stoica F04 25 Datagram Sockets  Similar to stream sockets, except: -Sockets created using SOCK_DGRAM instead of SOCK_STREAM -No need for connection establishment and termination -Uses recvfrom() and sendto() in place of recv() and send() respectively -Data sent in packets, not byte-stream oriented

Katz, Stoica F04 26 How to handle multiple connections?  Where do we get incoming data? -Stdin (typically keyboard input) -All stream, datagram sockets -Asynchronous arrival, program doesn’t know when data will arrive  Solution: I/O multiplexing using select () -Coming up soon  Solution: I/O multiplexing using polling -Very inefficient  Solution: multithreading -More complex, requires mutex, semaphores, etc. -Not covered

Katz, Stoica F04 27 I/O Multiplexing: Polling int opts = fcntl (sock, F_GETFL); if (opts < 0) { perror ("fcntl(F_GETFL)"); abort (); } opts = (opts | O_NONBLOCK); if (fcntl (sock, F_SETFL, opts) < 0) { perror ("fcntl(F_SETFL)"); abort (); } while (1) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; } get data from socket get user input first get current socket option settings then adjust settings finally store settings back

Katz, Stoica F04 28 I/O Multiplexing: Select (1)  Select() -Wait on multiple file descriptors/sockets and timeout -Application does not consume CPU cycles while waiting -Return when file descriptors/sockets are ready to be read or written or they have an error, or timeout exceeded  Advantages -Simple -More efficient than polling  Disadvantages -Does not scale to large number of file descriptors/sockets -More awkward to use than it needs to be

Katz, Stoica F04 29 I/O Multiplexing: Select (2) fd_set read_set; struct timeval time_out; while (1) { FD_ZERO (read_set); FD_SET (stdin, read_set); /* stdin is typically 0 */ FD_SET (sock, read_set); time_out.tv_usec = ; time_out.tv_sec = 0; select_retval = select(MAX(stdin, sock) + 1, &read_set, NULL, NULL, &time_out); if (select_retval < 0) { perror ("select"); abort (); } if (select_retval > 0) { if (FD_ISSET(sock, read_set)) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (FD_ISSET(stdin, read_set)) { if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; } set up parameters for select() run select() interpret result

Katz, Stoica F04 30 Common Mistakes + Hints  Common mistakes: -C programming Use gdb Use printf for debugging, remember to do fflush(stdout) ; -Byte-ordering -Use of select() -Separating records in TCP stream -Not knowing what exactly gets transmitted on the wire Use tcpdump / Ethereal  Hints: -Use man pages (available on the web too) -Check out WWW, programming books

Katz, Stoica F04 31 Project Orion (1)  Basically a messaging program  Client registers with server, after which server forwards messages to other clients in the same session

Katz, Stoica F04 32 Project Orion (2)  First project: write client and server code  Specs at  Divided into 2 checkpoints: -Checkpoint 1: client code, due 9/24/03, 3.50pm -Checkpoint 2: server code, due 10/1/03, 3.50pm  Some details in project 1 may seem unnecessary, but will be used in project 3 -Eg. inclusion of server user id in packet header -Eg. reserved fields (may also be used for extra features)

Katz, Stoica F04 33 Project Orion (3)  Use both TCP and UDP sockets -UDP used in control plane (operations that install state, or information, in system) -TCP used in data plane (operations that are performed directly on the data messages)  Constants used in project -found in header file orion.h -orion.h MUST NOT BE CHANGED  Read the specifications carefully, clarify immediately when in doubt  Hopefully, project 3 specs released within 2 weeks’ time -Preferably, extra features should be implemented in project 3 -Spend more time on project 3 (6 weeks to do it)