Archives for June, 2005

21
Jun

TcpListener and TcpClient (an easy-to-use example)

During the weekend I needed some tool: Have to be able to listen on some port and to do something “useful”. First I was trying to use sockets. But I’ve found useful classes: TcpListener and TcpClient. It was exactly what I was looking for. Here you can “taste” the example.

using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace port_listen
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("Starting...");
      TcpListener server = new TcpListener(IPAddress.Parse("0.0.0.0"), 66);
      server.Start();
      Console.WriteLine("Started.");
      while (true)
      {
        ClientWorking cw = new ClientWorking(server.AcceptTcpClient());
        new Thread(new ThreadStart(cw.DoSomethingWithClient)).Start();
      }
      server.Stop();
    }
  }

  class ClientWorking
  {
    private Stream ClientStream;
    private TcpClient Client; 

    public ClientWorking(TcpClient Client)
    {
      this.Client = Client;
      ClientStream = Client.GetStream();
    }

    public void DoSomethingWithClient()
    {
      StreamWriter sw = new StreamWriter(ClientStream);
      StreamReader sr = new StreamReader(sw.BaseStream);
      sw.WriteLine("Hi. This is x2 TCP/IP easy-to-use server");
      sw.Flush();
      string data;
      try
      {
        while ((data = sr.ReadLine()) != "exit")
        {
          sw.WriteLine(data);
          sw.Flush();
        }
      }
      finally
      {
        sw.Close();
      }
    }
  }
}
14
Jun

Lide a binarni kod – pro zasmani :-)

Na svete je 10 druhu lidi: ty kteri rozumi binarnimu kodu a ti, kteri mu nerozumi.

14
Jun

První zápisek

Hmm, tak jsem dneska od tymu serveru vyvojar.cz dostal blog. Nevim co smysluplneho bych takhle hned napsal, takze kdo sem omylem zavita, necht se zatim podiva na http://www.cincura.net/.