Professional Documents
Culture Documents
SWE-344
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
Concurrent Servers:
This type of server runs multiple concurrent processes to serve many request at a
time.
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.
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
•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
•IP address and port number has vital role in client server application development
as well as in Client/Server communication.
•These two classes are required in creating both server and client objects.
•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.
•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
•UdpClient( ) : Bound the new instance to any local IP and any local port
•Methods
•public byte[ ] Receive( ref IPEndPoint remoteEP )
• Returns a UDP datagram sent by a remote host.
•int Send( byte[ ] data, int size, string host, int port )
•Sends a datagram to the specified remote IPEndPoint .
•void Close( )
•Closes the UDP connection.
class SimpleUdpServer{
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;
•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.
•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: