SWE 344 Internet Protocols & Client Server Programming The C# Sockets Helper Classes.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Socket Programming CS3320 Fall 2010.
Asynchronous Sockets SWE 344 Internet Protocols & Client Server Programming.
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Lecture 2b Sockets Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Data Communications and Networking (Third Edition)
Socket Programming.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
Tutorial 8 Socket Programming
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
ISP – 9 th Recitation Socket programming – Client side.
1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6.
I NTRODUCTION OF S OCKET P ROGRAMMING L.Aseel AlTurki King Saud University.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Internet Protocols & Client Server Programming
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
.NET & C# Introduction 11 March, 2004 Yusung Kim CS441 Introduction to Computer Networking.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Lector: Aliyev H.U. Lecture №5 Telecommunication network software design with.NET. Using streams for network programming TASHKENT UNIVERSITY OF INFORMATION.
Lector: Aliyev H.U. Lecture №6 Design of client-server communication software. TCP-based network programming TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
Windows Programming Using C# Internet Programming.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Lector: Aliyev H.U. Lecture №9 Broadcast network software design TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION NETWORKS.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Lector: Aliyev H.U. Lecture №4 Telecommunication network software design with.NET. Using sockets for network programming TASHKENT UNIVERSITY OF INFORMATION.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Lector: Aliyev H.U. Lecture №10 Multicast network software design TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION.
UDP Client-Server. The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Network Programming with C# Exceed Camp #2, CPE, KU Day 3.
Connectionless Sockets SWE 344 Internet Protocols & Client Server Programming.
Network Programming Chapter 5: Raw Socket Programming.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
IP multicasting SWE 344 Internet Protocols & Client Server Programming.
Socket Programming Lab 1 1CS Computer Networks.
12/5/2015.net 1 system.net Contains any network functionallity you would need in c# Several sub namespaces exists to allow for more fined control –System.Net.Sockets.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Socket Programming.
 Socket class ◦ provides a rich set of methods and properties for network communications. The Socket class allows you to perform both synchronous and.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket programming with UDP UDP: no “connection” between client & server sender explicitly attaches IP destination address and port # to each packet receiver.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
1 Network Communications A Brief Introduction. 2 Network Communications.
1 K. Salah Application Layer Module K. Salah Network layer duties.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 14 th Lecture Pavel Ježek
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
Sockets and Beginning Network Programming
Networking COMP
Socket Programming in C
Internet Applications and Network Programming
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Internet Networking recitation #8
Presentation transcript:

SWE 344 Internet Protocols & Client Server Programming The C# Sockets Helper Classes

TcpClient Class The TcpClient class, located in the System.Net.Sockets namespace, was designed to facilitate the writing of TCP client applications by bypassing the need to use the Socket class TCP functions. TcpClient Class Constructors The TcpClient class allows you to create a TcpClient object using three constructor formats: TcpClient() TcpClient newcon = new TcpClient(); newcon.Connect(" 8000); TcpClient(IPEndPoint localEP) IPEndPoint iep = new IPEndPoint(IPAddress,Parse(" "), 8000); TcpClient newcon = new TcpClient(iep); newcon.Connect(" 8000); TcpClient(String host, int port) TcpClient newcon = new TcpClient(" 8000); 2

TcpClient Class Methods TcpClient Methods : MethodDescription Close() Closes the TCP connection Connect() Attempts to establish a TCP connection with a remote device. Equals() Determines if two TcpClient objects are equal GetHashCode() Gets a hash code suitable for use in hash functions GetStream() Gets a Stream object that can be used to send and receive data GetType() Gets the Type of the current instance ToString() Converts the current instance to a String object The TcpClient class contains a helpful collection of properties and methods to assist your efforts in writing TCP client applications 3

TcpListener Class The TcpListener class (also located in the System.Net.Sockets namespace) provides a simplified way to create TCP server applications. TcpListener Class Constructors TcpListener class has three constructor formats: TcpListener(int port) This constructor binds to a specific local port number. TcpListener(IPEndPoint ie) This constructor binds to a specific local EndPoint object. TcpListener(IPAddress addr, int port) TcpListener(IPAddress addr, int port) This constructor binds to a specific local IPAddress object and port number. 4

TcpListener Class Methods TcpListener Class Methods : MethodDescription AcceptSocket()Accepts an incoming connection on the port and assign it to a Socket object AcceptTcpClient()Accepts an incoming connection on the port and assigns it to a TcpClient object Equals()Determines if two TcpListener objects are equal GetHashCode()Gets a hash code suitable for use in hash functions GetType()Gets the type of the current instance Pending()Determines if there are pending connection requests Start()Starts listening for connection attempts Stop()Stops listening for connection attempts (closes the socket) ToString()Creates a string representation of the TcpListener object To create a TcpListener object and listen for incoming connections goes like this: TcpListener server = new TcpListener(IPAddress.Parse(" "), 9050); server.Start(); TcpClient newclient = server.AcceptTcpClient(); 5

6 UdpClient Class UdpClient class was created to help make UDP network programs simpler for network programmers. UdpClient Class Constructors The UdpClient class has four formats of constructors: UdpClient() This format creates a new UdpClient instance not bound to any specific address or port. UdpClient(int port) This constructor binds the new UdpClient object to a specific UDP port number. UdpClient(IPEndPoint iep) This constructor binds the new UdpClient object to a specific local IP address and port number. UdpClient(string host, int port) This format binds the new UdpClient object to any local IP address and port and associates it with a specific remote IP address and port.

7 UdpClient Class Methods : MethodDescription Close()Closes the underlying socket Connect()Allows you to specify a remote IP endpoint to send and receive data with DropMulticastGroup()Removes the socket from a UDP multicast group Equals()Determines if two UdpClient objects are equal GetHashCode()Gets a hash code for the UdpClient object GetType()Gets the Type of the current object JoinMulticastGroup()Adds the socket to a UDP multicast group Receive()Receives data from the socket Send()Sends data to a remote host from the socket ToString()Creates a string representation of the UdpClient object UdpClient Class Methods The JoinMulticastGroup() and DropMulticastGroup() methods allow you to program UDP applications to use IP multicasting.

8 Receive() Methods The UdpClient class uses the Receive() method to accept UDP packets on the specified interface and port. There is only one Receive() method format: byte[ ] Receive(ref IPEndPoint iep) The Receive() method accepts UDP packets on the IP address and UDP port specified by the UdpClient constructor, either system-specified values, or values set in the constructor. Send() method has three formats that can send data to a remote host: Send(byte[] data, int sz) This format sends the byte array data of size sz to the default remote host. To use this format, you must specify a default remote UDP host using either UdpClient constructor, or the Connect() method: UdpClient host = new UdpClient(" ", 9050); Send(byte[] data, int sz, IPEndPoint iep) This format sends the byte array data of size sz to the remote host specified by iep. Send(byte[] data, int sz, string host, int port) This format sends the byte array data of size sz to the host host at port port. Send() Methods

9 Moving Data across the Network This section shows techniques for moving different types of binary data across the network. When programming to communicate with various systems, it is important that you understand how binary data is stored on a device and how it is transmitted across the network. Binary Data Representation The major issue when moving binary datatypes on a network is how they are represented. The various types of machines all represent binary datatype in their own way. You must ensure that the binary value on one machine turns out to be the same binary value on another machine. Machines running a Microsoft Windows OS on an Intel (or compatible) processor platform store binary information using a set pattern for each datatype. It is important that you understand how this information is represented when sending binary data to a non-Windows remote host.

10 C# Binary datatypes DatatypeBytesDescription sbyte1Signed byte integer with values from –128 to 127 byte1Unsigned integer with values from 0 to 255 short2Signed short integer with values from –32,768 to 32,767 ushort2Unsigned short integer with values from 0 to 65,535 int4Standard integer with values from –2,147,483,648 to 2,147,483,647 uint4Unsigned integer with values from 0 to 4,294,967,295 long8Large signed integer with values from – 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ulong8Large unsigned integer with values from 0 to 18,446,744,073,709,551,615 float4A floating-point decimal number with values from 1.5 x 10 –45 to 3.4 x 1,038, using 7-digit precision double8A floating-point decimal number with values from 5.0 x 10 –324 to 1.7 x , using 15–16-digit precision decimal16A high-precision floating-point number with values from 1.0 x 10 – 28 to 7.9 x 10 28, using 28–29-digit precision

11.NET System class supplies the BitConverter class to convert binary datatypes into byte arrays, and vice versa. BitConverter class’ method GetBytes()converts a standard binary datatype to a byte array. For example: int test = 1990; byte[ ] data = BitConverter.GetBytes(test); newsock.Send(data, data.Length); Sending Binary Datatypes Receiving Binary Datatypes The receiving program must be able to receive the byte array containing the binary datatype and convert it back into the original binary datatype. This is also done using BitConverter class methods.

12 BitConverter Methods for Converting Data MethodDescription ToBoolean()Converts a 1-byte byte array to a Boolean value ToChar()Converts a 2-byte byte array to a Unicode character value ToDouble()Converts an 8-byte byte array to a double floating-point value ToInt16()Converts a 2-byte byte array to a 16-bit signed integer value ToInt32()Converts a 4-byte byte array to a 32-bit signed integer value ToSingle()Converts a 4-byte byte array to a single-precision floating-point value ToString()Converts all bytes in the byte array to a string that represents the hexadecimal values of the binary data ToUInt16()Converts a 2-byte byte array to a 16-bit unsigned integer value ToUInt32()Converts a 4-byte byte array to a 32-bit unsigned integer value ToUing64()Converts an 8-byte byte array to a 64-bit unsigned integer value All the converter methods have the same format: BitConverter.ToInt16(byte[] data, int offset)

13 Communicating with Other Host Types Binary Datatype Representation The problem of dueling binary datatypes arises from the fact that CPU platforms may store binary datatypes differently. Because multiple bytes are used for the datatype, they can be stored one of two ways: The least significant byte first (called little endian) The most significant byte first (called big endian) The binary datatype is interpreted correctly on each system, sending and receiving. If the wrong datatype representation is used to convert a raw binary byte array, your programs will be working with incorrect data.

14 Converting Binary Data Representation The problem of using different binary datatype representations is a significant issue in the Unix environment. The Unix world has devised a solution: sending binary datatypes in a generic method. The network byte order representation of binary datatypes was created as intermediate storage for binary data to be transmitted across the network. The idea is for each network program to convert its own local binary data into network byte order before transmitting it. On the receiving side, the system must convert the incoming data from network byte order into its own internal byte order. This ensures that the binary data will be converted to the proper representation for the destination host.

15 The.NET library includes methods to convert integer values to network byte order, and vice versa. These methods are included in the IPAddress class, contained in the System.Net namespace. One is HostToNetworkOrder(), which converts integer datatypes to a network byte order representation. The NetworkToHostOrder() method of the IPAddress class converts data received in network byte order back to the appropriate byte order of the system running the program. Similar to HostToNetworkOrder(), the NetworkToHostOrder() method converts an integer value in network byte order to an integer value in the local host’s byte order. It is possible that both orders are the same and no conversion will be necessary, but to be on the safe side, it is always best to include this method.

16 The NetworkOrderClient.cs program, which demonstrates how to use the HostToNetworkOrder() and NetworkToHostOrder() methods to transmit data across the network. using System; using System.Net; using System.Net.Sockets; using System.Text; class NetworkOrderClient { public static void Main() { byte[] data = new byte[1024]; string stringData; TcpClient server; try { server = new TcpClient(" ", 9050); } catch (SocketException) { Console.WriteLine("Unable to connect to server"); return; } NetworkStream ns = server.GetStream(); int recv = ns.Read(data, 0, data.Length); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData);

17 short test1 = 45; int test2 = ; long test3 = ; short test1b = IPAddress.HostToNetworkOrder(test1); data = BitConverter.GetBytes(test1b); Console.WriteLine("sending test1 = {0}", test1); ns.Write(data, 0, data.Length); ns.Flush(); int test2b = IPAddress.HostToNetworkOrder(test2); data = BitConverter.GetBytes(test2b); Console.WriteLine("sending test2 = {0}", test2); ns.Write(data, 0, data.Length); ns.Flush(); long test3b = IPAddress.HostToNetworkOrder(test3); data = BitConverter.GetBytes(test3b); Console.WriteLine("sending test3 = {0}", test3); ns.Write(data, 0, data.Length); ns.Flush(); ns.Close(); server.Close(); }

18 The NetworkOrderSrvr.cs program is used to receive the data and convert it back to host byte order. using System; using System.Net; using System.Net.Sockets; using System.Text; class NetworkOrderSrvr { public static void Main() { int recv; byte[] data = new byte[1024]; TcpListener server = new TcpListener(9050); server.Start(); Console.WriteLine("waiting for a client..."); TcpClient client = server.AcceptTcpClient(); NetworkStream ns = client.GetStream(); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); ns.Write(data, 0, data.Length); ns.Flush(); data = new byte[2]; recv = ns.Read(data, 0, data.Length);

19 short test1t = BitConverter.ToInt16(data, 0); short test1 = IPAddress.NetworkToHostOrder(test1t); Console.WriteLine("received test1 = {0}", test1); data = new byte[4]; recv = ns.Read(data, 0, data.Length); int test2t = BitConverter.ToInt32(data, 0); int test2 = IPAddress.NetworkToHostOrder(test2t); Console.WriteLine("received test2 = {0}", test2); data = new byte[8]; recv = ns.Read(data, 0, data.Length); long test3t = BitConverter.ToInt64(data, 0); long test3 = IPAddress.NetworkToHostOrder(test3t); Console.WriteLine("received test3 = {0}", test3); ns.Close(); client.Close(); server.Stop(); } }

20 END