Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server...

13
Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming

Transcript of Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server...

Page 1: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Network Layer ProgramingConnection-Oriented Sockets

SWE 344Internet Protocols & Client Server

Programming

Page 2: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

We have four tasks to perform before a server can transfer data with a client connection:

Create a socket

Bind the socket to a local IPEndPoint

Place the socket in listen mode

Accept an incoming connection on the socket

The IPEndPoint class contains the host and local or remote port information needed by an application to connect to a service on a host. By combining the host's IP address and port number of a service, the IPEndPoint class forms a connection point to a service.

A Simple TCP Server

Page 3: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Creating the Server The first step to constructing a TCP server is to

create an instance of a Socket object. The other three functions necessary for successful server operations are then accomplished by using methods of the Socket object.

IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);Socket newsock = Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);newsock.Bind(ipep);newsock.Listen(10);Socket client = newsock.Accept();

The Socket object created by the Accept() method can now be used to transmit data in either direction between the server and the remote client.

Page 4: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

//SimpleTcpSrvr.cs program

using System;using System.Net;using System.Net.Sockets;using System.Text;class SimpleTcpSrvr{ public static void Main() { int recv; byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newsock.Accept();

Page 5: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None); while(true) { data = new byte[1024]; recv = client.Receive(data); if (recv == 0) break; Console.WriteLine( Encoding.ASCII.GetString(data, 0, recv)); client.Send(data, recv, SocketFlags.None); }

Page 6: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Console.WriteLine("Disconnected from {0}", clientep.Address); client.Close(); newsock.Close(); }}

Page 7: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Testing the ServerTo start the sample TCP server, open a command prompt window and type SimpleTcpSrvr. The server will display the opening greeting and wait for an incoming client:

C:\>SimpleTcpSrvrWaiting for a client...

Once the server is running, open another command prompt window (either on the same system or another system on the network) and start the Telnet program. Connect to the address of the server and the 9050 port used:

C:\>telnet 127.0.0.1 9050

The connection should start, and the server welcome screen should appear.

At this point, the server is waiting for a message from the client. Using the Telnet program, a strange thing happens. If you try to type a message, each individual character is sent to the server and immediately echoed back. If you try to type in a phrase, each character is sent individually and returned by the server individually.

Page 8: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Creating the Client

the first step of creating the client program is to create a Socket object. The Socket object is used by the Socket Connect() method to connect the socket to a remote host:

IPEndPoint ipep = new IPEndPoint(Ipaddress.Parse("192.168.1.6"), 9050);

Socket server = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.Tcp);

server.Connect(ipep);

This example attempts to connect the socket to the server located at address 192.168.1.6.

Page 9: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

//SimpleTcpClient.cs program using System;using System.Net;using System.Net.Sockets;using System.Text;class SimpleTcpClient{ public static void Main() { byte[] data = new byte[1024]; string input, stringData; IPEndPoint ipep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 9050); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Page 10: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

try { server.Connect(ipep); } catch (SocketException e) { Console.WriteLine("Unable to connect to server."); Console.WriteLine(e.ToString()); return; } int recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData);while(true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input)); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

Page 11: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Console.WriteLine("Disconnecting from server..."); server.Shutdown(SocketShutdown.Both); server.Close(); }}

Page 12: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

Testing the ClientThe first thing to test is the Exception code used for the situation where the server is unavailable. This is an easy thing to do: just don’t start the server program and do run the SimpleTcpClient program. This should produce the warning message you created in the Exception code:

C:\>SimpleTcpClientUnable to connect to server.System.Net.Sockets.SocketException: Unknown error (0x274d) at System.Net.Sockets.Socket.Connect(EndPoint remoteEP) at SimpleTcpClient.Main()C:\>

First, start the SimpleTcpSrvr program on the designated server machine. Once it has indicated it is waiting for clients, start the SimpleTcpClient program either in a separate command prompt window on the same machine, or on another machine on the network. When the client establishes the TCP connection, it should display the greeting banner from the server. At this point, it is ready to accept data from the console, so you can start entering data.

Page 13: Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.

END