Monday, December 21, 2009

Simple Socket Client to connect to a server

private void ConnectToServer()
{
using (TcpClient tc = new TcpClient("127.0.0.1", 12))
{
NetworkStream ns = tc.GetStream();
StreamWriter sw = new StreamWriter(ns);
StreamReader sr = new StreamReader(ns);

sw.WriteLine("test message");
sw.Flush();
System.Console.WriteLine(sr.ReadLine());
}
}
This code can be used to connect to a server using a socket and send a simple message to it.
Post a Comment