delphi编写mac程序_用Delphi编写可识别网络的应用程序

delphi编写mac程序

Of all the components that Delphi provides to support applications that exchange data over a network (internet, intranet, and local), two of the most common are TServerSocket and TClientSocket, both of which are designed to support read and write functions over a TCP/IP connection.

Delphi提供的所有组件都支持通过网络(互联网,内部网和本地)交换数据的应用程序,其中最常见的两个是TServerSocketTClientSocket ,它们都旨在支持通过TCP / IP的读写功能。 IP连接。

Winsock和Delphi套接字组件 ( Winsock and Delphi Socket Components )

Windows Sockets (Winsock) provides an open interface for network programming under the Windows operating system. It offers a set of functions, data structures, and related parameters required to access the network services of any protocol stacks. Winsock acts as a link between network applications and underlying protocol stacks.

Windows套接字(Winsock)为Windows操作系统下的网络编程提供了一个开放的接口。 它提供了访问任何协议栈的网络服务所需的一组功能,数据结构和相关参数。 Winsock充当网络应用程序与基础协议栈之间的链接。

Delphi socket components (wrappers for the Winsock) streamline the creation of applications that communicate with other systems using TCP/IP and related protocols. With sockets, you can read and write over connections to other machines without worrying about the details of the underlying networking software.

Delphi套接字组件(用于Winsock的包装器)简化了使用TCP / IP和相关协议与其他系统通信的应用程序的创建。 使用套接字,您可以读写与其他计算机的连接,而不必担心基础网络软件的详细信息。

The internet palette on the Delphi components toolbar hosts the TServerSocket and TClientSocket components as well as TcpClient, TcpServer, and TUdpSocket.

Delphi组件工具栏上的Internet调色板托管TServerSocketTClientSocket组件以及TcpClientTcpServerTUdpSocket

To start a socket connection using a socket component, you must specify a host and a port. In general, host specifies an alias for the IP address of the server system; port specifies the ID number that identifies the server socket connection.

要使用套接字组件启动套接字连接,必须指定主机和端口。 通常, 主机为服务器系统的IP地址指定一个别名。 port指定用于标识服务器套接字连接的ID号。

一个简单的单向程序发送文本 ( A Simple One-Way Program to Send Text )

To build a simple example using the socket components provided by Delphi, create two forms—one for the server and one for the client computer. The idea is to enable the clients to send some textual data to the server.

要使用Delphi提供的套接字组件构建一个简单的示例,请创建两种形式-一种用于服务器,一种用于客户端计算机。 这个想法是使客户端能够向服务器发送一些文本数据。

To start, open Delphi twice, creating one project for the server application and one for the client.

首先,打开Delphi两次,为服务器应用程序创建一个项目,为客户端创建一个项目。

服务器端: ( Server Side: )

On a form, insert one TServerSocket component and one TMemo component. In the OnCreate event for the form, add the next code:

在表单上,​​插入一个TServerSocket组件和一个TMemo组件。 在表单的OnCreate事件中,添加以下代码:


procedure TForm1.FormCreate(Sender: TObject);begin
ServerSocket1.Port := 23;
ServerSocket1.Active := True;end;

The OnClose event should contain:

OnClose事件应包含:


procedure TForm1.FormClose
(Sender: TObject; var Action: TCloseAction);begin
ServerSocket1.Active := false;end;

Client Side:

客户端:

For the client application, add a TClientSocket, TEdit, and TButton component to a form. Insert the following code for the client:

对于客户端应用程序,将TClientSocket,TEdit和TButton组件添加到表单。 为客户端插入以下代码:


procedure TForm1.FormCreate(Sender: TObject);begin
ClientSocket1.Port := 23;//local TCP/IP address of the server
ClientSocket1.Host := '192.168.167.12';
ClientSocket1.Active := true;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin
ClientSocket1.Active := false;end;procedure TForm1.Button1Click(Sender: TObject);beginif ClientSocket1.Active then
ClientSocket1.Socket.SendText(Edit1.Text);end;

The code pretty much describes itself: when a client clicks a button, the text specified inside the Edit1 component will be sent to the server with specified port and host address.

该代码几乎描述了自己:当客户端单击按钮时,Edit1组件内指定的文本将发送到具有指定端口和主机地址的服务器。

Back to the Server:

返回服务器:

The final touch in this sample is to provide a function for the server to "see" the data the client is sending. The event we are interested in is OnClientRead—it occurs when the server socket should read information from a client socket.

此样本中的最后一点是为服务器提供一种功能,以“查看”客户端正在发送的数据。 我们感兴趣的事件是OnClientRead,它发生在服务器套接字应从客户端套接字读取信息时。


procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);begin
Memo1.Lines.Add(Socket.ReceiveText);end;

When more than one client sends data to the server, you'll need a little more to code:

当多个客户端向服务器发送数据时,您将需要更多代码:


procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);var
i:integer;
sRec : string;beginfor i := 0 to ServerSocket1.Socket.ActiveConnections-1 dobeginwith ServerSocket1.Socket.Connections[i] dobegin
sRec := ReceiveText;if sRecr '' thenbegin
Memo1.Lines.Add(RemoteAddress + ' sends :') ;
Memo1.Lines.Add(sRecr);end;end;end;end;

When the server reads information from a client socket, it adds that text to the Memo component; both the text and the client RemoteAddress are added, so you'll know which client sent the information. In more sophisticated implementations, aliases for known IP addresses can serve as a substitute.

当服务器从客户端套接字读取信息时,它将把该文本添加到Memo组件。 文本和客户端RemoteAddress都被添加,因此您将知道哪个客户端发送了信息。 在更复杂的实现中,已知IP地址的别名可以替代。

For a more complex project that uses these components, explore the Delphi > Demos > Internet > Chat project. It's a simple network chat application that uses one form (project) for both the server and the client.

对于使用这些组件的更复杂的项目,请浏览Delphi>演示> Internet>聊天项目。 这是一个简单的网络聊天应用程序,它对服务器和客户端使用一种形式(项目)。

翻译自: https://www.thoughtco.com/write-network-aware-applications-with-delphi-4071210

delphi编写mac程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值