Sunday, 24 April 2011

Network Program in C#.Net

Client - Server Communication

  The term client and server refer to these roles.The client program initiates communication,while the server program waits passively for and  then responds to client that contact it.
 How does a client find out a server's IP Address and port number?.Let us see 

 IP Address:
        An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication.

Port Number:
    A port number is a way to identify a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.

Protocol:
   A protocol is an agreement about the packets exchanged by communicating programs .Standard set of rules to be followed to transfer the packets such as TCP/IP, UDP,SMTP,FTP etc..

Socket:
   A socket is an abstraction through which an application may send and receive data in much the same way as an open file allows an application to read and write data to stable storage.

Client Program:

// Two Way Communication Using TCP/IP

 using System;
 using System.Text;
 using System.IO; 
 using System.Net.Sockets;
 class twoclient
 {
 public static void Main(string[] args)
 {
 string server=args[0];
 int port=Int32.Parse(args[1]);
 string s1;
 TcpClient client=null;
 NetworkStream netstream=null;
 try
 {
 client=new TcpClient(server,port);
 Console.WriteLine("Connected to server:");
 netstream=client.GetStream();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
for(;;)
{
 Console.WriteLine("Enter the string:");  // Send the message
 s1=Console.ReadLine();
 byte[] buffer=Encoding.ASCII.GetBytes(s1);
netstream.Write(buffer,0,buffer.Length);
if(s1=="bye")
goto x;
byte[] buf=new byte[32];     //Receive the message
netstream.Read(buf,0,buf.Length);
Console.WriteLine("received {0}",Encoding.ASCII.GetString(buf,0,buf.Length));
string s2=Encoding.ASCII.GetString(buf,0,buf.Length);
}
x:
netstream.Close();
client.Close();
}
}

Server Program:

 using System;
 using System.Text;
 using System.Net;
 using System.Net.Sockets;
 class twoserver
 {
 private const int size=32;
 public static void Main(string[] args)
 {
 string s1,s2;
  int port= (args.Length ==1)?Int32.Parse(args[0]):7;
  TcpListener listener=null;
  try
  {
  listener=new TcpListener(IPAddress.Any,port);
  listener.Start();
  }
  catch(Exception e)
  {
  Console.WriteLine(e.Message);
  }
  for(;;)
  {
  TcpClient client=null;
  NetworkStream netstream=null;
  client=listener.AcceptTcpClient();
  netstream=client.GetStream();
  Console.WriteLine("handling client");
  for(;;)
  {
  byte[] buffer=new byte[size];
  netstream.Read(buffer,0,buffer.Length);
  Console.WriteLine("echoed string {0}:",Encoding.ASCII.GetString(buffer,0,buffer.Length));
  s2= Encoding.ASCII.GetString(buffer,0,buffer.Length);
  Console.WriteLine(s2);
  Console.WriteLine("Enter the string:");
  s1=Console.ReadLine();
  byte[] buf=Encoding.ASCII.GetBytes(s1);
  netstream.Write(buf,0,buf.Length);
  if(s2== "bye")
  break;
}
  netstream.Close();
  client.Close();
 }
 }
 }

//One Way Communication Using UDP

Client program:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
class udpclient
{
public static void Main(string[] args)
{
string s1;
string server=args[0];
int port=Int32.Parse(args[1]);
for(;;){
Console.WriteLine("Message");
s1=Console.ReadLine();
byte[] buffer=Encoding.ASCII.GetBytes(s1);
UdpClient client=new UdpClient();
client.Send(buffer,buffer.Length,server,port);
IPEndPoint remote = new IPEndPoint(IPAddress.Any,0);
byte[] buf=client.Receive(ref remote);
Console.WriteLine("reply:{0}",Encoding.ASCII.GetString(buf,0,buf.Length));
client.Close();
}
}
}

Server Program:

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;
class udpserver
{
public static void Main(string[] args)
{
string s1;
int port=Int32.Parse(args[0]);
UdpClient client=null;
try
{
client=new UdpClient(port);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
IPEndPoint remote=new IPEndPoint(IPAddress.Any,0);
for(;;)
{
byte[] buffer=client.Receive(ref remote);
Console.WriteLine("reply:{0}",Encoding.ASCII.GetString(buffer,0,buffer.Length));
Console.WriteLine("message:");
s1=Console.ReadLine();
byte[] buf=Encoding.ASCII.GetBytes(s1);
client.Send(buf,buf.Length,remote);
}
}
}

  These  are the way to transform the packets client to server and server to client using protocol such as TCP/IP or UDP.



No comments:

Post a Comment