Create a dynamic listening server with ICS in runtime.

how to create a service listening on specific port.

// this will create a listening socket on port 113 (Ident D)

// and welcome any client with a text

// when incoming text is received the OnServerData is trigged.



uses Wsocket;



// private declared

OnServerSessionAvailable(Sender: TObject; Error:

OnServerDataAvailable(Sender: TObject; Error: Word);





Procedure TForm1.OnServerSessionAvailable(Sender: TObject; Error: Word);

var

newclient : Twsocket;



begin

  with TWsocket(sender) do

  begin

    // uncomment this, and you will get a very simple identd server.

    {

    newclient := Twsocket.create(self);

    newclient.HSocket := Accept;

      with newclient do

      begin

        SendStr(getpeerport + ', 113 : USERID : UNIX : JoePezt' + #13#10)

        close;

        free;



        exit;


      end;

    }

    // send a welcome note to the client.

    sendstr('welcome' + #13#10);

  end;

end;



Procedure Tform1.OnServerDataAvailable(Sender: TObject; Error: Word);

var

received : string;

begin

  // we trim the received string to remove whitespaces and crlf

  received := trim(TWsocket(sender).Receivestr);



end;



procedure TForm1.cmdListenClick(Sender: TObject);

var

  server : Twsocket;

begin

  Server := Twsocket.create(self);

    with Server do

    begin

      OnSessionAvailable := OnServerSessionAvailable;

      OnDataAvailable := OnServerDataAvailable;

      Linemode := true;

      LineEnd := #13#10

      Addr := '0.0.0.0';

      port := '113'; // which port we use

      proto := 'tcp';

      listen;

    end;

end;

 

Share this article!

Follow us!

Find more helpful articles: