Client software design

Similar documents
The User Datagram Protocol

NETWORKS (CONT D) RELATION TO OTHER PROTOCOLS

UDP CONNECT TO A SERVER

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

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

Application Programming Interfaces

Elementary TCP Sockets

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

CS4700/CS5700 Fundamentals of Computer Networking

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Network Communication

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

CS321: Computer Networks Introduction to Application Layer

Tutorial on Socket Programming

Introduction to Socket Programming

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

CSE 333 Section 8 - Client-Side Networking

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

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

TCP: Three-way handshake

CSE 124 Discussion Section Sockets Programming 10/10/17

CS321: Computer Networks Socket Programming

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

Programming with TCP/IP. Ram Dantu

EEC-484/584 Computer Networks

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

Socket Programming for TCP and UDP

ICT 6544 Distributed Systems Lecture 5

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

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

EE 122: Sockets. Motivation. Sockets. Types of Sockets. Kevin Lai September 11, 2002

Network programming(i) Lenuta Alboaie

Group-A Assignment No. 6

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

A. Basic Function Calls for Network Communications

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

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

Communication. Sockets (Haviland Ch. 10)

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

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

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

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

Unix Network Programming

Ά η η 1 (30%): Sockets. socket () bind () listen () accept () connect () read () write () close ()

CLIENT-SIDE PROGRAMMING

CS4514 B08 HELP Session 1

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

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

Piotr Mielecki Ph. D.

Cracking WEP Keys Using WEPCrack

UNIT IV- SOCKETS Part A

Socket Programming TCP UDP

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

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

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

STUDY OF SOCKET PROGRAMMING

Department of Computer Science

CS 3516: Computer Networks

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

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

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

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

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

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

Applications and Layered Architectures. Chapter 2 Communication Networks Leon-Garcia, Widjaja

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Introduction to Client-Server Model

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Computer Networks Prof. Ashok K. Agrawala

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

CSE 333 SECTION 7. Client-Side Network Programming

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

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

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

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

CSE 333 SECTION 8. Sockets, Network Programming

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Sockets Sockets Communication domains

ECE 435 Network Engineering Lecture 2

Lab 0. Yvan Petillot. Networks - Lab 0 1

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

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

System Programming. Sockets

Network programming(ii) Lenuta Alboaie

Client/Server. Networking Approach.

Introduction to Network Programming using C/C++

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Socket Programming(2/2)

Sockets 15H2. Inshik Song

C Structures in Practice

SOCKETS. COMP750 Distributed Systems

The client-server model

// socket for establishing connections

Transcription:

Client software design Stefan D. Bruda Winter 2018

A TCP CLIENT 1 Get the IP address and port number of the peer 2 Allocate a socket 3 Choose a local IP address 4 Allow TCP to choose an arbitrary, unused port number 5 Connect the socket to the server 6 Communicate with the server Exchange messages Often the client sends requests and the server replies, but this is not always the case The message exchange happens according to the application-level protocol 7 Close connection Client software design (S. D. Bruda) Winter 2018 1 / 14

PEER IDENTIFICATION Depending on the actual application, the IP address of the peer (i.e., server) can be specified in more than one ways, including: Hardcoded (rarely) We specify it directly as an integer As command-line argument (read from configuration file, etc.) We use gethostbyname to get the actual address (i.e., number) Use a separate protocol (broadcast or multicast) to find a server Ports can also be specified in many ways, including: It is a well-known port We use getservbyname to obtain the actual port number Hardcoded Possibly suitable for custom client-server applications As command-line argument (read from configuration file, etc.) Especially useful for parameterized clients telnet linux.ubishops.ca 22 my-client linux.ubishops.ca ssh Client software design (S. D. Bruda) Winter 2018 2 / 14

ALLOCATE A SOCKET We need to specify at allocation time: The protocol family The socket type (TCP for the time being) #include <sys/types.h> #include <sys/socket.h> int sd = socket(pf_inet, SOCK_STREAM, 0); We end up with a socket descriptor Client software design (S. D. Bruda) Winter 2018 3 / 14

CHOOSING A LOCAL IP ADDRESS Why do we need the local IP address? Client software design (S. D. Bruda) Winter 2018 4 / 14

CHOOSING A LOCAL IP ADDRESS Why do we need the local IP address? Because a connection is specified by two endpoints Why is it a problem to choose a local IP address? Client software design (S. D. Bruda) Winter 2018 4 / 14

CHOOSING A LOCAL IP ADDRESS Why do we need the local IP address? Because a connection is specified by two endpoints Why is it a problem to choose a local IP address? Because a machine might have multiple adresses Internet 216.95.151.69 (ppp0) 10.0.0.1 (br0) 10.0.1.2 (en1) The appropriate address must be chosen so that IP is able to route packets in the right direction Choosing the right IP address is done after a dialogue with IP The system call connect does it for us Client software design (S. D. Bruda) Winter 2018 4 / 14

CHOOSE A PORT We must specify a local port number for the same reasons we have to specify a local address The choice of port number does not matter as long as: It does not conflict with the port assigned to a well-know service It is not in use by another process We could try at random until we get a free port...... However, the system keeps track of port usage anyway, so this would be overkill Thus the port number choice is again taken care of by the call to connect Client software design (S. D. Bruda) Winter 2018 5 / 14

CONNECT TO THE SERVER In all, we obtain the local coordinates (IP address, port) and we connect in one step: int connect(int sockfd, struct sockaddr *serv_addr, socklen_t addrlen); Something like this: struct sockaddr_in sin; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { perror("connect"); exit(1); Client software design (S. D. Bruda) Winter 2018 6 / 14

FILLING IN THE SERVER ADDRESS int connectbyportint(const char* host, const unsigned short port) { struct hostent *hinfo; struct sockaddr_in sin; const int type = SOCK_STREAM; int sd; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; hinfo = gethostbyname(host); if (hinfo == NULL) return err_host; sin.sin_addr=(unsigned int)hinfo->h_addr; sin.sin_port = port; sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { close(sd); return err_connect; return sd; Client software design (S. D. Bruda) Winter 2018 7 / 14

FILLING IN THE SERVER ADDRESS int connectbyportint(const char* host, const unsigned short port) { struct hostent *hinfo; struct sockaddr_in sin; const int type = SOCK_STREAM; int sd; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; hinfo = gethostbyname(host); if (hinfo == NULL) return err_host; sin.sin_addr=(unsigned int)hinfo->h_addr; sin.sin_port = port; // doubleplus ungood // doubleplus ungood sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { close(sd); return err_connect; return sd; Client software design (S. D. Bruda) Winter 2018 8 / 14

FILLING IN THE SERVER ADDRESS int connectbyportint(const char* host, const unsigned short port) { struct hostent *hinfo; struct sockaddr_in sin; const int type = SOCK_STREAM; int sd; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; hinfo = gethostbyname(host); if (hinfo == NULL) return err_host; sin.sin_addr=(unsigned int)htonl(hinfo->h_addr); sin.sin_port = (unsigned short)htons(port); sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { close(sd); return err_connect; return sd; Client software design (S. D. Bruda) Winter 2018 9 / 14

FILLING IN THE SERVER ADDRESS int connectbyportint(const char* host, const unsigned short port) { struct hostent *hinfo; struct sockaddr_in sin; const int type = SOCK_STREAM; int sd; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; hinfo = gethostbyname(host); if (hinfo == NULL) return err_host; memcpy(&sin.sin_addr, hinfo->h_addr, hinfo->h_length); sin.sin_port = (unsigned short)htons(port); sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { close(sd); return err_connect; return sd; Client software design (S. D. Bruda) Winter 2018 10 / 14

COMMUNICATE WITH THE SERVER We send data using send (or write) We receive responses using recv (or read) Note that the response could come in pieces, even if the server answers back in large chunks You should be prepared to accept data a few bytes at a time const int ALEN = 128; char* req = "some sort of request"; char ans[alen]; char* ans_ptr = ans; int ans_to_go = ALEN, n = 0; send(sd,req,strlen(req),0); while ( ( n = recv(sd,ans_ptr,ans_to_go,0) ) > 0 ) { ans_ptr += n; ans_to_go -= n; Client software design (S. D. Bruda) Winter 2018 11 / 14

COMMUNICATE WITH THE SERVER (CONT D) We do not necessarily know how long is the response The shape of the response varies according to the application-level protocol and may be: One line of text (terminated by \n ) We use readline (or equivalent) to read the answer One line of text determines what comes after it Again, we use readline to read one line at a time, and then decide what to do next based on what we just read As much as the server cares to send, with no special end marker We read until there is no more data But how? Client software design (S. D. Bruda) Winter 2018 12 / 14

COMMUNICATE WITH THE SERVER (CONT D) We check whether we have any more data coming on our way Communication is not instantaneous, so we have to give some time for the data to arrive const int recv_nodata = -2; int recv_nonblock (int sd, char* buf, size_t max, int timeout) { struct pollfd pollrec; pollrec.fd = sd; pollrec.events = POLLIN; int polled = poll(&pollrec,1,timeout); if (polled == 0) return recv_nodata; if (polled == -1) return -1; return recv(sd,buf,max,0); Outcomes: -2: no more data available within the given timeout 0: end of file (when the server closes connection on us) n > 0: n characters have been read. Client software design (S. D. Bruda) Winter 2018 13 / 14

CLOSING THE CONNECTION close closes the connection and destroys the socket Sometimes we want to shut down communication in one direction only Reason: the server receive a request and responds to it But what does it do now with the connection? If the client has in fact more requests, the connection should stay open If this is the last request, the connection should be closed A client (or server) can partially close a connection, to let the server know that it is finished. int err = shutdown(sd,shut_wr); The server (client) will then receive and end of file The second argument of shutdown can be SHUT_RD (0): further receives will be disallowed SHUT_WR (1): further sends will be disallowed SHUT_RDWR (2): neither receives, nor sends will be allowed Client software design (S. D. Bruda) Winter 2018 14 / 14