I am using Synapse with blocking sockets and try to simply send text to the connected client. Here comes the code:
var
SServer: TTCPBlockSocket;
SClient: TTCPBlockSocket;
implementation
//Create and initialize the Sockets.
procedure TForm1.FormCreate(Sender: TObject);
begin
SClient := TTCPBlockSocket.Create;
SServer := TTCPBlockSocket.Create;
SServer.Bind('127.0.0.1', '12345');
SClient.Connect('127.0.0.1', '12345');
end;
//Wait for connections.
procedure TForm1.FormShow(Sender: TObject);
begin
SServer.Accept;
//SServer.Listen; <- Could also work here?
end;
//Send the string to the connected server.
procedure TForm1.Button3Click(Sender: TObject);
begin
SClient.SendString('hi server');
end;
//Receive the string from the client with timeout 1000ms and write it into a memo
procedure TForm1.Button2Click(Sender: TObject);
var buf: string;
begin
Memo1.Lines.Add(SServer.RecvString(1000));
end;
First, I click Button 3, then I click Button2. Doing so, there is nothing getting written inside the memo1 field.
Shouldn't this work?
#
****EDIT:****
#
According to skramads comment, I have now split it up into 2 programs. Here we go:
Client:
var
SClient: TTCPBlockSocket;
implementation
procedure TForm2.Button1Click(Sender: TObject);
begin
SClient.SendString(Edit1.Text);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
SClient := TTCPBlockSocket.Create;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
SClient.Connect('127.0.0.1','12345');
end;
Server:
var
Form1: TForm1;
SSocket: TTCPBlockSocket;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
SSocket.Bind('127.0.0.1','12345');
SSocket.Listen;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines.Add(SSocket.RecvString(1000));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SSocket := TTCPBlockSocket.Create;
end;
Still, this does not work as intended. I just get no data over there.
Any ideas?