Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Similar documents
Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

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

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

CSE 124 Discussion Section Sockets Programming 10/10/17

CS4700/CS5700 Fundamentals of Computer Networking

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

Unix Network Programming

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

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

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

Programming with TCP/IP. Ram Dantu

Socket Programming for TCP and UDP

TCP: Three-way handshake

Socket Programming TCP UDP

Network programming(ii) Lenuta Alboaie

Tutorial on Socket Programming

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

Network programming(i) Lenuta Alboaie

CS321: Computer Networks Socket Programming

System Programming. Sockets

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

CS321: Computer Networks Introduction to Application Layer

UNIT IV- SOCKETS Part A

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 3516: Computer Networks

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Chapter 6. The Transport Layer. Transport Layer 3-1

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

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

EEC-484/584 Computer Networks

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

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

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSE 333 SECTION 8. Sockets, Network Programming

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

Elementary TCP Sockets

CS 640: Computer Networking

CLIENT-SIDE PROGRAMMING

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Introduction to Socket Programming

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

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

STUDY OF SOCKET PROGRAMMING

The User Datagram Protocol

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

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

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Network Programming Week #1. K.C. Kim

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

What s an API? Do we need standardization?

A Client-Server Exchange

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

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

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

ECE 435 Network Engineering Lecture 2

UDP CONNECT TO A SERVER

CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

A. Basic Function Calls for Network Communications

TCP Network Programming in C

Lab 0. Yvan Petillot. Networks - Lab 0 1

Communication. Sockets (Haviland Ch. 10)

ICT 6544 Distributed Systems Lecture 5

Department of Computer Science

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

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

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

Client Server Computing

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

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

Introduction to Socket Programming

Group-A Assignment No. 6

SOCKETS. COMP750 Distributed Systems

CSE 333 Lecture 16 - network programming intro

Socket Programming(2/2)

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

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

CSE 333 SECTION 6. Networking and sockets

Linux Network Programming, Part 1

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

Network Communication

The Socket Interface. Socket and Socket Library

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

ECE 435 Network Engineering Lecture 2

Sockets 15H2. Inshik Song

Network Programming and Protocl Design. Nixu Ltd.

Transcription:

Followup symbolic link (soft link): pathname, can be across file systems, replacement of file will be active on all symbolic links, consumes at least an inode. hard link: pointers to an inode, only in same file system, can t replace original file (replacing it will still keep the original file, hard link points to original file s inode, new file has a new inode). Lecture 7 Socket Programming January 28, 2005 Review Talked about Process management Thread management Inter-process communication Synchronization File systems Focus on single-machine issues. Now: distributed systems issues. Communication Interface Specify local/remote communication endpoints. Initiate a connection. Wait for a connection. Send/receive data. Handle incoming data. Terminate connection gracefully. Handle connection termination from remote site. Abort communication. Handle error conditions or connection abort. Allocate and release local resources. Socket Communication Client-Server Model File I/O: open, close, read, write, seek, fcntl,... Network communication: developers extended set of file descriptors to include network connections. extended read/write to work on these new file descriptors. but other required functionality did not fit into the openread-write-close paradigm. -> Socket API Server listens for requests from clients Server: passive open Client: active open request response Example: file server web server print server mail server name server CLIENTS X window server SERVER 1

Socket Creation int socket(int family, int type, int protocol); return value: socket descriptor (like file descriptor) int s; s = socket (PF_INET, SOCK_STREAM, 0); Socket Creation Domain: PF_INET, PF_UNIX,... Type: datagram UDP SOCK_DGRAM reliable stream TCP SOCK_STREAM raw IP SOCK_RAW Protocol: typically 0 = default protocol for type Endpoint Addresses Different protocols may have different representations. TCP/IP uses combination of IP address and port. Sockets offer different address families, TCP/IP uses a single address representation (AF_INET). PF_INET, AF_INET often confused, use same numeric value (2). Now: #define PF_INET AF_INET (Linux) Socket Addresses struct sockaddr { /* sys/socket.h */ sa_family_t sa_family; /* address family */ char sa_data[14]; /* addressing information */ struct sockaddr_in { short sin_family; /* AF_INET */ u_short sin_port; /* network byte order */ struct sin_addr sin_addr; /* network address */ Binding the Local Address int bind(int s, struct sockaddr *addr, int addresslen); Socket has no notion of endpoint addresses (neither local nor remote). bind(): specify local endpoint. Binding the Local Address #include <netinet/in.h> int s; s = socket(pf_inet, SOCK_DGRAM, 0); memset((char *)&sin, 0, sizeof(sin)); sin.sin_port = htons (6000); /* 0 -> let system choose */ sin.sin_addr.s_addr = htonl(inaddr_any); /* allow any interface */ bind (s, (struct sockaddr *)&sin, sizeof(sin)); 2

setsockopt()/getsockopt() A server waits 2 MSL (maximum segment lifetime) for old connection. If not properly terminated, bind() will return EADDRINUSE. setsockopt(int s, int level, int optname, const void *optval, int optlen); getsockopt(int s, int level, int optname,void *optval, int *optlen); More Socket Options SO_ERROR get error status SO_KEEPALIVE send periodic keep-alives SO_LINGER close() + non-empty buffer SO_SNDBUF send buffer size SO_RCVBUF receive buffer size Before bind: setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); Allows re-use of port in use. Linger shutdown Closing: struct linger { int l_onoff; /* 0:off, else on */ int l_linger; /* seconds */ l_onoff l_linger behavior 0 ignored close returns immediately 1 0 abort & flush, send RST 1 >0 sleep until data ack ed shutdown (int s, int how); how = 0: no more receives how = 1: no more sending how = 2: neither (close();) Overview SERVER socket() bind() accept() blocks until connection from client connection establishment CLIENT socket() int connect(int s, struct sockaddr *addr, int namelen); Client issues to establish remote address and port establish connection Fails if host not listening to port. read() data request write() process request write() data reply read() 3

int listen(int s, int backlog); Only for stream sockets. Socket being listened to can t be used for client. does not wait for connections, but needed to receive connection attempts. Completes 3-way handshake. Allows backlog pending connection requests (SYN, completed 3WH) while waiting for accept(). Queue full: SYN requests are silently dropped. SYN-flood : send fake TCP SYN requests to fill queue. TCP Review client port=64444 active open (9, 64444) active close close() SYN k ACK j+1 SYN j ACK k+1 FIN ACK server port=9 (64444, 9) (64444, 9) accept() int accept(int s, struct sockaddr *addr, int *addrlen); By connection-oriented server, after. Can t preview connection: accept and close. Returns a new socket. Returns address of client. Sending/Receiving Example: TCP Client read/write: send/receive, no explicit address send/recv: send/receive, flags sendto: specify destination explicitly recvfrom: also receive address of peer sendmsg: msghdr data structure, scatter/gather recvmsg: msghdr data structure, scatter/gather #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> extern struct in_addr host2ip (char *host); int main(int argc, char *argv[]) { int s, n; char msg[80] = Hello, World! ; if ((s = socket(pf_inet, SOCK_STREAM, 0)) < 0) { perror( socket ); return(-1); 4

Example: TCP Client sin.sin_port = htons(atoi(argv[2])); sin.sin_addr = host2ip(argv[1]); if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { perror( connect ); return -1; if (write(s, msg, strlen(msg)+1) < 0) { perror( write ); return -1; if ((n = read(s, msg, sizeof(msg))) < 0) { perror( read ); return -1; printf( %d bytes: %s\n, n, msg); if (close(s) < 0) { perror( close ); return -1; return 0; Single-Threaded Server int server; server = initialize(); while (1) { wait_for_request(server); read_request; send_answer; Example: TCP Server int main(int argc, char *argv[]) { int s, t, n; char *r; int sinlen; if ((s = socket(pf_inet, SOCK_STREAM, 0)) < 0) { perror( socket); return -1; sin.sin_port = htons(13333); sin.sin_addr.s_addr = INADDR_ANY; if (bind (s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { perror( bind ); return -1; if (listen(s, 5) < 0) { perror( listen ); return -1; Example: TCP Server for (;;) { sinlen = sizeof(sin); if ((t = accept(s, (struct sockaddr *) &sin, &sinlen)) < 0) { perror( accept ); return -1; r = gettime(); if (write(t, r, strlen(r)) < 0) { perror ( write ); return -1; if (close(t) < 0) { perror( close ); return -1; if (close(s) < 0) { perror( close ); return -1; 5