CIS6930 Network Programming Module - 4 Objectives
Total Page:16
File Type:pdf, Size:1020Kb
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 Client/Server 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 (WinSock) interface Many more,.. Cache, Configuration, HttpHeaders, Mime, NetworkInformation,PeerToPeer, Security, WebSockets 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 Stream, 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 header 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 Datagram 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 network socket 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: Microsoft 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