C语言 使用C进行套接字编程中的双向通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6810444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two-way communication in socket programming using C
提问by user537670
I have a small doubt in socket programming. i am able to send my data from client to server and my server processes the data. The o/p of the data processed, I want to send back to my client. So can we "write" the data back to the client using the same socket. I mean a server listens on a port before accepting connection and receiving data, so similarly, do i need to make my client listen to some other port (bind it some other socket) and make my server connect to that socket and transfer the data back. Any kind of example or explanation or references would be appreciated. Thanks a lot in advance.
我对套接字编程有一点疑问。我能够将我的数据从客户端发送到服务器,我的服务器处理数据。处理的数据的 o/p,我想发回给我的客户。那么我们是否可以使用相同的套接字将数据“写”回客户端。我的意思是服务器在接受连接和接收数据之前侦听端口,所以类似地,我是否需要让我的客户端侦听其他端口(将其绑定到其他套接字)并使我的服务器连接到该套接字并将数据传回. 任何类型的示例或解释或参考将不胜感激。非常感谢。
回答by Kerrek SB
Check out Beej's Network Programming Guidefirst of all.
首先查看Beej 的网络编程指南。
The basic screenplay of a server/client connection goes like this:
服务器/客户端连接的基本剧本是这样的:
- Server
listen()s on a fixed port, with a given socket. - Client
connect()s to a the server port; client obtains a socket. - Server
accept()s the connection, andaccept()returns a newsocket for the connection. - (Server continues listening on the original port with the original socket.)
- 服务器
listen()在固定端口上,具有给定的套接字。 - Client
connect()s 到服务器端口;客户端获得一个套接字。 - 服务器
accept()s 连接,并为连接accept()返回一个新的套接字。 - (服务器继续使用原始套接字侦听原始端口。)
For the specific connection with the client, the server write()s to the new socket it obtained when accept()ing the incoming connection. A busy server will have many, many sockets, but it will only ever need to bind()to one port. All connections come in to that one port, but the OS's networking protocol stack separates the data and makes it available at the connection-specific socket.
对于与客户端的特定连接,服务器write()会使用它accept()在传入连接时获得的新套接字。一个繁忙的服务器会有很多很多的套接字,但它只需要bind()一个端口。所有连接都进入该端口,但操作系统的网络协议栈将数据分开并使其在特定于连接的套接字上可用。
回答by Karoly Horvath
You don't need a new socket.
你不需要一个新的插座。
A socket is a duplex connection you can send data in both directions and you can even close the socket from one direction (don't want to write anymore) but still send data from the other direction.
套接字是一种双工连接,您可以在两个方向上发送数据,您甚至可以从一个方向关闭套接字(不想再写),但仍可以从另一个方向发送数据。
回答by Andrey Borisov
Technically it is right, the socket is duplex and you can send the data to the same socket you read from:
从技术上讲是正确的,套接字是双工的,您可以将数据发送到您从中读取的同一个套接字:
SOCKET s = socket()
... //Connect
int size = receive(s,...);
//make response
send(s, ...);
But in practice it depends on what are you going to do. It is possible to hang out socket if you have the following situation:
但在实践中,这取决于你要做什么。如果您有以下情况,可以挂出socket:
Process 1 sends very big data (<100K) over the socket by one send operation
Process 2 receives data from 1 by portions and sends small packets to 1 (~20b). It is not a
confirmations, but some external events. The situation goes into hangout, where the sending buffer of the 2 is full and it stops sending confirmations to 1. 2 and 1 are hanging in their send operations making a deadlock. In this case I'd recommend using two sockets. One for read, one for write.
进程 1 通过一次发送操作通过套接字发送非常大的数据(<100K)
进程 2 分部分接收来自 1 的数据并将小数据包发送到 1 (~20b)。它不是一个
确认,但一些外部事件。情况进入环聊,其中 2 的发送缓冲区已满,它停止向 1 发送确认。2 和 1 挂在它们的发送操作中,造成死锁。在这种情况下,我建议使用两个套接字。一为读,一为写。
回答by Andrey Borisov
Your socket is bi-directional, so there is no need to create another socket. Unless you are using some sort of middleware, such as Pub/Sub, there is no need to create another socket to enable bi-directional communication.
您的套接字是双向的,因此无需创建另一个套接字。除非您使用某种中间件,例如 Pub/Sub,否则无需创建另一个套接字来启用双向通信。
回答by mindthief
(Late answer, so mainly for anyone else who comes here looking for help)
(迟到的答案,所以主要是给来这里寻求帮助的其他人)
I recently put up an example client/server application that closely follows Beej's Guide to Network Programming (which was also recommended by Kerrek SB in his answer). If you're looking for a simple working example of client/server communication, maybe this will help:
我最近提出了一个示例客户端/服务器应用程序,它紧跟 Beej 的网络编程指南(Kerrek SB 在他的回答中也推荐)。如果您正在寻找客户端/服务器通信的简单工作示例,这可能会有所帮助:
https://github.com/countvajhula/dummyclientserver
https://github.com/countvajhula/dummyclientserver
In particular, no, your client does not need to set up a separate listening socket to receive data from the server -- after the server has accepted the connection from the client, the server can simply send data back to the client on the same socket.
特别是,不,您的客户端不需要设置单独的侦听套接字来接收来自服务器的数据——在服务器接受来自客户端的连接后,服务器可以简单地在同一个套接字上将数据发送回客户端.

