1

I do not have much familiarity with Delphi 2010 and am having trouble with using components ClientSocket and ServerSocket. The question is simple: I am trying to send a text from a client to a server using the following code:

cliente.Socket.SendText('call');

On the server side I writed the following code:

procedure TForm6.ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
s: string;
begin
s:=Socket.ReceiveText;
if s = 'call' then
begin
showmessage('Client is Calling');
end;
end;

However, the server dont displaying the message. Could help me once more?

1 Answer 1

4

In D2009+, SendText() and ReceiveText() do not work correctly with Unicode strings. It is better to use SendBuf() and ReceiveBuf() directly instead.

With that said, TClientSocket and TServerSocket have been deprecated for a LONG time. You should use a different component set, such as Indy (which also ships with Delphi), eg:

IdTCPClient1.IOHandler.WriteLn('call');

procedure TForm6.IdTCPServer1Execute(AContext: TIdContext);
var
  s: string;
begin
  s := AContext.Connection.IOHandler.ReadLn;
  if s = 'call' then
  begin
    // TIdTCPServer is a multi-threaded component,
    // but ShowMessage() is not thread-safe...
    Windows.MessageBox(0, 'Client is Calling', '', MB_OK);
  end;
end;
Sign up to request clarification or add additional context in comments.

4 Comments

It's really, the sockets are deprecated for a long time, but I did not know how to use another component to the same result, but now I know. Thank you.
Anyway for those who are still using the sokets in Delphi 2010 a function that responds to the initial question:function ReturnStringReceive(Socket: TCustomWinSocket): String; Var Res: AnsiString; Begin SetLength(res, Socket.ReceiveBuf(Pointer(nil)^, -1)); SetLength(res, Socket.ReceiveBuf(Pointer(res)^, Length(res))); Result := String(res); End; and to call: procedure TForm9.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); begin label1.caption := ReturnStringReceive(Socket); end;
@V.Salles: that is not a true solution, because it does not take message boundaries into account. Imagine what would happen if you called SendText() multiple times before ReturnStringReceive() is called. ReceiveBuf(nil, -1) (which is the same as ReceiveLength()) is likely to return a value that includes data from more than one SendText().
@V.Salles: the correct way to handle this is to either: 1) preceed each string with its length, so the receiver can read the length first and then read the data, or 2) delimit the messages with a unique identifier not found in the string data, so the receiver can keep reading until it finds the delimiter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.