DOC PREVIEW
GT ECE 4110 - INTRODUCTION UNIX NETWORK PROGRAMMING

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 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 39 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 39 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 39 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 39 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 39 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 39 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 39 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

INTRODUCTION“UNIX NETWORK PROGRAMMING” Vol 1, Third Edition by Richard StevensAside on compiling programs in linux:Linux Configuration Tip:Slide 4Slide 5Slide 6Slide 7Continued:Slide 9Slide 10Simple Daytime ServerSlide 12Slide 13Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 391 INTRODUCTION“UNIX NETWORK PROGRAMMING” Vol 1, Third Edition by Richard Stevens ServerClientCommunicationsEx: TCP/IPExample: Telnet client on local machine to Telnet server on a remote machineClient and server are “user” processes TCP and IP are normally part on the “kernel” protocol stack Read: Chapters 1,2, 3, 42Aside on compiling programs in linux:To compile a program in Linux (not using make) for example the program intro/byteorder.c on p. 78gcc byteorder.cThis places the executable in a default file a.out gcc -o byteorder byteorder.cPlaces executable in byteorder ; can run by . / byteorder3Linux Configuration Tip:Instead of having to type . / program every time you run a “program”what you can do is in .bash_profile in your /root directory add to the path line. Path  . :}Add thisNow you can just type program instead of ./program since . is now included in your path(From a security standpoint this is a bad thing to do, thus it is not already there for you.)On another Linux subject:To switch from one default text login screen to another (not in the graphics GUI x windows) Alt F1 Default screen Alt F2 A second screen Alt F3 A third screen etc4Now Figure 2.18 page 615Example: a simple daytime client:What this example client program does is connect to a server and gets the date and time if we run:. / daytimetcpcli < reachable IP address> <for example 127.0.0.1>Gets date & time back printed out on screen (However this will not run unless we have a daytime server running on port 13. We can do this by using server code later in this lecture or by in a config file turning on the daytime daemon which is off by default in our linux install.)Details:Line 1 : Includes a bunch on code (unp.h) in appendix D.1 (pages 899-912) with commonly used stuff like MAXLINE (on line 6 of program).Line 2-3: Allows values to be passed to our program for example an IP address is considered to be the second value.678Continued:Line 8 : If you do not have an IP value stop.Line 10-11: Your first use of the sockets application programming interface calling the function “socket.”•This creates an Internet (AF_ INET) stream (SOCK_STREAM) socket  means TCP socket.•Function returns an integer descriptor used to identify this particular socket in later code.•Failure calls a function err_sys in our unp.h code Line 12: Function bzero clears out by filling with zeros the structure servaddrLine 13: We fill in the structure member servaddr.sin_family with the type AF_INET meaning internet type.Line 14: We set servaddr.sin_port to the port number 13. This is “well-known” daytime port on TCP/IP hosts.[ Fig 2.13 ] Call function htons “ Host To Network Short” to convert the decimal value into format we need.9Line 15-16 : Call function inet_pton “Presentation to numeric” to convert ASCII value input in command line to format needed. Put result in servaddr.sin_addr If return error value of a negative number, call err_sys functionLine 17-18 : Connect function establishes TCP connection with server specified by the socket address structure pointed to by &servaddr which we set in line 15Lines 19-25 : Read server’s reply to an enquiry on port 13, display result using function fputs TCP is a BYTE-Stream Protocol with no “record” boundaries. Therefore for TCP socket always need to code read in a loop which terminates on 0 or end. Line 26: Exit terminates program. TCP socket is closed automatically10Error Handling: Stevens defines wrapper functions to perform function call, test of return value and error handling.Stevens indicates one of his wrapper functions with an upper case letter.11Simple Daytime ServerTo run this server and then client in directory with the source code.. /daytimetcpsrv & puts in background mode. /daytimetcpcli 127.0.0.1ps shows . / daytimetcpsrv is runningkill <number>ps shows no longer running121314 Line 10: Creates the TCP socket. Now we are using the wrapper function with a capital S. Instead of sockfd we put returned integer descriptor in listenfd. Otherwise identical to Fig 1.5 client code line 10.Line 11-15: Bind the servers port 13 to the socket by filling in internet socket address structure.Line 11: Function bzero clears out by filling with zeros the structure servddr.Line 12: Fill in the structure member servaddr.sin_family with type AF_INET meaning internet type.Line 13: Fill in the structure member servaddr.sin_addr.s_addr with the IP address INADDR _ ANY This allows server to accept a client connection on any inter- face. Use function htonl which is host to network long to convert declimal value into format needed.Line 14: Set structure member servaddr.sin_ port to port number 13Line 15: Use function bind on socket pointed to by listenfd to assign a local protocol address to the socket.15Line 16: Using the function listen the socket is converted into one that accepts incoming connections from clients. The Three Steps:•Socket•Bind•Listen•Are used to prepare a TCP server “Listening Descriptor” which is listenfdin our example.•The constant LISTENQ is from header “unp.h” and specifies max number of clientconnections.Line 17-21:For ( ; ; ) { } This loops foreverWe wait at line 18 until a client has connected to us and the TCP 3 way handshake has occurred.=> Function accept returns description connfd which is the “connected descriptor.” Line 19: Function time returns time.Line 20: Function ctime converts to human readable form. snprintf adds a return and line feed.Line 21: Function write sends result back to client.Line 22: Server closes connection with client by calling close.16Internet Requests For Comments (RFC)RFC documents are definitions


View Full Document

GT ECE 4110 - INTRODUCTION UNIX NETWORK PROGRAMMING

Documents in this Course
PUSH Flag

PUSH Flag

17 pages

Ethernet

Ethernet

33 pages

Load more
Download INTRODUCTION UNIX NETWORK PROGRAMMING
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 INTRODUCTION UNIX NETWORK PROGRAMMING 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 INTRODUCTION UNIX NETWORK PROGRAMMING 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?