Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with console server

siegsieg Posts: 2Member
Hi
I'm writing for the second time. I have a problem with this
console aplication i am practicing for the first time making console
aplications i'm making something like telnet server but it isn't doing
anything please help me, when the client conects to server it isn' writing in console window anything and not sending to
client any information and also the client imediatly disconects! or if you can send me a working example of a console server.
Thanks!!!!!


program sturm;
{$APPTYPE CONSOLE}

uses
SysUtils, forms,
ScktComp;

type
TMyObject = class

public
procedure OnClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientRead(Sender: TObject; Socket: TCustomWinSocket);
end;


var
ServerSocket : TServerSocket;
MyObject: TMyObject;

procedure TMyObject.OnClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
i: integer;
begin
WriteLn('Client connect ' + Socket.RemoteAddress + ':' + IntToStr(Socket.RemotePort));
{Посылаем ВСЕМ клиентам сообщение}
for i := 0 to ServerSocket.Socket.ActiveConnections-1 do begin
ServerSocket.Socket.Connections[i].SendText('+OK Sturm.NET Server v0.1 ready <' + 'localhost' + '>');
end;
end;

procedure TMyObject.ClientRead(Sender: TObject; Socket: TCustomWinSocket);
begin
WriteLn('Client message ' +Socket.ReceiveText);
end;

begin
ServerSocket := TServerSocket.Create(nil); // создаем объект - серверное гнездо
ServerSocket.Port := 4567; // порт задан "от фонаря" !!
ServerSocket.ServerType := stNonBlocking; // для простоты - неблокирующий режим
MyObject := TMyObject.Create; // создаем объект - обработчик событий серв.гнезда
ServerSocket.OnClientConnect := MyObject.OnClientConnect; // назначаем обработчик на подключение ублюдка
ServerSocket.OnClientRead := MyObject.ClientRead; // назначаем обработчик на прием месаг ублюдка
WriteLn('Sturm.NET Server v0.1');

try
ServerSocket.Active := True; // старт серв.гнезда
ServerSocket.Active := False; // halt
while not Application.Terminated do begin
Application.ProcessMessages; // process messages in the message queue
//Sleep(10); // don't use 100% CPU
end;

finally
ServerSocket.Free; // разрушаем серв.гнездо
MyObject.Free; // разрушаем объект-обработчик
end;
end.

Sign In or Register to comment.