Synapse:无法从 Socket 接收数据

发布于 2024-08-01 21:13:06 字数 1996 浏览 5 评论 0原文

我正在使用具有阻塞套接字的 Synapse,并尝试简单地将文本发送到连接的客户端。 代码如下:

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;

首先,我单击“按钮 3”,然后单击“按钮 2”。 这样做后,memo1 字段内不会写入任何内容。

这不应该起作用吗?

#

****编辑:****

#

根据 skramads 的评论,我现在将其分成 2 个程序。 我们开始吧:

客户端:

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;

服务器:

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;

尽管如此,这并没有按预期工作。 我只是在那里没有得到任何数据。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法少女 2024-08-08 21:13:06

您应该阅读套接字通信的工作原理,例如此处(英文)或此处(德语)。 简而言之:您在服务器端listen()的套接字本身并不用于通信,您必须调用accept()来打开另一个套接字作为伙伴为客户端,并使用它来发送和接收数据。 侦听套接字仅用于接受来自其他客户端的其他连接,然后您可以使用这些连接在一台服务器和多个客户端之间并行通信。

也许您应该首先检查一个简单的客户端/服务器演示应用程序。 无论您使用 Synapse、Indy 还是低级 API 编程,原理都是相同的。

You should read how socket communications work, for example here (English) or here (German). In short: the socket you listen() on on the server side is not used for the communication itself, you have to call accept() to open another socket as the partner for the client, and use that one to send and receive data. The listening socket is used solely to accept other connections from other clients, which you can then use to communicate between one server and several clients in parallel.

Maybe you should first examine a simple client/server demo application. The principles are the same, whether you use Synapse, Indy or low-level API programming.

风苍溪 2024-08-08 21:13:06

如果你把它分成两个单独的程序,那么它会工作得更好。 阻塞调用就是这样做的......它们会阻塞直到完成。

if you break this into two separate programs, then it will work better. Blocking calls do just that...they block until they complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文