Network Programming Appendix Network Programming Labs Tornado Training Workshop Copyright E-41L
Note to Windows Teams The networking programming labs ask you to compile and run some programs on the UNIX host Server. The advanced labs also ask you to do some programming under UNIX. To do these things you will need to telnet to Server, logging in as teamn (with password teamn). You may also want to add Server s name and IP address to the host tables on your target as you did in lab 12. You may use a UNIX editor such as vi to edit files, or you may do your editing in the Windows environment and ftp the source files across. You will only have to write the generic network programming part of the UNIX server or client programs; the UNIX specific parts of the programs have been done for you. The labs tell you the appropriate commands to use for compilation. Of course, if you have any trouble while using UNIX, ask your instructor for help. Tornado Training Workshop Copyright E-42L
UDP Socket Programming Objective: To communicate between two nodes on a network, using UDP sockets. A VxWorks UDP Client We will start a UDP server on a UNIX host. From the VxWorks shell, we will implement the client interface. For the client interface, we will need to: Create a UDP socket. Create a message to send to the server. Initialize a sockaddr_in structure, containing the server s address. Send the server our message. Read the server s reply. Since we won t bind our client s socket to a port, the network software will dynamically assign a port number (in the range 1024-5000) to our socket. 1. Change directories to HomeDir/a_netProg/udp. Windows teams telnet to Server, and change directory to /u/teamn/ a_netprog/udp on Server, also: % cd ~/a_netprog/udp 2. Start the UNIX UDP server. You may wish to examine the source code in unixserver.c. The program creates a UDP socket and binds it to a specific port. It then sits in an infinite loop, reading requests from its socket and sending back replies. The syntax for the program is: % unixserver portnumber (The % is a generic UNIX prompt, which you do not need to type in! Your prompt may look different. :-) Tornado Training Workshop Copyright E-43L
To keep team separated, use the following port numbers: Team Port Numbers 1 5100-5199 2 5200-5299 3 5300-5399...... 8 5800-5899 % unixserver 5101 The prompt will not return until you terminate the server by typing ^C. Leave the server running for now. 3. Create the client socket. Family or domain (PF_INET) Type (SOCK_DGRAM) Team 1 port number shown. Protocol (IPPROTO_IP) -> sock = socket (2, 2, 0) new symbol "sock" added to symbol table. sock = 0xb6f98: value = 7 = 0x7 4. Initialize a pointer to a string to send to the UNIX server. -> str = "This is a test message\n" new symbol "str" added to symbol table. str = 0xb6f78: value = 749440 = 0xb6f80 = str + 0x8 -> len = strlen (str) + 1 new symbol "len" added to symbol table. len = 0xb6f70: value = 24 = 0x18 5. We must now initialize a structure containing the server s address. Allocate memory to hold a sockaddr_in structure (16 bytes). -> pdestaddr = malloc (16) new symbol "pdestaddr" added to symbol table. pdestaddr = 0xb6f68: value = 4193496 = 0x3ffcd8 6. Load sockhelp.o, which has utilities to initialize and display a sockaddr_in structure. Tornado Training Workshop Copyright E-44L
-> ld < sockhelp.o value = 688304 = 0xa80b0 7. Initialize pdestaddr. The pdestaddr members should be assigned as follows: sin_family AF_INET (2) sin_addr The server s internet address. sin_port The server s port number. sin_zero Set to 0. The sockaddrprompt( ) routine: Automatically initializes the sin_family structure member to AF_INET (2). Prompts for and initializes sin_addr to the internet address provided. The address can be specified in dot notation or by providing a host name, if the host is a known host see hostadd( ) and hostshow( ). Prompts for and initializes sin_port to the port number provided. Zeroes the sin_zero member. When prompting for a value, the current values are shown in brackets. Type a <CR> to leave the value unchanged. Enter Server or ServerIP for the sin_addr field, and for sin_port, use the same port number on which you started unixserver waiting for requests. Example for team1 using server isis: -> sockaddrprompt pdestaddr sin_addr -- Enter inet address or host name [0.0.0.36]: isis sin_port -- Enter port number [36720]: 5101 value = 65535 = 0xffff To verify the values, use: -> sockaddrshow pdestaddr value = 19 = 0x13 sin_family = 2 sin_addr = 147.11.12.150 sin_port = 5101 Tornado Training Workshop Copyright E-45L
8. Send str to the server. Pointer to data to be sent Sender s socket descriptor Number of bytes of data to send Option flags Pointer to sockaddr_in for destination socket Size of struct sockaddr_in -> sendto sock, str, len, 0, pdestaddr, 16 value = 24 = 0x18 9. You should see unixserver print your message in the UNIX shell window. It will also show you which port number you were assigned (since you never bound an address to your socket, you were assigned one dynamically). 10. The server will send you a reply. First get a place to put the reply, then read the reply. -> pbuf = calloc (1, 80) -> read (sock, pbuf, 80) Notice that the read command returns the number of bytes read. To see the reply: -> printf pbuf Kill the unixserver process. (If you ran it in the foreground as shown above, just type ^C in the window where it is running.) Tornado Training Workshop Copyright E-46L
Advanced UDP lab Objective: To send data from a UNIX client to a VxWorks server and to understand the consequences of data representation In this lab, you will write a UNIX client and a VxWorks server (It won t take very long - trust me). The UNIX client will send the VxWorks server a 4-byte integer, and the VxWorks server will print that integer to the console. The issue involved is that the UNIX host and the VxWorks target might store integers using different byte-ordering. You must use the macros htonl( ) and ntohl( ) to perform the byte swapping (i.e., the client puts the integer in network byte ordering and then sends it; the server converts the byte received to its local byte ordering before using it). 1. Write the VxWorks server that reads an integer from a UDP socket and prints it to the screen. The easiest way to do this is to copy unixserver.c to vxserver.c and make some modifications. 2. Write a UNIX client that takes an integer as an argument and sends it to the VxWorks server. A stub for this code is provided in unixclient.c. You saw all the steps needed to implement the client in the regular lab, so this should be straightforward. 3. Compile the UNIX client and the VxWorks server: % cc unixclient.c -o unixclient -lnsl -lsocket % ccx vxserver.c (Windows users may compile vxserver.c within the Tornado IDE. The exact command line used for compiling unixclient.c under UNIX may vary. Ask your instructor.) 4. Download and then start the VxWorks server. If you want to, use port 5000: Tornado Training Workshop Copyright E-47L
-> ld < vxserver.o -> sp vxserver, 5000 5. Start the UNIX client. If you want to pass the number 17, and your target s internet address is 147.11.12.106: % unixclient 5000 147.11.12.106 17 Hints: The following modifications must be made to write the VxWorks server: The header files included must be changed. The main( ) routine should be renamed vxserver( ). The arguments passed to it should also be changed (i.e., replace argc and argv with port). The main loop should be modified to read in a long integer (i.e, 4 bytes). This long integer should then be converted from the network byte order to the host byte order via ntohl( ). The UNIX client should be simple. Before sending the integer, don t forget to put it in network byte order via htonl( ). Tornado Training Workshop Copyright E-48L
Objective: TCP Socket Programming To communicate between two nodes on a network using TCP sockets. 1. If you have rebooted your target, re-load sockhelp.o. 2. Change your directory to HomeDir/a_netProg/tcp. 3. In UNIX shell window, change your directory to ~/a_netprog/ tcp: % cd ~/a_netprog/tcp 4. Start the UNIX TCP server. The source code is in testserver.c. You may want to look at the source in a separate window as we use the program. The program creates a TCP socket, binds the socket to a specified port, creates the socket queue, and blocks, waiting for a connection. The syntax for the server program is: % testserver portnumber To keep students separated, use the following port numbers: Team Port Numbers 1 5100-5199 2 5200-5299 3 5300-5399...... 8 5800-5899 % testserver 5101 A team1 port number is shown. 5. On the target, allocate and initialize a sockaddr_in structure to refer to your server on columbia: -> paddr = malloc (16) new symbol "paddr" added to symbol table. paddr = 0xb6c9c: value = 4193800 = 0x3ffe08 Tornado Training Workshop Copyright E-49L
-> sockaddrprompt paddr Type <CR> to leave field unchanged: sin_addr -- Enter inet address or host name [47.116.99.112]: Server sin_port -- Enter port number [25145]: 5101 Your port number may differ. value = 65535 = 0xffff 6. Create a TCP socket. -> sock = socket (2, 1, 0) new symbol "sock" added to symbol table. sock = 0xb6c94: value = 7 = 0x7 7. Connect to the testserver port on columbia. -> connect sock, paddr, 16 value = 0 = 0x0 In the window where testserver( ) is running, you should see testserver print something like: Client connected from inet 147.11.12.171, port 1024 8. Initialize str to point to a string to send to columbia. -> str = "How are you?" new symbol "str" added to symbol table. str = 0xb6c7c: value = 748676 = 0xb6c84 = str + 0x8 9. Write the string to the socket. -> write sock, str, strlen (str) + 1 value = 13 = 0xd You should see testserver( ) printing the string in the X- window and prompting you for a string. Enter a reply. Here is your 13 bytes message: How are you? SOCK_STREAM VxWorks target IP address Port number being used on VxWorks (dynamically assigned) Tornado Training Workshop Copyright E-50L
Enter message to send back: Fine, thanks. And you? The testserver( ) program blocks, waiting for another request from the same client. 10. To read the reply, allocate a buffer, read the socket, and print the results. -> pbuf = malloc (100) new symbol "pbuf" added to symbol table. pbuf = 0xb6c74: value = 4193388 = 0x3ffc6c -> read sock, pbuf, 100 value = 24 = 0x18 -> printf pbuf value = 23 = 0x17 Fine, thanks. And you? 11. Examine the server source code and make sure that you understand each of the steps. Try different combinations and see if you can predict the outcome. 12. Shut down the connection. -> close sock value = 0 = 0x0 In the UNIX window running the server, you should see: server socket closing. Tornado Training Workshop Copyright E-51L
Advanced TCP lab Objective: We will implement a VxWorks TCP server. This will be a concurrent server, that is to say, it will be able to process several requests simultaneously. Overview A sequential server sits in an infinite loop, connecting to a client and then servicing it. Consequently, a sequential server cannot service a second client until it has finished servicing the first. A concurrent server, on the other hand, can handle multiple requests concurrently. We have seen in this chapter how to implement a concurrent server in which several equivalent server tasks are spawned at initialization time; this has advantages in real time and embedded environments. A more traditional concurrent server would dynamically spawn a sub-server task to service each client. Thus, if five different client tasks wanted the same service, the master concurrent server would spawn five subserver (slave) tasks, one for each client. In this lab, the source code for a sequential VxWorks server is provided. We will modify the code to create a traditional concurrent server with a master and dynamically spawned slaves. 1. Look at the code for seqserver.c. It accepts connections one at a time. Once a client is connected, it reads requests from that client. It services the request by printing something like: Task t5 servicing request from inet 90.0.0.70, port 1025 2. Copy the code to conserver.c. Modify the code to make it a traditional concurrent server. There are two modifications needed: The (master) server must spawn tasks to service each connection instead of handling them itself (i.e., spawn a task to execute dorequest instead of just calling the function). Tornado Training Workshop Copyright E-52L
There is a reentrancy issue. Each sub-server task should have its own allocated structure containing the client s address. 3. Compile the code and download it to VxWorks. Spawn the server task with a priority of 60 (We want the server task to have a higher priority than the helper tasks it spawns. Why? Really?) -> taskspawn "tconserver", 60, 0, 3000, vxserver 4. UNIX client code is provided in conclient.c. The client issues a request every few seconds for a couple of minutes. Start two client tasks (you must specify your target s IP address): % conclient TargetIP & % conclient TargetIP & 5. Verify that the server has spawned two helper tasks to service the clients. Tornado Training Workshop Copyright E-53L