将40个套接字绑定到40个不同的IP地址 [英] Binding 40 Sockets to 40 different IP addresses

查看:115
本文介绍了将40个套接字绑定到40个不同的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究udp服务器/客户端应用程序。我希望我的单个服务器一次处理40个客户端。为此,我想创建40个专用线程,每个线程专用于一个客户端。由于每个客户端有40个线程,我想创建40个专用套接字。但问题是我不知道将绑定的40个IP地址()我的套接字。 (因为就我而言,我必须将()绑定到我的服务器IP地址。)通常,当只有单个套接字时,我将bind()绑定到INADDR_ANY。但是我应该绑定()每个40个套接字的IP地址应该是什么?请帮我。任何评论/帮助都表示赞赏。

I am working on udp server/client application. I want my single server to handle 40 clients at a time. For this, I want to create 40 dedicated threads, each dedicated for one single client. Since there are 40 threads one for each client, I want to create 40 dedicated sockets as well. But the problem that I don''t know what will be the 40 IP addresses to which I shall bind() my sockets. (since as far as I now, I have to bind() to my Server\s IP address.) Normally I bind() to "INADDR_ANY" when there is only single socket. But what should be the IP addresses at which I should bind() each of my 40 sockets? Please help me. Any comment/ help is appreciated.

推荐答案

你正在以错误的方式尝试这一点。服务器应该有一个套接字(IP地址和端口),它接受连接,然后它将为每个连接的客户端生成一个单独的线程。如果您按照自己的方式进行操作,则需要40个IP地址并在一个指定端口上进行侦听,或者使用一个IP地址侦听40个不同的端口。前者是浪费IP地址空间而后者是屁股配置管理的明智之处,因为每个客户端都必须配置不同的端口才能连接到服务器。

因为我无法想象这是一个要求我建议你看一下互联网上提供的大量样本,看看如何实现使用套接字的服务器并沿着那条路走。



套接字服务器示例:

http://www.tutorialspoint.com/unix_sockets/socket_server_example.htm [ ^ ]



线程C Windows:

使用C和Win32进行多线程处理 [ ^ ]



问候,



- Manfred
You''re trying this the wrong way round. The server should have one socket (IP-adress and port) on which it accepts connections and then it will spawn a separate thread for each client that connects. If you were to do it your way you''d need either 40 IP adresses and listening on one designated port or use one IP adress listening on 40 different ports. The former is a waste of IP adress space and the latter a pain in the arse configuration management wise as each client would have to be configured with a different port to connect to the server.
Since I cannot imagine this being a requirement I suggest you look at the ample samples available on the internet to see how a server using sockets is implemented and go by that route.

Socket server sample:
http://www.tutorialspoint.com/unix_sockets/socket_server_example.htm[^]

Threading C Windows:
Multithreading with C and Win32 [^]

Regards,

— Manfred


如果你想要一个多客户端服务器(谁不会?)你不需要听40个不同的套接字,而是一个单独的套接字。在接收连接时,产生一个线程来处理连接......或者如果你想上学就过程一个过程:)



其实这里很好示例开始Winsock编程 - 带客户端的多线程TCP服务器 [ ^ ]
If you want a multi client server (who wouldn''t?) you shouldn''t need to listen on 40 different sockets, but rather a single socket. On recieving a connection, spawn a thread to handle the connection... or a process if you want to go old school :)

In fact here''s a good example Beginning Winsock Programming - Multithreaded TCP server with client[^]


这篇关于将40个套接字绑定到40个不同的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆