CH5 TCP Client - Server Example:

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Socket Programming 101 Vivek Ramachandran.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Programming project #4 1 CS502 Spring 2006 Programming Project #4 Web Server CS-502 Operating Systems Spring 2006.
Concurrent vs. iterative servers
1 Nonblocking I/O Nonblocking reads and writes Buffers enabling overlapped nonblocking I/O Nonblocking connect.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
CS-3103 & CS-502, Summer 2006 Programming Project #31 Programming Project #3 Web Server CS-3103 & CS-502 Operating Systems.
File Transfer Protocol (FTP)
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Sirak Kaewjamnong Computer Network Systems
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.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Elementary TCP Sockets
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
System calls for Process management
I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Process Management Azzam Mourad COEN 346.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
TCP Client-Server Example
CSCI 330 UNIX and Network Programming Unit XVI: TCP Server Programming.
Fork(), Concurrent Server, Normal Termination ( 금 ) 김 희 준
1 CSC103: Introduction to Computer and Programming Lecture No 28.
I/O Multiplexing Chap 6. I/O Models  Blocking I/O Model  Nonblocking I/O Model  I/O Multiplexing Model  Signal Driven I/O Model  Asynchronous I/O.
System calls for Process management Process creation, termination, waiting.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Netprog: Client/Server Issues1 Issues in Client/Server Programming Refs: Chapter 27.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
1 Issues in Client/Server Refs: Chapter 27 Case Studies RFCs.
User-Written Functions
Strings CSCI 112: Programming in C.
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Onward with Chat! Networking CS 3470, Section 1.
Threads Threads.
Concurrent vs. iterative servers
Net 323 D: Networks Protocols
UNIX PROCESSES.
CHAPTER 8 ELEMENTARY UDP SOCKETS
Interacting With Protocol Software
Network Programming CSC- 341
Network Programming CSC- 341
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
TCP Sockets Programming
Advanced Network Programming spring 2007
Network Programming Chapter 12
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Net 323 D: Networks Protocols
File Handling.
Issues in Client/Server Programming
TA: Donghyun (David) Kim
TCP Client-Server Example
TCP/IP Sockets in Java: Practical Guide for Programmers
Presentation transcript:

CH5 TCP Client - Server Example: Example: echo client and server 1. Client reads a line of text from standard input and sends to server. 2. Server sends line back to client. 3. Client receives line and displays. fgets writen readline fputs readline writen TCP Client TCP Server fgets and fputs are from standard C I/O library writen and readline are from § 3.9 (lets wait to describe them)

TCP echo Server : Main function:

TCP echo Server : Main function: Lines 9-15: Create a TCP socket. Fill in internet socket address structure with “Wildcard Address “INADDR_ANY” which is 0. We will accept any connection destined for us. Fill in server port with “SERV_PORT”= 9877 defined in unp.h Chosen based on Fig 2.10 Lines 17-18: Server waits until a client connection is complete. Lines 19-24: For each client that connects “fork” spawns a child process. Child process handles the new client. Child closes listening socket but serves the connected socket. In line 24 parent closes connected socket so it can listen for another connection. Child calls function “str_echo” passing the connfd descriptor.

TCP echo SERVER Cont. str_echo Function

TCP echo SERVER Cont. str_echo Function Readline Function This function reads the lines from client and echos back to client. Lines 7-11: The function readline reads the next line from the socket. The function writen sends the line back to client. When the client closes the connection , receipt of FIN at child causes readline to return a 0. This causes str_echo to return which terminates the child. Readline Function [ Fig 3.17 ] This calls C library read function once for every byte of data.

Now Figure 3.17

TCP echo Client : Main Function :

str_cli function:

TCP echo Client : Main Function : str_cli function: Lines 9-13: TCP socket is created and Internet socket address structure is filled with IP address and port number. Servers IP address is taken from the command line argument argv [1] Servers port comes from unp.h “SERV_PORT” = 9877. Lines 14-15: Connect establishes connection with server. Function str_cli reads a line of text from standard input, writes to server, reads back servers echo of line and outputs echoed line to standard output. str_cli function: Lines 6-7: fgets reads a line of text. Writen sends the line to the server.

str_cli function: Cont. Lines 8-10: Readline reads the line echoed back from the server. fputs writes to standard output. Lines 11-12: Loop terminates when fgets returns a null pointer from end of file or an error NOTE: Fgets is a wrappers function! Writen Function: [ Fig. 3.16 ] This writes “n” bytes to a socket

Now Figure 3.16

Now Lets Back Up And Talk About “fork” Section 4.7 Fork Function Process makes a copy of itself so one copy can handle one operation and another copy another operation. Fork is called ONCE but returns TWICE. Returns once in calling process (this is the parent) with process ID of new process. Returns second time in child with value Ø so that return value tells you are parent or child. [Fig. 5.2 again] When connection established accept returns and then the server calls fork. Child process serves client ( str_echo(connfd) ) Parent closes connected socket since child now handles this client.

Now Back To Running Example Normal Start Up

[Figure 5.4] [Figure 5.2] [Figure 5.2]

xxxxxxxxxx (Now Fig 2.4 and 2.5) [from page 128]

Now we need to clean up Zombie Processes Now we need to clean up Zombie Processes. They take up space in the Kernel. With lots of magic the solution is to add line 17 in figure 5.12 ( see book sections 5.9 & 5.10 if you want details ) Now when terminate only have the one ( original ) tcpserv0l still running all Zombies disappear

Figure 5.12 adds line 17 (and one other variable definition) to Fig. 5.2 TCP echo server

Data Format : Passing Text Strings Between Client And Server So far our example server does not really look at the data from the client Modify server to look for two integers separated by a space, server returns sum of the two integers. All we change is the str_echo function. The function sscanf converts two arguments from text strings to long integers. Function snprintf converts to a text string. The only thing we need to change is the str_echo function as shown in Fig 5.17

To Run This Example: . / tcpserv08 & . / tcpcli08 127.0.0.1 1 2 3 4 6 10 1 input error ^ d kill < > ( for . / tcpserv08 ) This works with no problems.

What Happens When We Pass Numbers (Binary) Between Computers? Using the same client and server main functions but change both the called functions to use binary not ASCII.

sscanf converts text to binary ( long )

[ From page 150 ]

Two Common Solutions: Pass all the numeric data as text strings as done in Figure 5.17 str_echo function that adds two numbers. 2. Explicitly define the binary formats (number of bits, big or little endian)