I Want To create a TCP/Server and receive data from Multiple Clients in text Format.Data Length is never more than 1024 chars. I am using the follow code But i am not sure is the correct way i think sould be a easiest way like myString=Socket.ReceiveText I am excpecting around 100+ simulate connections. Is it anything i can to for make it better?
indy is not a choice and i am working in Delphi 5
unit Unit1;
interface
uses
ScktComp, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
private
{ Private declarations }
public
{ Public declarations }
end;
TClientThread = class(TServerClientThread)
private
Received_text: string;
fsocketStream: TWinsocketStream;
public
procedure ClientExecute; override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TClientThread.ClientExecute;
var
Data: array[0..1023] of char;
begin
inherited FreeOnTerminate := TRUE;
fsocketStream := TWinSocketStream.Create(ClientSocket, 1000);
try
while not Terminated and ClientSocket.Connected do
try
FillChar(Data, SizeOf(Data), 0);
try
if fsocketStream.Read(Data, SizeOf(Data)) = 0 then
begin
ClientSocket.Close;
Terminate;
end;
except
ClientSocket.Close;
Terminate;
end;
Received_text := Data;
//Process Data HEre
//process the data Like Read From SQL and Take actions Depence on data received
//****************************************************************
try ClientSocket.sendtext('Hello From MultiThread Server'); except end;
finally
fsocketStream.Free;
end;
except
HandleException;
end;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
errorcode := 0;
end;
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TClientThread.Create(False, ClientSocket);
end;
end.
here is a second Example. I think for my meeds is better
Received_text:=Received_text+clientsocket.ReceiveText;
if pos(#13#10,Received_text)>0 Then
begin
try clientsocket.SendText(#13#10+'REC='+Received_text+#10#13);except end;
Received_text:='';
end;
TWinSocketStreamon each loop iteration. Create it once before entering the loop, free it after exiting the loop. "i think sould be a easiest way like myString=Socket.ReceiveText" - do not useReceiveText. All it does is return whatever bytes are currently in the socket without regard to format or structure. TCP messages require structure, you need to code for that. "indy is not a choice and i am working in Delphi 5" - Indy supports Delphi 5.Socket.ReceiveBuf()to read incoming data into a buffer, and then you can scan that buffer for(CR)LFso you are processing only complete strings. "I know abou indy i used them a lot but in real world tclientsocket and tserversocket its much better for my needs" - doubtful, they don't offer anything that Indy doesn't.