Monday, August 17, 2009

Message Transfer between Server and Client

This code can be used to transfer messages between a client and a server. This client server are two applications running upon the same computer, or multiple computers connected over a network. Changing the IP address within the code to your own computer’s IP address will initiate the communication between the client server of the same computer.

IP address can be found by going to Start->Run, then type cmd and press enter to enter the command prompt. After this type ipconfig in the command prompt and press enter and the IP address of your computer will be displayed.

Client Code

Write the following using statements if they are not initially written:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Collections.Generic;

Now write the following code (tried and tested in a console application) :

try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(” /* IP Address of Server */”, 8001);

// use the IP address as in the server program

Console.WriteLine(”Connected”);
Console.Write(”Enter the string to be send : “);

String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();

ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine(”Transmitting…..”);

stm.Write(ba, 0, ba.Length);

// The string has been transmitted but we are waiting for an acknowledgement from the server telling us that the message has been successfully received.

byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);

for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));

// The acknowledgment has been received and displayed

tcpclnt.Close();
}

catch (Exception e)
{
Console.WriteLine(”Error: ” + e.StackTrace);
}

Note that these try catch statements are written because they are already created functions present within the C# compiler that catch the exceptions, and perform the exception handling for us. However they do not catch every possible exception, but still they catch a lot of exceptions to be used as good programming practises.

—————————————————————————————————————————————————————————-

Server Code

Write the following using statements if they are not initially written:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;

Now write the following code (tried and tested in a console application) :

try
{
IPAddress ipAd = IPAddress.Parse(” /*IP Address of client */ “);
TcpListener myList=new TcpListener(ipAd,8001);

myList.Start();
// The server is waiting for incoming client connections

Socket s=myList.AcceptSocket();

// The connection has been established

byte[] b=new byte[100];
int k=s.Receive(b);
Console.WriteLine(”Recieved…”);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));

// The message has been received

ASCIIEncoding asen=new ASCIIEncoding();

s.Send(asen.GetBytes(”The string was recieved by the server.”));
// The acknowledgement has been sent

s.Close();
myList.Stop();

}

catch (Exception e)
{
Console.WriteLine(”Error….. ” + e.StackTrace);
}

If this code is being used in different computers, then make sure that your firewall is turned off and the computers are connected within a secure network (for all the rights of the connection to be granted).

No comments:

Post a Comment