简体   繁体   English

多个套接字供客户端连接

[英]Multiple sockets for clients to connect to

Is it possible to have multiple sockets, which can either by TCP or UDP in one program? 一个程序中是否可以有多个套接字,可以通过TCP或UDP来实现?

For example: SocketOne: TCP socket at port 4567; 例如:SocketOne:端口4567处的TCP套接字; socketTwo: TCP socket at port 8765; socketTwo:端口8765上的TCP套接字; socketThree: UDP socket at 7643. socketThree:UDP套接字在7643。

The families will be AF_INET, and addresses will be INADDR_ANY for each. 这些族将为AF_INET,每个族的地址将为INADDR_ANY。

I bind and listen for TCP, and just bind for UDP. 我绑定并监听TCP,仅绑定UDP。

What makes me doubt being about to do this is, how do I wait for a client at each socket together. 使我感到疑惑的是,如何在每个套接字上一起等待客户端。

I know that the code below won't work, but I don't know what else, or how to, explain what I'm trying to say. 我知道下面的代码行不通,但是我不知道其他什么或如何解释我想说的话。

while (1)  
{   
    connected = accept(socketOne, (struct sockaddr *)&client_addr,&sin_size);

    connected = accept(socketTwo, (struct sockaddr *)&client_addr,&sin_size);

    bytes_read = recvfrom(socketThree,recv_data,1024,0,(struct sockaddr *)&client_addr, &addr_len);

}

男人选择

There are a few real world examples of this. 有一些现实的例子。 FTP has a control and data port which both use TCP and multimedia applications will use SIP or RTSP connections for control (TCP) and mulitple RTP and RTCP port (UDP) for each data stream received. FTP有一个控制和数据端口,使用TCP和多媒体应用程序都将使用SIP或RTSP连接进行控制(TCP),并为接收到的每个数据流使用多个RTP和RTCP端口(UDP)。

select or poll are used on unix and on Windows there are the OVERLAPPED apis to do this non-preemptively . 在Unix上使用select或poll并在Windows上使用OVERLAPPED api来非抢先地执行此操作。 Alternatively, this can be done with multiple threads. 或者,可以使用多个线程来完成。

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

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