13-Week, SWE-344, Dr. S. Ahamad
Assistant Professor
Dr. Shahanawaj Ahamad
Internet Protocols & Client Server Programming
SWE-344
13-Week
1
• • • • • • • • • • 13-Week, SWE-344, Dr. S. Ahamad
Socket Class Steps for UDP Server Steps for UDP Client UDP Client /Server Communication Model UDP Server Using Socket Class Steps for TCP client and server TcpListner Class TCP Echo Server Using TcpListner Class Creating TCP Client Using TcpClient Class Few Important Methods of TcpClient Class
Outlines
2
13-Week, SWE-344, Dr. S. Ahamad
3
•Once the socket is created and bound to a local end-point, the socket can be used to send and receive UDP datagram's.
•Bind the socket to a local IPEndPoint (No need for Listen and Accept methods)
•Create a Socket object
server application:
• Because UDP is connectionless, only two things need to be specified to create a
Servers and TCP Clients.
•Socket class has methods for creating both UDP servers and clients and TCP
Socket Class
int int int int
ReceiveFrom( byte[ ] data, ReceiveFrom( byte[ ] data, ReceiveFrom( byte[ ] data, ReceiveFrom( byte[ ] data,
13-Week, SWE-344, Dr. S. Ahamad
4
ref EndPoint remote); SocketFlags flag, ref EndPoint remote); int size, SocketFlags flag, ref EndPoint remote); int offset, int size, SocketFlags flag, ref EndPoint remote);
• The formats of these two methods are shown below: int SendTo( byte[ ] data, EndPoint remote); int SendTo( byte[ ] data, SocketFlags flag, EndPoint remote); int SendTo( byte[ ] data, int size, SocketFlags flag, EndPoint remote); int SendTo( byte[ ] data, int offset, int size, SocketFlags flag, EndPoint remote);
cannot be used. Instead, the SendTo and ReceiveFrom methods are used.
•Since there is no connection in UDP, the Send and Receive methods of the socket class
Socket Class
13-Week, SWE-344, Dr. S. Ahamad
or should wait for another client
5
•On completion of communication with the Clients, the server should close the socket
specify the IPAddress and Port# of client.
•Since UDP is connectionless, hence to send every datagram, the server will MUST
socket, server can communicate to the clients by sending & receiving data grams.
•Once the socket has been created and bound to the local IPEndPoint, using the same
IPEndPoint.
•Server should bind the socket on specified local port# and local IP address or local
•Server should create a socket.
steps in sequence only.
•Since UDP is connectionless, hence the Server must implement the following two
Steps for UDP Server
continue processing
13-Week, SWE-344, Dr. S. Ahamad
6
•On Completion of communication with the Server, the client should close Socket or
specify the IP address and Port# of the server.
•Since UDP is connectionless, hence to send every datagram, the Client will MUST
or receiving datagram.
•Once the socket has been created, client should communicate to the server by sending
•Client should create socket by creating object of Socket class.
•The UDP Client must implement the following steps in sequence
Steps for UDP Client
13-Week, SWE-344, Dr. S. Ahamad
UDP Client /Server Communication Model
7
}
} 13-Week, SWE-344, Dr. S. Ahamad
while(true) { data = new byte[1024]; recv = server.ReceiveFrom(data, ref remoteEP); Console.Write("Received from {0}: ", remoteEP.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); server.SendTo(data, recv, SocketFlags.None, remoteEP); }
int recv; byte[] data;
server.Bind(localEP); Console.WriteLine("Waiting for a client..."); //dummy end-point EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
class UdpSocketServer { public static void Main( ){ IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
using System; using System.Net; using System.Net.Sockets; using System.Text;
UDP Server Using Socket Class
8
}
}
Console.Write("Enter message for server or exit to stop: "); input = Console.ReadLine(); if (input == "exit") break; client.SendTo(Encoding.ASCII.GetBytes(input), remoteEP); data = new byte[1024]; recv = client.ReceiveFrom(data, ref remoteEP); Console.Write("Echo from from {0}: ", remoteEP.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
13-Week, SWE-344, Dr. S. Ahamad
} Console.WriteLine("Stopping client"); client.Close();
while(true){
EndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); byte[] data; string input; int recv;
class SimpleUdpClient{ public static void Main( ){ Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
using System; using System.Net; using System.Net.Sockets; using System.Text;
UDP Client Using Socket Class
9
client. 13-Week, SWE-344, Dr. S. Ahamad
10
•On the listening socket the server will wait for the connection requests from the
will convert the same socket into listening socket.
•Once the socket has been created and bound to the local IPEndPoint, the server
IPEndPoint.
•Server will bind the socket on specified local port# and local IP address or local
•Server will create a socket.
steps in sequence only.
•Since TCP is connection-oriented, hence the Server must implement the following
Steps for TCP Server
13-Week, SWE-344, Dr. S. Ahamad
•At the end the server should also close the listening socket.
connected socket.
•On completion of communication with the Client, the server should close the
the IPAddress and Port# of client.
11
•Since TCP is connection-oriented, hence to send data, the server will NOT specify
•Using the connected socket the server can exchange stream of bytes with the client.
the client.
•The server will get the connected socket, on accepting the connection request from
Steps for TCP Server
13-Week, SWE-344, Dr. S. Ahamad
12
•On Completion of communication with the Server, the client must close Socket
specify the IP Address and Port# of the server.
•Since TCP is connection-oriented, hence to send data, the client will not need to
client).
& Port# of the server (on which the server is listening for connection request from
•While sending the connection request, the client will MUST specify the IPAddress
connection request to server.
•Once the socket has been created, using the same socket the client will send
•Client will create socket.
•The TCP Client must implement the following steps in sequence
Steps for TCP Client
13-Week, SWE-344, Dr. S. Ahamad
•Using TcpClient class to make TCP Client
•Using TcpListener class to make TCP Server
•Second Option
•Using Socket class to make TCP Client
•Using Socket Class to make TCP Server
•First Option
•Two different ways to develop TCP Server & TCP Client applications
13
Different Options to Develop TCP Server & Client
13-Week, SWE-344, Dr. S. Ahamad
•bool Pending( ): Determines if there are pending connections
•void Stop( ): Stops the server
•TcpClient AcceptClient( ): Accepts connection from a TcpClient
•void Start( ) : Starts the server
•Methods of TcpListner Class
Binds the server to an IPEndPoint object.
•TcpListener(IPEndPoint ie)
Binds the server to a specific IPAddress object and port number.
•TcpListener(IPAddress ip , int port)
•This class has the following constructors:
•The easiest way to create a TCP server is by using the TcpListener class.
TcpListner Class
14
13-Week, SWE-344, Dr. S. Ahamad
15
•NetworkStream behaves exactly like the FileStream we saw earlier. It can be used to read/write byte array. Also it can be used to create StreamReader/StreamWriter object for exchanging text messages with the client.
•After establishing a connection, this method returns an instance of TcpClient.The TcpClient instance has a method, GetStream( ), which returns a NetworkStream object. It is this NetworkStream object that is used both for reading and writing data from and to the client.
•The AcceptTcpClient( ) method is then called to establish a connection with a client. Note that this method will block until a client is received.
•After a TcpListener object is created, it must be started by calling the Start( ) method.
TcpListner Class
}
} 13-Week, SWE-344, Dr. S. Ahamad
writer.WriteLine("Welcome to my test server"); writer.Flush( ); String line = null; while((line = reader.ReadLine( )).Length != 0) { Console.WriteLine(line); writer.WriteLine(line); writer.Flush( ); } client.Close( ); server.Stop( );
NetworkStream stream = client.GetStream( ); StreamReader reader = new StreamReader(stream); StreamWriter writer = new StreamWriter(stream);
Console.WriteLine("Waiting for Client..."); TcpClient client = server.AcceptTcpClient( ); Console.WriteLine("Connected with a client");
class SimpleTcpServer { public static void Main( ) { TcpListener server = new TcpListener(IPAddress.Any, 9050); server.Start( );
using System; using System.Net; using System.Net.Sockets; using System.IO;
TCP Echo Server Using TcpListner Class
16
13-Week, SWE-344, Dr. S. Ahamad
•The protocol used in the above client-server example can be summarized as follows:
17
•One important thing that must be decided when writing a network program is the protocol - the rule that governs communication between the server and the client.
Protocol of TCP Echo Server
running.
13-Week, SWE-344, Dr. S. Ahamad
18
where ipaddress and port are the IPaddress and the port number on which the server is
•C:\>telnet ipaddress port
•To start the Telnet, simply go to command window and type:
windows platform.
•Microsoft Telnet is a program used to test server application, that comes with all
•After development of TCP server , the stage comes to test it
Microsoft Telnet
•TcpClient( ) 13-Week, SWE-344, Dr. S. Ahamad
using the Connect method discuss below.
19
card. Both this and the last constructor need to be connected to a remote server
•The second constructor is suitable for machines having more than one interface
•TcpClient(IPEndPoint localEP)
•This constructor is by far the most frequently used. You just need to give the host name or ip address followed by the port number of the server.
•TcpClient(string host, int port)
•Constructors of TcpClient Class
number on which a Tcp server is listening to.
TcpClient instance must be bound to a remote IP and port number. The IP and port
•While a TcpListener object must be bound to a local IP and port number, a
Creating TCP Client Using TcpClient Class
13-Week, SWE-344, Dr. S. Ahamad
•After a client is done, the Close method is called to disengage from the server.
•Close( )
•NetworkStream GetStream( )
•Methods of TcpClient Class •void Connect(string host, int port)
Few Important Methods of TcpClient Class
20
}
} 13-Week, SWE-344, Dr. S. Ahamad
String line = null; do { Console.Write("Enter Message for Server : "); line = Console.ReadLine(); writer.WriteLine(line); writer.Flush(); if (line.Length != 0) { line = "Echo: "+ reader.ReadLine(); Console.WriteLine(line); } } while(line.Length != 0); client.Close();
String input = reader.ReadLine(); Console.WriteLine(input);
NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); StreamWriter writer = new StreamWriter(stream);
class SimpleTcpClient { public static void Main() { TcpClient client = new TcpClient("localhost", 9050);
using System; using System.Net; using System.Net.Sockets; using System.IO;
TCP Client Using TcpClient Class
21
13-Week, SWE-344, Dr. S. Ahamad
Thanking You
22