DOC PREVIEW
GT ECE 4110 - TCP Client - Server Example

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 321CH5 TCP Client - Server Example:Example: echo client and server1. 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 writenfgets and fputs are from standard C I/O librarywriten and readline are from § 3.9 (lets wait to describe them) TCPServer TCPClient2TCP echo Server :Main function:34TCP 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.10Lines 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.56TCP echo SERVER Cont.str_echo Function7str_echo 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.TCP echo SERVER Cont.8Now Figure 3.179TCP echo Client :Main Function :10str_cli function:11TCP echo Client :Main 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.12str_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 socket13Now Figure 3.1614Now 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.15Now Back To Running ExampleNormal Start Up16[Figure 5.4][Figure 5.2][Figure 5.2]171819[from page 128](Now Fig 2.4 and 2.5)xxxxxxxxxx20212223• 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 disappear24Figure 5.12 adds line 17 (and one other variable definition) to Fig. 5.2 TCP echo server25Data 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.172627To Run This Example:. / tcpserv08 &. / tcpcli08 127.0.0.11 234 6101input error^ dkill < > ( for . / tcpserv08 )This works with no problems.28What 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.29sscanf converts text to binary ( long )3031[ From page 150 ]32Two Common Solutions:1. 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


View Full Document

GT ECE 4110 - TCP Client - Server Example

Documents in this Course
PUSH Flag

PUSH Flag

17 pages

Ethernet

Ethernet

33 pages

Load more
Download TCP Client - Server Example
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view TCP Client - Server Example and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view TCP Client - Server Example 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?