简体   繁体   English

在套接字上进行boost :: asio :: tcp双向通信

[英]boost::asio::tcp two-way communication on a socket

I am using boost::asio , TCP communication and C++ to create a client and a server that talk over a TCP socket . 我正在使用boost::asioTCP communicationC++来创建通过TCP socket TCP communication的客户端和服务器。 I need both the client and the server to be able send and receive data to each other. 我需要客户端和服务器都能够相互发送和接收数据。 I am able to make them communicate over a socket where the server is continuously sending some data and the client is continuously reading on the socket. 我能够使它们通过套接字进行通信,在该套接字上服务器不断发送一些数据,而客户端在套接字上不断读取数据。 It works. 有用。

Now for the other way communication : For client to send some data and the server to be able to read it, can I use the same socket for this? 现在以另一种方式进行通信: 为了使客户端发送一些数据而使服务器能够读取数据,我可以为此使用相同的套接字吗? Or Do I need to use a separate socket? 还是我需要使用单独的插座? Is it possible to read and write on the same socket for two applications communicating over TCP? 是否可以在同一套接字上为通过TCP通信的两个应用程序进行读写?

A boost::asio based example to illustrate this will be great if available. 一个基于boost :: asio的示例来说明这一点,如果可以的话,将是很棒的。 But I am able to find examples which are about only one-way communications. 但是我能够找到仅与单向通信有关的示例。

For client to send some data & server to be able to read it, can I use the same socket for this? 为了让客户端发送一些数据并使其能够读取服务器,我可以为此使用相同的套接字吗? Or Do I need to use a separate socket ? 还是我需要使用单独的插座? Is it possible to read and write on the same socket for two applications communicating over TCP ? 是否可以在同一套接字上为通过TCP通信的两个应用程序进行读写?

Yes. 是。 TCP is full duplex. TCP是全双工的。 Applications define the protocol of what/how messages are exchanged between client and server. 应用程序定义客户端和服务器之间交换消息的方式/方式的协议。 Weather they do asynchronously or synchronously, TCP doesn't care. 无论它们异步还是同步执行,TCP都不在乎。

The client server paradigm in tcp is one where the client initiates the connection and the server listens for incoming connections. tcp中的客户端服务器范例是客户端启动连接,服务器侦听传入连接的范例。 Once tge connection is established, it is up to higher layer protocol like http to establish how data is exchanged. 一旦建立了tge连接,就可以通过http之类的更高层协议来确定如何交换数据。 As far as tcp is concerned both client and server may send or receive data any way they choose. 就tcp而言,客户端和服务器都可以以他们选择的任何方式发送或接收数据。 Tcp is full duplex. Tcp是全双工的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM