You are on page 1of 21

12-Week

SWE-344

Internet Protocols & Client


Server Programming
Dr. Shahanawaj Ahamad
Assistant Professor

12-Week, SWE-344, Dr. S. Ahamad 1


Outlines

• Client / Server Architecture


• Server class
• Connection-Oriented vs Connectionless Servers
• Different Options to Develop UDP Server & Client
• IP Address and Port Number
• IPAddress & IPEndPoint Classes
• IPAddress Class
• UdpClient Class
• UDP Client & Server Using UdpClient class

12-Week, SWE-344, Dr. S. Ahamad 2


Client / Server Architecture

•A network architecture in which each computer or process on the network is either


a client or a server
•Most of the Net Applications use the Client Server architecture.
•These terms refer to the two processes or two applications which will be
communicating with each other to exchange some information.
• One of the two processes acts as a client process and another process acts as a
server.

12-Week, SWE-344, Dr. S. Ahamad 3


Client / Server Architecture

Client Process
This is the process which typically makes a request for information. After getting
the response this process may terminate or may do some other processing.

Example:
Internet Browser works as a client application which sends a request to Web Server
to get one HTML web page

Server
Client
12-Week, SWE-344, Dr. S. Ahamad 4
Client / Server Architecture

Server Process
•A process which takes a request from the clients.
•After getting a request from the client,
•This process will do required processing and will gather requested information
and will send it to the requestor client.
• Once done, it becomes ready to serve another client.
• Server process are always alert and ready to serve incoming requests.

Example: Web Server keeps waiting for requests from Internet Browsers and as
soon as it gets any request from a browser, it picks up a requested HTML page and
sends it back to that Browser

Server
Client
12-Week, SWE-344, Dr. S. Ahamad 5
Classes of Servers

There are two classes of servers you can have:


Iterative Server:
•This is the simplest form of server where a server process serves one client at a
time
•After completing first request then it takes request from another client. Meanwhile
another client keeps waiting.

Concurrent Servers:
This type of server runs multiple concurrent processes to serve many request at a
time.

12-Week, SWE-344, Dr. S. Ahamad 6


Connection-Oriented vs Connectionless Servers

• Programmer can choose a connection-oriented server or a connectionless server


based on their applications.

TCP Server:
•Is a connection-oriented.
•A client must connect a socket to a server.
•TCP socket provides bidirectional channel between client and server.
•Lost data is re-transmitted.
•Data is delivered in-order.
•Data is delivered as a stream of bytes.
•TCP uses flow control.

12-Week, SWE-344, Dr. S. Ahamad 7


Connection-Oriented vs Connectionless Servers

Connectionless-Oriented (UDP)
•Is a connectionless.
•A single socket can send and receive packets from many different cleints.
•Best effort delivery.
•Some packets may be lost some packets may arrive out of order.
•Send and receive data as datagram

Advantage of TCP and UDP Servers


•It is simple for a single UDP server to accept data from multiple clients and reply.
•UDP is fast & supports Broadcasting & Multicasting
•It is easier to cope with network problems using TCP.

12-Week, SWE-344, Dr. S. Ahamad 8


Different Options to Develop UDP Server & Client

•Two different options to develop UDP Server & UDP Client applications
•First Option
•Using UdpClient class to develop UDP Server
•Using UdpClient class to develop UDP Client

•Second Option
•Using Socket Class to develop UDP Server
•Using Socket class to develop UDP Client

12-Week, SWE-344, Dr. S. Ahamad 9


IP Address and Port Number

•IP address and port number has vital role in client server application development
as well as in Client/Server communication.

12-Week, SWE-344, Dr. S. Ahamad 10


IPAddress & IPEndPoint Classes

•Before going to start the development of Client and server applications , it is


important to understand two basic classes:
•IPAddress class
•IPEndPoint class

•These two classes are required in creating both server and client objects.

12-Week, SWE-344, Dr. S. Ahamad 11


IPAddress Class
•This class is used to represent an IP address as an object.

•The two constructors of the IPAddress class are as follows:


• public IPAddress( long address )
• public IPAddress( byte[ ] address )

•These constructors are rarely used, as we hardly have the IP address represented in
bytes or long format.

•Instead of using these constructors, the static method, Parse( ) of the IPAddress
class, discussed below, is often used to create an instance of IPAddress.

•Important Methods of IPAddress Class are :


•static IPAddress Parse(String address)
•Takes an IP address as a string and returns an IPAddress object

•static bool IsLoopback( IPAddress address )


•Returns true if address is a loop back address
12-Week, SWE-344, Dr. S. Ahamad 12
IPAddress Class (Contd…)
•IPAddress class also has a number of public read-only properties as described
below:

•Important properties of IPAddress Class are :


•Any : Represents any IP address available on the local system
•Broadcast: Represents broadcast address.
•Loopback: Represents loopback address.

12-Week, SWE-344, Dr. S. Ahamad 13


IPEndPoint Class (Contd…)
•TCP/UDP client-server applications usually connect or listen to a pair of
IPAddress and a port number.

•The IPEndPoint class allows these two components to be represented as a single


object.

•The most frequently used constructor of this class has the form:
public IPEndPoint( IPAddress address, int port )

•It also has properties, IPAddress and Port, that can be used to get the
corresponding components of the end-point

12-Week, SWE-344, Dr. S. Ahamad 14


UdpClient Class

•UdpClient class is designed to simplify the process of creating UDP applications


•Since UDP is connectionless, there is no listener component.
•Both server and client applications can be created using UdpClient class
•Constructors

•UdpClient( ) : Bound the new instance to any local IP and any local port

•UdpClient(int port) : Bounds the new instance to a specific local port


•UdpClient(IPEndPoint localEP) : Bounds the new instance to a specific local IP
endpoint
•UdpClient(string host, int port) : Bounds the new instance to any local endpoint and
associated it with a default remote host and default remote port.

12-Week, SWE-344, Dr. S. Ahamad 15


UdpClient Class (Contd…)

•Methods
•public byte[ ] Receive( ref IPEndPoint remoteEP )
• Returns a UDP datagram sent by a remote host.

•int Send( byte[ ] data, int size, IPEndPoint remoteEP )


•Sends a datagram to the remote IPEndPoint specified by the remoteEP.

•int Send( byte[ ] data, int size, string host, int port )
•Sends a datagram to the specified remote IPEndPoint .

•void Close( )
•Closes the UDP connection.

12-Week, SWE-344, Dr. S. Ahamad 16


UDP Server Using UdpClient Class
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class SimpleUdpServer{

public static void Main( ) {


IPEndPoint localEP = new IPEndPoint( IPAddress.Any, 9050 );
UdpClient server = new UdpClient( localEP ) ;

Console.WriteLine("Waiting for a client...");

IPEndPoint remoteEP = new IPEndPoint( IPAddress.Any, 0) ; //dummy IP

byte[] data;

while(true) {
data = server.Receive( ref remoteEP) ;
Console.Write("Received from {0}: ", remoteEP.ToString( ) );
Console.WriteLine(Encoding.ASCII.GetString( data, 0, data.Length ) );
server.Send( data, data.Length, remoteEP );
}
}
}
12-Week, SWE-344, Dr. S. Ahamad 17
UDP Client Using UdpClient Class
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class SimpleUdpClient {
public static void Main( ) {
UdpClient client = new UdpClient("127.0.0.1", 9050);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

byte[ ] data;
string input;

while( true) {
Console.Write("Enter message for server or exit to stop: ");
input = Console.ReadLine( );
if (input == "exit")
break;

client.Send( Encoding.ASCII.GetBytes(input), input.Length ) ;


data = client.Receive(ref remoteEP);
Console.Write("Echo Received from {0}: ", remoteEP.ToString( ) );
Console.WriteLine( Encoding.ASCII.GetString( data, 0, data.Length ) );
}
Console.WriteLine( "Stopping client“ );
client.Close( );
}
}
12-Week, SWE-344, Dr. S. Ahamad 18
UDP Client & Server Using UdpClient class[Cont…]
•Details of Communication Protocol
•There must be a protocol based on which a server and client communicate. In
this case:

•The server first receives a message from client – so that it captures the client’s
EndPoint

•Then it echoes the message back to the client and the process is repeated.

•The reverse process takes place at the client.

•Note the following points arising from the connection-less nature of UDP:
•There is no AcceptClient method that returns the client’s socket.
•NetworkStream, StreamReader and StreamWriter cannot be used for exchange of
messages.
•The server can receive from any number of clients at the same time.
12-Week, SWE-344, Dr. S. Ahamad 19
Details of Receive( ) Method of UdpClient Class
•We do not need to create a fixed size array before calling the Receive method. The
method returns an array big-enough to store the received datagram.

•The Receive method uses the ref modifier to capture the IPEndPoint of the remote
client.

•The local client has to first create a dummy IPEndPoint and then use it to call the
Receive method.

•The Receive method will then replace the content of this dummy IPEndPoint with
that of the remote client:

12-Week, SWE-344, Dr. S. Ahamad 20


Thanking You

12-Week, SWE-344, Dr. S. Ahamad 21

You might also like