CIS6930 Network Programming Module - 4 Objectives

Skills/Concepts/Assignments Objectives Basic Network Programming • TCP/UDP Classes In C# .NET • File, Memory and Network Streams • Introduction to XML • Network programming using C# .NET and Network Streams Project Concepts Introduction to Control/Data network protocols, Protocol Details RTSP/RTP Overview of the Media / assignment Introduction to LINQ , Classes Read Chapters 9 and 10, pages 239 to 283. and Objects TCP/UDP Classes Section Goals

Goal Course Presentation Understanding Windows Form Understanding .NET Network Classes Network Client/Server Programming Project Concepts Creating a Client and Server Application

Understanding the Visual Managing Projects Studio Development Environment Questions? Open to any questions Namespace System.Net Namespace Purpose

System.Net Simple network programming interface for many protocols with WebRequest and WebResponse forming the basis of the support. System.Net.Http Interface for HTTP applications

System.Net.Mail Used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) servers

System.Net.Sockets IPv4 and Ipv6 support Windows Socket () interface Many more,.. Cache, Configuration, HttpHeaders, Mime, NetworkInformation,PeerToPeer, Security, Windows Network Programming

Recap: .There are two types of classes in Windows .Protocol specific class (Pluggable Protocols) .Transport specific class .Protocol specific classes .WebRequest (Pluggable Protocol) .WebClient and HttpListener .WebProxy – Used for proxying the above request .Transport specific .TcpClient and TcpListener .UdpClient Pluggable Protocols • Built in protocols for http, https, ftp and file • WebRequest • WebResponse (returns a , more on this later) • Allows custom protocols to be “plugged-in” • Need to register the prefix required using RegisterPrefix •Provide the prefix •Provide the create method • Response is of type HttpWebResponse • Use abstract classes WebRequest and WebResponse WebRequest

Method Purpose

WebRequest Constructor for abstract class used to provide a response from a Uniform Resource Identifier (URI) Create(URI) Creates a WebRequest instance based on URI scheme, http,https,ftp and file CreateHttp(URI) Creates a HttpWebRequest instance for the specified URI

GetResponse Virtual method overriden based on Create resource type used WebResponse

Method Purpose

WebResponse Constructor for abstract class used to provide a response from a Uniform Resource Identifier (URI) ContentLength Data Member, content length of data being received ContentType Data Member, content type of data being received

Headers Name/Value pairs of being received Close Closes the response stream Windows Network Programming

Namespace System.Net.Sockets .Network Programming Resource .http://msdn.microsoft.com/en-us/library/orm-9780596527570-03-14.aspx .O’Reilly C# 3.0 (Old but helpful) .Read Chapter 14 - Networking .TcpClient - Provides client side connections for TCP network services .TcpListener - Provides server side TCP listener for incoming client connection request .UdpClient – Provides User Protocol services TcpClient - Constructor

Method Purpose

TcpClient() Initializes a new TcpClient class TcpClient(AddressFamily) Initializes a new TcpClient class with the specified family, for example IPv4 or IPv6 TcpClient(IPEndPoint) Initializes a new TcpClient class, bound to a specific local endpoint

TcpClient(String, Int32) Initializes a new TcpClient class and connects to the host and port specified TcpClient - Properties

Properties Purpose

Active Identifies whether the connection has been made Available Amount of data available to read

Connected Indicates whether the socket is connected

ReceiveTimeout Amount of time operation will wait to complete before timing out SendTimeout Many more,… Client, ExclusiveAddressUse, LingerState, NoDelay, ReceiveBufferSize, SendBufferSize etc. TcpClient - Methods

Method Purpose

Connect Connects the client to the specified server using either IPEndPoint or IPAddress and port Close Disconnect and close the socket

GetStream Returns the NetworkStream of a connected client for sending and receiving Many more,… BeginConnect and other asynchrounous methods, TcpListener - Constructor

Method Purpose

TcpListener (AddressFamily, Initializes a new TcpListener class that Int32) listens on the port specified using the specified family, for example IPv4 or IPv6 TcpListener (IPEndPoint) Initializes a new TcpListener class, bound to a specific local endpoint

TcpListener (Int32) Obsolete, Initializes a new TcpListener class that listens on the port specified TcpListener - Properties

Properties Purpose

Active Identifies whether the listener is actively listening for client connections ExclusiveAddressUse Determines if the listener allows more than one socket to listen on a specified port LocalEndPoint Gets the underlying EndPoint for the current listening object

Server Gets the underlying TcpListener - Methods

Methods * Purpose

AcceptSocket Accepts a pending connection request Create Create a TcpListener and listen on specified port Pending Determine if there are any pending client connections Start Start, starts listening for incoming connections on specified port, Stop Stop closes the listener GetContent Returns a HttpListenerContent

* There are many asynchronous methods not being represented HttpListenerContent • Request • Property that gets a HttpListenerRequest • Represents client’s request for a resource • Response • Property that gets a HttpListenerResponse • Represents response sent to the client • User •Property used to obtain identity of User •Authentication information •Security Roles HttpListenerRequest - Properties

Properties Purpose

AcceptTypes Gets the MIME types accepted by the client. ClientCertificateError Gets an error code that identifies a problem with the X509Certificate provided by the client. ContentEncoding Gets the content encoding that can be used with data sent with the request ContentLength64 Gets the length of the body data included in the request. ContentType Gets the MIME type of the body data included in the request. HttpListenerRequest - Properties

Properties Purpose

Cookies Gets the cookies sent with the request. HasEntityBody Gets a Boolean value that indicates whether the request has associated body data. Headers Gets the collection of header name/value pairs sent in the request. HttpMethod Gets the HTTP method specified by the client. InputStream Gets a stream that contains the body data sent by the client. HttpListenerRequest - Properties

Properties Purpose

IsAuthenticated Gets a Boolean value that indicates whether the client sending this request is authenticated. IsLocal Gets a Boolean value that indicates whether the request is sent from the local computer. IsSecureConnection Gets a Boolean value that indicates whether the TCP connection used to send the request is using the Secure Sockets Layer (SSL) protocol. IsWebSocketRequest Gets a Boolean value that indicates whether the TCP connection was a WebSocket request. WebSockets WebSockets enable Web applications to deliver real-time notifications and updates in the browser. Developers have faced problems in working around the limitations in the browser’s original HTTP request-response model, which was not designed for real-time scenarios. WebSockets enable browsers to open a bidirectional, full-duplex communication channel with services. Each side can then use this channel to immediately send data to the other. Now, sites from social networking and games to financial sites can deliver better real-time scenarios, ideally using same markup across different browsers.

Source: Developer Network HttpListenerRequest - Properties

Properties Purpose

LocalEndPoint Get the server IP address and port number to which the request is directed. ProtocolVersion Gets the HTTP version used by the requesting client. QueryString Gets the query string included in the request. RawUrl Gets the URL information (without the host and port) requested by the client. RemoteEndPoint Gets the client IP address and port number from which the request originated. HttpListenerRequest - Properties

Properties Purpose

ServiceName Gets the Service Provider Name (SPN) that the client sent on the request. TransportContext Gets the TransportContext for the client request.

Url Gets the Uri object requested by the client. UrlReferrer Gets the Uniform Resource Identifier (URI) of the resource that referred the client to the server. UserAgent Gets the user agent presented by the client. HttpListenerRequest - Properties

Properties Purpose

UserHostAddress Gets the server IP address and port number to which the request is directed. UserHostName Gets the DNS name and, if provided, the port number specified by the client. UserLanguages Gets the natural languages that are preferred for the response. HttpListenerRequest - Methods

Methods * Purpose

GetClientCertificate Retrieves the client's X.509 v.3 certificate.

* There are many asynchronous methods not being represented HttpListenerResponse • Allows both getting and setting of properties • Much of the same properties as HttpListenerRequest • ContentEncoding, ContentLength64, ContentType, Cookies, Headers, ProtocolVersion. • OutputStream (instead of InputStream) • But also includes • KeepAlive – Persistent connections • RedirectLocation – Redirection support • SendChunked – Support chunked transfer encoding • StatusCode – Http status code • StatusDescription – Result description HttpListenerResponse - Methods

Methods * Purpose

Abort Closes the connection to the client without sending a response. AddHeader Adds the specified header and value to the HTTP headers for this response. AppendCookie Adds the specified Cookie to the collection of cookies for this response. Redirect Configures the response to redirect the client to the specified URL. SetCookie Adds or updates a Cookie in the collection of cookies sent with this response.

* There are many asynchronous methods not being represented, as well as close options UdpClient - Constructor Method Purpose

UdpClient() Initializes a new UdpClient class UdpClient(AddressFamily) Initializes a new UdpClient class with the specified family, for example IPv4 or IPv6 UdpClient(Int32) Initializes a new UdpClient class and binds it to the port specified UdpClient(IPEndPoint) Initializes a new UdpClient class, bound to a specific local endpoint

UdpClient(Int32, AddressFamily) Initializes a new UdpClient class and binds it to the port and family specified UdpClient(String, Int32) Initializes a new UdpClient class and establishes a default remote host and port UdpClient - Properties

Properties Purpose

Active Identifies whether a default remote host has been established Available Amount of data available to read

Client Identifies the underlying socket

DontFragment Allow IP to be fragmented

Many more,… EnableBroadCast, ExclusiveAddressUse, MulticastLoopback, Ttl UdpClient

Method Purpose

Connect Establish a default remote host Receive Returns a UDP datagram sent from a remove host Send Sends a UDP datagram to a remote host Close Close a UDP connection

Many more,… Both asynchronous and features Streams Section Goals

Goal Course Presentation Understanding Windows Form Understanding .NET Stream Classes, the basis for Programming sending and receiving data. Understand XML Classes Project Concepts Creating files and network streams to use within the class project Understanding Object Managing files, open, reading, writing, and closing Oriented Programming files. Examples Section Objectives • Manage byte streams by using Stream classes. • Access files and folders by using the classes. • Manage the .NET Framework application data by using Reader and Writer classes. • Compress or decompress stream information in a .NET Framework application. • Learn how to use network streams Analyzing Streams

Streams .Represent an abstract form of an external source of data such as a file, network, , or an area of system memory. .Allow transfer of data between the application and the external source in bytes. System.IO namespace .Provides the majority of classes for file-based, stream-based and network-based operations. Analyzing Streams

Class Purpose

FileStream Supports read-write operations from and to files on a file system StreamReader Reads data from a text file

StreamWriter Writes data to a text file

MemoryStream Stores data in the memory

BufferedStream Caches data by buffering read- write operations on another stream NetworkStream Reads and writes to and from a remote host File System Classes

Class Description

MarshalByRefObject Base class for objects that communicate across application domain boundaries FileSystemInfo Base class for FileInfo and DirectoryInfo classes FileInfo Provides instance methods for managing files

File Provides static methods for managing files File System Classes

Class Description

DirectoryInfo Provides instance methods for managing folders Directory Provides static methods for managing folders Path Provides members to operate on a string instance of a directory or file location Managing Files and Folders To browse folders 1. Create an instance of the DirectoryInfo class by specifying the folder you want to browse. 2. Call on the GetDirectories or GetFiles method of the DirectoryInfo object. To create folders 1. Create an instance of the DirectoryInfo class by specifying the required path. 2. Test the Exists property of the DirectoryInfo object to ensure the uniqueness of the folder. 3. Call on the Create method of the DirectoryInfo object. Managing Files and Folders

To manage files .Call on the static methods such as Create, Copy, Delete, or Move of the File class or .Create an instance of the FileInfo class and call on the corresponding methods of the FileInfo object Managing File Input and Output • The classes for file-based input and output are part of the System.IO namespace. • Text files are the data files that contain only character-based data. • The StreamReader and the StreamWriter classes manipulate text files. • Binary files store any type of data as a sequence of bytes. • The BinaryReader and the BinaryWriter classes manipulate binary files. Managing Text Files • A text file is a disk file that stores only character- based data. • Character encoding describes the rules by which each character is represented. • There are different encoding schemes available, such as ASCIIEncoding, UTF8Encoding, and UnicodeEncoding. • By using UTF8Encoding and UnicodeEncoding, you can represent characters from all the international languages. Reading and Writing a Text File

Basic steps to read from a text file: 1. To open a text file, use one of the following steps: a. Create an instance of the StreamReader class. b. Create an instance of the TextReader class by instantiating a StreamReader class. c. Use the File.OpenText method. 2. Call the specific method of the chosen class to complete the task. Reading and Writing a Text File

. You can use the StreamWriter and the TextWriter classes to write a text file. . You should call the Flush method to ensure that data is being written and saved on the disk to be used afterward. Writing Text Files Reading Text Files Writing to and Reading from Binary Files Steps to read and write to nontext or binary files 1. Create an instance of the FileStream class. 2. Assign this instance to a BinaryReader or a BinaryWriter class as required to read and write a binary file, respectively.  To write to a binary file . Use the BinaryWriter class that works in association with the FileStream class.  To read from a binary file . Use the BinaryReader class that works in association with the FileStream class. Writing Binary Files Reading Binary Files Reading and Writing Strings StringReader .Enables reading from a string using a String object or StringBuilder object. StringWriter .Enables writing to a string using a StringBuilder object. StringBuilder .Works in association with StringWriter and StringReader classes to write to and read from the strings, respectively. StringBuilder Example Concatenation with Append using System; using System.Text; public class Test { static void Main() { StringBuilder builder = new StringBuilder(); for (int i=0; i < 100000; i++) builder.Append("!"); string x = builder.ToString(); using System; public class Test } { } static void Main() { string x = ""; // Strings are immutable for (int i=0; i < 100000; i++) x += "!"; } } XML XML (Extensible Markup Language) Is a text-based format for representing structured data. Working with XML

• The classes to work with XML data are organized in the System.Xml namespace. • The XmlReader and XmlWriter classes provide methods to respectively read data from and write data to XML files. • The XmlReader and XmlWriter classes provide a fast, non-cached, and forward-only way to read or write XML data. Writing XML File Reading XML File XML Schema

• XML schema describes the structure of an XML document. • An XML document is considered valid only when it conforms to its XML schema. • XML schema is particularly important when an XML file is used to share data between two applications. Without XML schema, the applications won’t know how the data in an XML file is structured. Using Streams in Memory

MemoryStream .Stores data temporarily as a byte or byte array in memory. .Minimizes the locking period of a file in a multi-user environment, thereby increasing the performance during read-write operations. To read and write data in memory .Use MemoryStream to read data from and write data to files in the form of array of bytes. Buffering Streams

.BufferedStream acts as an intermediate data holder for another stream. .BufferedStream provides custom stream implementations. .The BufferedStream class can be used like the MemoryStream class—wherein both classes can write bytes and byte arrays only. Using Network Streams Stream .Used by classes such as WebClient .Used to send and receive data using Strings .Hides the byte array implementation NetworkStream .Used by classes such as TcpClient and TcpListener .Used to read data from and write data to a host in the form of array of bytes. .Need to convert to string for use for texted based protocols Without Streams (Our Old Homework) Using Streams Using Network Streams Data Encoding • Encoding is the of transforming a set of Unicode characters into a sequence of bytes. • Decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters. • ASCIIEncoding corresponds to the Windows code page 20127 • ASCII is a 7-bit encoding, ASCII characters are limited to the lowest 128 Unicode characters • Allows the encoding of network data Compressing Stream Data

System.IO.Compression

GZipStream DeflateStream

• Read and write data as byte array • Perform the from and to compressed streams. compression/decompression of streams like the GZipStream. • Perform compression or decompression of the specified • Employ Deflate data format for stream data using the value in the compression/decompression. CompressionMode enumeration. • Employ gzip data format for compression and decompression. Section Summary

• The System.IO namespace consists of classes, such as FileStream, MemoryStream, StreamWriter, StreamReader, and BufferedStream for file-based and stream-based functions. • You can use file system classes, such as FileSystemInfo, Directory, File, and Path for moving, copying, and deleting files. • This namespace also consists of classes that help in compression and decompression of stream data. • NetworkStreams provide bidirectional network traffic C# Client/Server Example Windows Network Programming - Server Windows Network Programming - Client Multimedia Project Section Goals

Goal Course Presentation Understanding Windows Form Understanding .NET Client/Server model Programming Project Concepts Understand how the media server/client work

Understanding Object Development environment using multiple projects Oriented Programming Examples Questions? Open to any questions Objectives

• Understand the project assignment • Work with a real client/server application • Learn how network control and data channels work • Learn how to program in C# .NET •TCP •UDP • Learn the RTSP and RTP protocols The Basics

• Analyze FTP •How is the control channel used •How is the data channel used • Examine the RTSP and RTP protocols •Determine how the protocol functions •How to map from network streams to video processing •What do the protocol headers look like FTP: File Transfer Protocol

FTP file transfer FTP FTP Server user client interface user at host remote file local file system system

 Transfer file to/from remote host  Client/Server model  client: side that initiates transfer (either to/from remote)  server: remote host  ftp: RFC 959  ftp server: port 21 FTP: Separate Control, Data Connections

 FTP client contacts FTP server at TCP control connection port 21 port 21, TCP is transport protocol  Client authorized over control TCP data connection port 20 connection FTP FTP client  Client browses remote directory by server sending commands over control  Server opens another TCP data connection. connection to transfer another file.  When server receives file transfer  Data connection: “out of band” nd command, server opens 2 TCP  FTP server maintains “state”: connection (for file) to client current directory, earlier  After transferring one file, server authentication closes data connection. FTP Commands, Responses

Sample commands: Sample return codes  Sent as ASCII text over control  status code and phrase (as in channel HTTP)  USER username  331 Username OK, password  PASS password required   LIST return list of file in current 125 data connection already open; transfer starting directory  425 Can’t open data  RETR filename retrieves (gets) connection file  452 Error writing file  STOR filename stores (puts) file onto remote host Media Project: Separate Control, Data Connections

 Media client contacts server over TCP control connection port 554 port 554, using TCP and RTSP  Client sends media file to play UDP data connection port 25000 Media  Client sends UDP ephemeral port Media Client server uses for media exchange Server  Server acknowledges Information  During server transfer of the  Client sends “Play” request stream, client can “Pause” or  When server receives “Play” “Terminate”. command, server opens 2nd  Data connection is “out of band” connection over UDP to client using  Client displays video via streaming RTP Real-Time Streaming Protocol

• Network control protocol designed for use in entertainment and communications systems to control streaming media servers. • It is a text based protocol (like HTTP) • Defined in RFC 2326. • The protocol is used for establishing and controlling media sessions between end points. • The commands for the protocol are as follows: • DESCRIBE – List media streams controlled • SETUP – Specified how a single media stream should be transported, generally over RTP using a given port • PLAY – Request media stream to be played • PAUSE – Temporarily halt a playing media stream • TEARDOWN – Terminate a Session Real-Time Streaming Protocol

SETUP

Media Client PLAY servers

RTP audio/video

TEARDOWN

Server OK Server OK Received Received Client SETUP Client PLAY

Client Startup INIT Ready Playing Wait for Client Request INIT Ready Playing Received Received Teardown Pause Client Client TEARDOWN PAUSE

Teardown Received Client TEARDOWN Real-Time Streaming Protocol

 Header much like HTTP, but no content  Method SETUP, PLAY, PAUSE, TEARDOWN  URL rtsp://server.com/file  Version RTSP/1.0  Important header names

 CSeq, Session and Transport

Method Space URL Space Version CR LF

Header1 : Value CR LF Field Name Header2 : Value CR LF Field Name

Header N : Value CR LF Field Name Real-Time Streaming Protocol Real-Time Protocol

 IETF Audio/Video Transport WG  RTPv1 RFC 1889 (January 1996)  RTPv2 draft-ietf-avt-rtp-new-09.txt (March 2001)  Real-Time Protocol (RTP)  A framing protocol for real-time applications »  Does not define any QoS mechanism for real-time delivery! Real-Time Protocol

 Data functions (RTP)

 Content labelling

 Source identification

 Loss detection

 Re-sequecing

 Timing

 Intra-media synchronization: remove jitter with playout buffers

 Inter-media synchronization: lip-synchro between audio-video Real-Time Protocol - Header

0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |V=2|P|X| CC |M| PT | sequence number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | synchronization source (SSRC) identifier | +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | contributing source (CSRC) identifiers | | .... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

 version (V) CSRC count (CC)  padding (P) marker (M)  extension (X) type (PT) = 26 Media Client/Server Assignment

 Download MediaAssignment  Review the project files

 MediaAssignment – Common Files

 MediaClient – Client Implementation

 Locate the locations with STODOs

 Start to map out what is required to implement missing pieces

 MediaServer – Server Implementation

 Locate MediaClientTrace.exe in the bin\debug directory  Wireshark does not work for Local Host

 Get and install RawCap http://www.netresec.com/?page=RawCap Media Client/Server Assignment

 Run the project and get a Wireshark trace

 Click on Start

 Ignore BROKEN Client, you’ll need to fix it Media Client/Server Assignment

 Run MediaClientTrace.exe

 Click on Setup to get the dialog below

 Run RawCap (you must run as administrator)

 Select Loopback (address 127.0.0.1)

 Input file name, example media.pcap

 Click “OK” on the dialog

 Click “Play” and let the movie play a little bit

 Click “Pause”

 Click “TearDown” Media Client/Server Assignment

 Run Wireshark.exe

 Open the pcap file

 Click on File/Open and browse to media.pcap

 Examine the RTSP protocol Media Client/Server Assignment

 Become familiar with:

 The project files (Specifically the client)

 The protocols RTSP and RTP

 How the Client/Server program works End Module - 4