交流电插座编程 - 接受多个后续连接 [英] C Socket Programming - Accept Multiple Subsequent Connections
问题描述
这是一个后续这个问题;作为我的认识过程已经演变等具有我的问题。
This is a follow-up to this question; as my understanding of the process has evolved so has my question.
我有一个监听器将通过任意数据,HTTP请求,网络套接字,然后通过TCP传送。这工作正常第一个请求,但听者不接受后续新的请求。
I have a listener that will pass arbitrary data, HTTP requests, to a network socket which is then delivered over TCP. This works fine for the first request but the listener does not accept subsequent new requests.
我的问题是:
如果我有袜子=接受(监听器,(结构地址*)及罪恶,和放大器; sinlen);
然后,基于socket函数引用,听者插座保持打开状态,我应该能够重新调用接受()
任意次数的后续请求。它是否正确?如果是这样,有人可以比我更熟悉套接字编程,请解释一下这是如何code可能看?
If I have sock=accept(listener,(struct addr *)&sin, &sinlen);
then, based on the socket function reference, the listener socket remains open and I should be able to re-call accept()
any number of times for subsequent requests. Is this correct? If so, can someone more familiar than I with socket programming please explain how this code might look?
先谢谢了。
-
感谢所有的答复。
推荐答案
是的,你可以接受()
监听套接字上许多倍。为了服务多个客户端,你需要避免阻塞I / O - 也就是说,你不能只是从插座和块,直到数据进来阅读有两种方法:你可以在自己的线程服务于每一个客户(或其。自己的过程中,通过使用叉()
在UNIX系统上),或者你可以使用选择()
。在选择()
的功能是检查是否可用在任何一组文件描述符的数据的方法。它可用于UNIX和Windows。
Yes, you can accept()
many times on the listening socket. To service multiple clients, you need to avoid blocking I/O -- i.e., you can't just read from the socket and block until data comes in. There are two approaches: you can service each client in its own thread (or its own process, by using fork()
on UNIX systems), or you can use select()
. The select()
function is a way of checking whether data is available on any of a group of file descriptors. It's available on both UNIX and Windows.
这篇关于交流电插座编程 - 接受多个后续连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!