Network Programming Appendix Network Programming Labs

Similar documents
CS321: Computer Networks Socket Programming

CS321: Computer Networks Introduction to Application Layer

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Programming with TCP/IP. Ram Dantu

System Programming. Sockets

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

A Client-Server Exchange

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Socket Programming TCP UDP

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

CLIENT-SIDE PROGRAMMING

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Tutorial on Socket Programming

Oral. Total. Dated Sign (2) (5) (3) (2)

Lab 0. Yvan Petillot. Networks - Lab 0 1

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

UNIX Network Programming. Overview of Socket API Network Programming Basics

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Unix Network Programming

SOCKETS. COMP750 Distributed Systems

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

TCP: Three-way handshake

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

ECE 435 Network Engineering Lecture 2

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Application Architecture

Department of Computer Science

CS 640: Computer Networking

Sockets 15H2. Inshik Song

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Network Socket Programming - 3 BUPT/QMUL

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

CSC209 Review. Yeah! We made it!

CS 3516: Computer Networks

ECE 435 Network Engineering Lecture 2

CSE 124 Discussion Section Sockets Programming 10/10/17

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Network Socket Programming - 3 BUPT/QMUL

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Socket Programming for TCP and UDP

Client Server Computing

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

System Programming. Sockets: examples

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Computer Networks Prof. Ashok K. Agrawala

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Introduction to Socket Programming

CSE 333 SECTION 6. Networking and sockets

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

CSE 333 Lecture 16 - network programming intro

TCP Network Programming in C

Topics for this Week

Network Software Implementations

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

Communication. Communication. Distributed Systems. Networks and protocols Sockets Remote Invocation Messages Streams. Fall /10/2001 DoCS

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CS 499 Lab 3: Disassembly of slammer.bin I. PURPOSE

Elementary TCP Sockets

Networked Applications: Sockets. End System: Computer on the Net

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

Application Programming Interfaces

NETWORK AND SYSTEM PROGRAMMING

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

Group-A Assignment No. 6

CSE 333 SECTION 8. Sockets, Network Programming

Internet applications

What s an API? Do we need standardization?

Overview. Setup and Preparation. Exercise 0: Four-way handshake

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Socket Programming(2/2)

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Introduction to Berkeley Sockets

UNIX Sockets. COS 461 Precept 1

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

UNIX System Programming Lecture 19: IP Sockets

JFx Joystick. June 21, Unrestricted Distribution. Revision 1.00

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

Interprocess Communication Mechanisms

Transcription:

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