I have created a simple communication between two WPF windows application, by which you can send any number of data as string. Right now i am closing TCP Connection just after successful result. You can do this multiple time.
So Here is the code
Client Side Code: (Sender)
//Publish this location to other opend application
void sendData(string data)
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect("127.0.0.1", 8001);
// use the ipaddress as in the server program
String str = data;
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
tcpclnt.Close();
}
catch (Exception em)
{
Console.WriteLine("Error..... " + em.StackTrace);
}
}
Server Side Code: (Receiver)
//To Listen the Connection type code for an event, As i write here for a button
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
TcpListener myList = new TcpListener(ipAd, 8001);
myList.Start();
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 em)
{
Console.WriteLine("Error..... " + em.StackTrace);
}
}
Reference Site : http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
So Here is the code
Client Side Code: (Sender)
//Publish this location to other opend application
void sendData(string data)
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect("127.0.0.1", 8001);
// use the ipaddress as in the server program
String str = data;
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
tcpclnt.Close();
}
catch (Exception em)
{
Console.WriteLine("Error..... " + em.StackTrace);
}
}
Server Side Code: (Receiver)
//To Listen the Connection type code for an event, As i write here for a button
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
TcpListener myList = new TcpListener(ipAd, 8001);
myList.Start();
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 em)
{
Console.WriteLine("Error..... " + em.StackTrace);
}
}
Reference Site : http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
No comments:
Post a Comment