Chapter 04. TCP Server/Client.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Socket Programming Application Programming Interface.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
1 Example (UDP Client) // This program sends UDP packets to the given address #include #define SERVER_ADDR " " #define SERVER_PORT 5555 void error(char.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1 Socket Programming A crash-course in network programming…
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Networking S04, Recitation, Section A
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
Recitation 12: 11/25/02 Outline Socket Interface –Echo Client/Server Http Protocol Evaluation Annie Luo Office Hours: Thursday.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Remote Shell CS230 Project #4 Assigned : Due date :
Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions
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.
Introduction to Socket
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.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
CS 447 Networks and Data Communication Server-Process Organization IP address and SockAddr_In Data Structure Department of Computer Science Southern Illinois.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 Vehicle Networking Networks Instruction 1 – Echo client/server in C Jeroen Voeten ES, 2012.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Sockets and Beginning Network Programming
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Review: TCP Client-Server Interaction
Recitation 11 – 4/29/01 Outline Sockets Interface
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP Sockets Programming
28.
Socket 程式設計.
Chapter 06. UDP Server/Client.
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Socket Programming Neil Tang 09/08/2008
Example (UDP Client) // This program sends UDP packets to the given address #include #include #include #include.
Internet Networking recitation #8
Sockets.
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Chapter 04. TCP Server/Client

Basic structure and principle of TCP server/client Goal Basic structure and principle of TCP server/client Socket system call for TCP application Understanding application protocol

TCP server/client operation (1/6) TCP server/client example GET / HTTP/1.1 Accept: image/gif, ... <HTML> <HEAD>...</HEAD>... Web server Web client Web client

TCP server/client operation (2/6) TCP client listen accept recv send connect network

TCP server/client operation (3/6) TCP server/client socket calls (cont’d) - bind : to assign a name to an unnamed socket. - listen: to indicate that it is willing to receive connections. - connect: to establish a connection with a server - send: to send data to peer side - accept: to accept the connection request from client - recv: to receive the data that client sent

TCP server/client operation(4/6) TCP server/client operation principle TCP server waiting TCP server TCP clients #1 Client connecting

TCP server/client operation(5/6) TCP server/client operation principle (cont’d) TCP server TCP client #1 Comm. waiting TCP server TCP client #1 client #2 Comm. waiting

TCP server/client operation(6/6) TCP server/client operation principle (cont’d) TCP server TCP client #1 waiting client #n . . .

TCP server/client example Code Example(TCPServer.cpp, TCPClient.cpp) TCP client TCP server fgets() send() printf() recv()

TCP server/client analysis (1/2) Socket requires three components ① protocol Defined by Socket() system call ② local IP address and port number Server or client side ③ remote IP address and port number

TCP server/client analysis (2/2) Socket data structure server local IP addr. Local port num. Remote IP addr. Remote port num. client application OS network • • •

Socket call for TCP server (1/8) TCP server side socket() bind() recv() send() closesocket() TCP server TCP client connect() listen() accept() network

Socket call for TCP server (2/8) bind() Assign a name to an unnamed socket Define IP address and port number of server side int bind ( SOCKET s, const struct sockaddr* name, int namelen ) ; success: 0, fail: SOCKET_ERROR

Socket call for TCP server (3/8) bind() example 050 SOCKADDR_IN serveraddr; 051 ZeroMemory(&serveraddr, sizeof(serveraddr)); 052 serveraddr.sin_family = AF_INET; 053 serveraddr.sin_port = htons(9000); 054 serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); 055 retval = bind(listen_sock, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); 056 if(retval == SOCKET_ERROR) err_quit("bind()");

Socket call for TCP server (4/8) listen() transit TCP port state to LISTENING state - ready to receive connections. int listen ( SOCKET s, int backlog ) ; success: 0, fail: SOCKET_ERROR

Socket call for TCP server (5/8) listen() example 059 retval = listen(listen_sock, SOMAXCONN); 060 if(retval == SOCKET_ERROR) err_quit("listen()");

Socket call for TCP server (6/8) accept() accept the connection request from client tell the IP address and port number of client SOCKET accept ( SOCKET s, struct sockaddr* addr, int* addrlen ) ; success: new socket, fail: INVALID_SOCKET

Socket call for TCP server (7/8) accept() example 062 // variables for data communiction 063 SOCKET client_sock; 064 SOCKADDR_IN clientaddr; 065 int addrlen; ... 068 while(1){ 069 // accept() 070 addrlen = sizeof(clientaddr); 071 client_sock = accept(listen_sock, (SOCKADDR *)&clientaddr, &addrlen); 072 if(client_sock == INVALID_SOCKET){ 073 err_display("accept()"); 074 continue; 075 }

Socket call for TCP server (8/8) accept() example (cont’d) 076 printf("\n[TCP server] client connection: IP addr=%s, port num=%d\n", 077 inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); 078 079 // data comm with client 080 while(1){ ... 101 } 102 103 // closesocket() 104 closesocket(client_sock); 105 printf("[TCP server] client exit: IP addr=%s, port num=%d\n", 106 inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); 107 }

Socket call for TCP client (1/3) TCP client side TCP server TCP client socket() socket() bind() listen() network accept() connect() recv() send() send() recv() closesocket() closesocket()

Socket call for TCP client (2/3) connect() to establish a connection with a server int connect ( SOCKET s, const struct sockaddr* name, int namelen ) ; success: 0, fail: SOCKET_ERROR

Socket call for TCP client (3/3) connect() example 070 SOCKADDR_IN serveraddr; 071 serveraddr.sin_family = AF_INET; 072 serveraddr.sin_port = htons(9000); 073 serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 074 retval = connect(sock, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); 075 if(retval == SOCKET_ERROR) err_quit("connect()");

Data transfer socket call (1/10) Socket data structure server Local IP addr Local port num Remote IP addr Remote port num client application OS network • • • Recv buffer Send buffer

Data transfer socket call (2/10) send() 함수 Data transfer to peer side int send ( SOCKET s, const char* buf, int len, int flags ); success: num of sent bytes, fail: SOCKET_ERROR

Data transfer socket call (3/10) recv() Data receving from peer side int recv ( SOCKET s, char* buf, int len, int flags ); success: num of received bytes or 0(the remote side has closed the connection ), fail: SOCKET_ERROR

Data transfer socket call (4/10) recvn() function 037 int recvn(SOCKET s, char *buf, int len, int flags) 038 { 039 int received; 040 char *ptr = buf; 041 int left = len; 042 043 while(left > 0){ 044 received = recv(s, ptr, left, flags); 045 if(received == SOCKET_ERROR) 046 return SOCKET_ERROR; 047 else if(received == 0) 048 break; 049 left -= received; 050 ptr += received; 051 } 052 053 return (len - left); 054 }

Data transfer socket call (5/10) recvn() function principle buf ptr left len 읽은 데이터

Data transfer socket call (6/10) Data send/recv example – TCP client 078 char buf[BUFSIZE+1]; 079 int len; ... 082 while(1){ 083 // data input 084 ZeroMemory(buf, sizeof(buf)); 085 printf("\n[sending data] "); 086 if(fgets(buf, BUFSIZE+1, stdin) == NULL) 087 break; 088 089 // '\n' char delete 090 len = strlen(buf); 091 if(buf[len-1] == '\n') 092 buf[len-1] = '\0'; 093 if(strlen(buf) == 0) 094 break;

Data transfer socket call (7/10) Data send/recv example – TCP client (cont’d) 096 // data sending 097 retval = send(sock, buf, strlen(buf), 0); 098 if(retval == SOCKET_ERROR){ 099 err_display("send()"); 100 break; 101 } 102 printf("[TCP client] %d bytes sent...\n", retval); 103 104 // data receiving 105 retval = recvn(sock, buf, retval, 0); 106 if(retval == SOCKET_ERROR){ 107 err_display("recv()"); 108 break; 109 } 110 else if(retval == 0) 111 break;

Data transfer socket call (8/10) Data send/recv example – TCP client(cont’d) 113 // print received data 114 buf[retval] = '\0'; 115 printf("[TCP client] %d bytes received...\n", retval); 116 printf("[received data] %s\n", buf); 117 }

Data transfer socket call (9/10) Data send/recv example– TCP server 066 char buf[BUFSIZE+1]; ... 080 while(1){ 081 // data receiving 082 retval = recv(client_sock, buf, BUFSIZE, 0); 083 if(retval == SOCKET_ERROR){ 084 err_display("recv()"); 085 break; 086 } 087 else if(retval == 0) 088 break; 089 090 // print received data 091 buf[retval] = '\0'; 092 printf("[TCP/%s:%d] %s\n", inet_ntoa(clientaddr.sin_addr), 093 ntohs(clientaddr.sin_port), buf);

Data transfer socket call (10/10) Data send/recv example– TCP server(cont’d) 095 // data sending 096 retval = send(client_sock, buf, retval, 0); 097 if(retval == SOCKET_ERROR){ 098 err_display("send()"); 099 break; 100 } 101 }

Application protocol and message design (1/3) Define data format and semantic which is exchanged in application level Application protocol example network

Application protocol and message design (2/3) Message definition ① Message definition ② struct DrawMessage1 { int x1, y1; // line starting point int x2, y2; // line ending point int width; // line width int color; // line color }; struct DrawMessage2 { int x1, y1; // circle center int r; // circle radius int fillcolor; // internal color int width; // line width int color; // line color };

Application protocol and message design (3/3) Message definition ③ struct DrawMessage1 { int type; // = LINE int x1, y1; // line starting point int x2, y2; // line starting point int width; // line width int color; // line color }; struct DrawMessage2 int type; // = CIRCLE int x1, y1; // circle center int r; // circle radius int fillcolor; // internal color int width; // line width int color; // line color

Application protocol and message design (3/3) FileSender.cpp, FileReceiver.cpp