突触:无法从套接字接收数据 [英] Synapse: Cannot receive data from Socket
问题描述
我正在将Synapse与阻塞套接字一起使用,并尝试简单地将文本发送到连接的客户端.代码如下:
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;
首先,我单击按钮3",然后单击"Button2".这样做,在memo1字段中什么都没写.
First, I click Button 3, then I click Button2. Doing so, there is nothing getting written inside the memo1 field.
这不行吗?
********
根据skramads的评论,我现在将其分为2个程序.我们开始:
According to skramads comment, I have now split it up into 2 programs. Here we go:
客户:
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;
尽管如此,这仍无法正常工作.我只是没有那边的数据.
Still, this does not work as intended. I just get no data over there.
有什么想法吗?
推荐答案
您应该阅读套接字通信的工作方式,例如此处(德语).简而言之:您在服务器端的listen()
套接字不用于通信本身,您必须调用accept()
来打开另一个套接字作为客户端的伙伴,并使用该套接字发送和接收数据.侦听套接字仅用于接受来自其他客户端的其他连接,然后可以使用它们在一个服务器和多个客户端之间进行并行通信.
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.
也许您应该首先检查一个简单的客户端/服务器演示应用程序.无论您使用Synapse,Indy还是底层API编程,原理都是相同的.
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.
这篇关于突触:无法从套接字接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!