Skip to content

Instantly share code, notes, and snippets.

@choucavalier
Created April 23, 2013 17:22
Show Gist options
  • Select an option

  • Save choucavalier/5445592 to your computer and use it in GitHub Desktop.

Select an option

Save choucavalier/5445592 to your computer and use it in GitHub Desktop.

Revisions

  1. Valentin Iovene created this gist Apr 23, 2013.
    35 changes: 35 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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();
    }
    }