Skip to content

Instantly share code, notes, and snippets.

@tgy
Created April 23, 2013 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgy/5445592 to your computer and use it in GitHub Desktop.
Save tgy/5445592 to your computer and use it in GitHub Desktop.
TcpListener server = new TcpListener(_ipAddress, _port);
server.Start();
Console.WriteLine("Listening on port " + _port);
Byte[] bytes = new byte[256];
while (true)
{
try
{
TcpClient client = server.AcceptTcpClient();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("New client connected :D");
NetworkStream stream = client.GetStream();
bool isOutput = stream.ReadByte() == 1;
if (!isOutput)
{
Console.WriteLine("He wants to talk! :O");
Console.ForegroundColor = ConsoleColor.White;
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
Console.WriteLine(" > " + Encoding.ASCII.GetString(bytes, 0, i));
stream.WriteByte(1);
}
}
stream.Close();
client.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment