简体   繁体   English

如果只想接收C / TCP中的数据,是否需要connect()?

[英]Is connect() necessary if I want only to receive data in C/TCP?

if I want ONLY to receive data from a client, and not send data out, is it necessary have a connect() in the code of my server? 如果我只希望从客户端接收数据而不发送数据,是否需要在服务器代码中使用connect()?

Or is it sufficient to have the following? 或拥有以下内容是否足够?

socket();
bind();
listen();
accept();

This describes the basic principle of server-client interaction. 这描述了服务器-客户端交互的基本原理。 As you can see, the client must connect to the server before any interaction. 如您所见,客户端必须在进行任何交互之前connect到服务器。

Once you've built a socket descriptor with the socket() call, you can connect that socket to a remote server using the well-named connect() system call. 使用socket()调用构建套接字描述符后,即可使用名称明确的connect()系统调用将该套接字连接到远程服务器。

Also the sequence you mentioned is in the server . 您提到的顺序也在server If you want to only receive data from server, just do the read in client and write in server. 如果您只想接收来自服务器的数据,则只需执行read入客户端并write服务器即可。 But connect is neccessary. 但是connect是必需的。

服务器 - 客户端

You need to use socket and connect for the client. 您需要使用socket并为客户端connect

The TCP protocol does send data to the server and recieves data back from the server (even if you are not transferring data) in the form of ACKs TCP协议确实将数据发送到服务器,并以ACK的形式从服务器接收数据(即使您不传输数据)。

Please read up on the TCP/IP protocol. 请阅读TCP / IP协议。

if I want ONLY to receive data from a client, and not send data out, is it necessary have a connect() in the code of my server? 如果我只希望从客户端接收数据而不发送数据,是否需要在服务器代码中使用connect()?

It is never necessary to have connect() in the code of a server. 永远不需要在服务器代码中包含connect()。

Clients call connect(). 客户端调用connect()。 Servers call accept(). 服务器调用accept()。 By definition. 根据定义。

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

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