Introduction
This is a simple implementation of the TCP client-server relationship.
You need to compile the server and the client programs separately. Before compiling, change the IP address in both programs to match that of your machine .
Note. You can get the IP address of your machine if you run 'ipconfig' from the command prompt in Windows NT/2000 m/c's.
When the server program is run, it will indicate at which IP it is running and the port it is listening to. Now the client program is run, so as to establish a connection with the server.
When a connection is established the server will display the IP address and Port from where it has accepted the connection and the client will ask for the string which is to be transmitted to the server.
The server on receipt of the string will display it and send an acknowledgement which will be received by the client.
The client can be either run from the same machine as the server or from a different machine. If run from a different machine then a network connection should exist between the machines running the server and client programs
Source code
/* Server Program */
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class serv
{
public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse("172.21.5.99"); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList=new TcpListener(ipAd,8001);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint );
Console.WriteLine("Waiting for a connection.....");
Socket s=myList.AcceptSocket();
Console.WriteLine("Connection accepted from "+s.RemoteEndPoint);
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]));
ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
/* Client Program */
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
public class clnt
{
public static void Main()
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("172.21.5.99",8001); // use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
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);
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]));
tcpclnt.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
Yan ZhiweiPosted Sep 25, 2012, 1:35 AM
Good.thank you very much.
zubii ahaPosted Feb 13, 2011, 7:46 AM
thank you very much..it was help full and i also found you in Codeproject ...
Former memberPosted Feb 9, 2010, 2:54 PM
Thank you for this simple example, it really helped me a lot!
JonathanPosted Nov 16, 2009, 5:53 PM
Is there a way to implement the client without using tcpclient? Is there like a SocketClient like used in java that could be used in c#
latin musicPosted May 13, 2009, 10:47 AM
I tried to put the client code in a mobile device with WCE 5.0 or windows Mobile 5.0 But it does not work. Someone knows how to do it? thanks latinmusic
DavidPosted Nov 3, 2008, 4:46 PM
I couldn't get it to work either. I added my machine IP address and opened port 8001. Then complied both apps and it still displays nothing. Has anyone got this thing to work? Thanks, dtittle
gopal kannanPosted Oct 23, 2008, 1:36 AM
hi friends.. i want to know how to send a mail or communicate using tcp client/server concept in a windows application.. urgent friends...
arnold moloPosted Mar 20, 2007, 8:56 AM
Hello, how do I run this sample? I have copied and past your code to a visual studio project, but when I compile, nothing happen. Is there any setup to do? Thanks