Concept | Server | Client


1. TCP/IP (Transmission Control Protocol / Internet Protocol)?
It is a communication protocol used by many networks for communication.

2. Network ?
A group of computers connected.

3. Protocol?
Set of rules for a communications

4. Window Service?
A service is a computer program run by OS automatically whenever OS is started.

5. Server?
A program waits for some one for communications.

6. Client?
Some one trying to communicate (Again both Listening and Talking) with a server.

7. Socket ?
A connecting point for communication between The Server and a Client.
we mean we are talking about a connection point on the Server
one Server serving many clients
All clients are connected to server on the same point (Socket) on the server
but have different individual connecting points on them.

8. Port?
a positive integer number used to identify a point of communication.
9. Socket Vs port?
23.456.44.56 : 8001


Basic
1. Server process starts at well known port where client awares of
2. Client process starts on any port .


Host Server Implementation
step to create TCP/IP server. (Host Server)
1. Create TcpListener with host server IP Address.

IPAddress ipAd = IPAddress.Parse("10.26.205.16");
TcpListener listener =new TcpListener(ipAd, 8001);
listener.Start();
2. Create socket to listen client connection (Host Server)
Socket soc = listener.AcceptSocket();
soc.RemoteEndPoint // IP where client connects from


3. Create network stream (passing socket) to communicate between Host Server <-> Client
NetworkStream ns =new NetworkStream(soc);
StreamReader sr=new StreamReader(ns);
StreamWriter sw=new StreamWriter()ns);


4. User sr and sw to read/write message from client.
reading : sr.ReadLine(); // Read message from client program
Writing : sw.WriteLine("Message"); // write to client program

Client Implementation.
1. Create TcpClient to connect to Host Server

Try{
TcpClient tc =new TcpClient();
tc.Connect("Host IP" , 8001);
}
Catch
{
Fail to connect to Host Server
}
2. Create NetworkStream with TcpClient to send and receive data with Host Server

NetworkStream ns = tc.GetStream();
3. Create Read/Write stream to pass and write data
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
sr.ReadLine();
sw.WriteLine();
sw.Flush();
ns.Close();



/* This method on server fires up when a client succeeds to connect to server.
public void OnClientConnect(IAsyncResult asyn)
{
try
{


}
}


/* User defined SocketPacket class containing references to : socket , xth clientnumber , databuffer byte
public class SocketPacket
{
public System.Net.Sockets.Socket m_currentSocket;
public int m_clientNumber;

public SocketPacket(System.Net.Sockets.Socket socket,
int clientNumber)
{
m_currentSocket = socket;
m_clientNumber = clientNumber;
}
// Buffer to store the data sent by the client
public byte[] dataBuffer = new byte[1024];
}

@Copy right of Soon Lim 2006. All Right Reserved