Indy TCP客户端/服务器,其中客户端充当服务器 [英] Indy TCP Client/Server with the client acting as a server

查看:186
本文介绍了Indy TCP客户端/服务器,其中客户端充当服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在以下情况下使用Indy的 TIdTCPClient TIdTCPServer

How can Indy's TIdTCPClient and TIdTCPServer be used in the following scenario:

Client  ---------- initate connection -----------> Server
...
Client  <---------------command------------------- Server
Client  ----------------response-----------------> Server
...
Client  <---------------command------------------- Server
Client  ----------------response-----------------> Server

客户端启动连接,但充当 (等待命令并执行它们)。

The client initiates the connection, but acts as a "server" (waiting for commands and executing them).

OnExecute 方法 TIdTCPServer 无法正常运作在这种情况下(至少我没有得到它的工作良好)。

The OnExecute approach of TIdTCPServer does not work well in this case (at least I am not getting it to work well). How could I do this?

推荐答案

没有什么能阻止你这样做与Indy的TIdTCPServer组件。

There is nothing preventing you from doing this with Indy's TIdTCPServer component.

TIdTCPServer只设置连接。你需要实现其余的。

A TIdTCPServer only sets up the connection. You'll need to implement the rest. So the sequence of the actual sending and receiving can be whatever you want.

将此代码放在您的TIdTCPServer组件的OnExecute事件中:

Put this code in your TIdTCPServer component's OnExecute event:

var
  sName: String;
begin
  // Send command to client immediately after connection
  AContext.Connection.Socket.WriteLn('What is your name?');
  // Receive response from client
  sName := AContext.Connection.Socket.ReadLn;
  // Send a response to the client
  AContext.Connection.Socket.WriteLn('Hello, ' + sName + '.');
  AContext.Connection.Socket.WriteLn('Would you like to play a game?');
  // We're done with our session
  AContext.Connection.Disconnect;
end;

以下是如何设置TIdTCPServer的简单方法:

Here's how you can setup the TIdTCPServer really simply:

IdTCPServer1.Bindings.Clear;
IdTCPServer1.Bindings.Add.SetBinding('127.0.0.1', 8080);
IdTCPServer1.Active := True;

这告诉服务器只监听环回地址,端口8080.这可以防止

This tells the server to listen on the loopback address only, at port 8080. This prevents anyone outside of your computer from connecting to it.

然后,要连接您的客户端,您可以转到Windows命令提示符处,并键入以下内容:

Then, to connect your client, you can go to a Windows command prompt and type the following:

telnet 127.0.0.1 8080

以下是输出结果:


您的姓名是什么?

What is your name?

您好,Marcus。

您要玩游戏吗?

与主机的连接失败。

没有telnet?要在在Vista和7上安装telnet客户端,请按以下步骤操作。

Don't have telnet? Here's how to install telnet client on Vista and 7.

或者使用TIdTCP客户端,您可以这样做:

Or with a TIdTCP Client, you can do this:

var
  sPrompt: String;
  sResponse: String;
begin
  // Set port to connect to
  IdTCPClient1.Port := 8080;
  // Set host to connect to
  IdTCPClient1.Host := '127.0.0.1';
  // Now actually connect
  IdTCPClient1.Connect;
  // Read the prompt text from the server
  sPrompt := IdTCPClient1.Socket.ReadLn;
  // Show it to the user and ask the user to respond
  sResponse := InputBox('Prompt', sPrompt, '');
  // Send user's response back to server
  IdTCPClient1.Socket.WriteLn(sResponse);
  // Show the user the server's final message
  ShowMessage(IdTCPClient1.Socket.AllData);
end;

这里需要注意的一个重要的事情是ReadLn语句等待,直到有数据。这是背后的魔力。

An important thing to note here is that the ReadLn statements wait until there is data. That's the magic behind it all.

这篇关于Indy TCP客户端/服务器,其中客户端充当服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆