简体   繁体   English

多端口监听套接字linux

[英]multiport listening socket linux

I am writing a multithreaded server application, in C (linux), that must listen to 2 different port numbers, say listen to port no 3000 and 4000, for different clients connecting to it to serve different functionality (actual function is executed by a worker thread, main thread runs indefinitely and spawns of new workers upon new connections). 我正在编写一个多线程服务器应用程序,在C(linux)中,它必须侦听2个不同的端口号,比如侦听端口号3000和4000,用于连接到它的不同客户端以提供不同的功能(实际功能由工作人员执行)线程,主线程无限期运行,并在新连接时产生新工作者)。 I am not sure if select would work here as we can have multiple socket connections but all associated with same port number. 我不确定select是否可以在这里工作,因为我们可以有多个套接字连接,但都与相同的端口号相关联。 I tried sequentially binding to sock_1 and sock_2. 我尝试顺序绑定到sock_1和sock_2。 When I run client_1, everything works as expected. 当我运行client_1时,一切都按预期工作。 But, when I run client_2, I get an error on connect() from client side. 但是,当我运行client_2时,我在客户端的connect()上收到错误。 If select() can be used here, please let me know how to do it. 如果可以在这里使用select(),请告诉我如何操作。 Any help much appreciated ! 任何帮助非常感谢! Thanks ! 谢谢 !

(PS sock_1 refers to port no 3000 and sock_2 refers to port no 4000, client_1 refers to client thats seeking service from port 3000 and client_2 refers to client seeking service from same server from port no 4000) (PS sock_1指的是端口号3000,sock_2指的是端口号4000,client_1指的是从端口3000寻求服务的客户端,而client_2指的是从端口号4000从同一服务器寻求服务的客户端)

You have totally misunderstood socket and port, these two are different things. 你完全误解了socket和port,这两个是不同的东西。 A port can have multiple sockets. 端口可以​​有多个套接字。 but you can bind your listing socket(passive socket) to only a single port. 但您可以将列表套接字(被动套接字)绑定到仅一个端口。 Before going any further read this 在进一步阅读之前

If you want that your application listen on two different ports, have you bind two different sockets with it. 如果您希望应用程序在两个不同的端口上侦听,请使用它绑定两个不同的套接字。

For a Quick reference a select in single process can only have upto 1024 socket descriptors. 对于快速参考,单个进程中的select只能有1024个套接字描述符。 So if you are using a single process model then a select can only handle 1024 connections. 因此,如果您使用的是单个流程模型,那么select只能处理1024个连接。 Also read C10k problem see what suits your need. 另请阅读C10k问题,看看哪些适合您的需求。

Using select() is great if your application has to accept data from more than one socket at a time since it will block until any one of a number of sockets is ready with data. 如果您的应用程序必须一次接受来自多个套接字的数据,那么使用select()是很好的,因为它将阻塞,直到许多套接字中的任何一个已准备好数据。 One other advantage to select() is that you can set a time-out value after which control will be returned to you whether any of the sockets have data for you or not. select()的另一个好处是你可以设置一个超时值,之后控制权将返回给你,无论任何套接字是否有你的数据。
Following Links are useful to you: 以下链接对您有用:
http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html

Question-20 问题20
http://www.scribd.com/doc/7296598/Unix-Network-Programming-Volume-I-The-Sockets-Networking-API-3rd-Edition http://www.scribd.com/doc/7296598/Unix-Network-Programming-Volume-I-The-Sockets-Networking-API-3rd-Edition

Chapter-6: What I understood your application 'not required select()' Have a look on code at following link: http://kturley.com/simple-multi-threaded-web-server-written-in-c-using-pthreads/ 第6章:我理解你的应用程序'不需要选择()'查看以下链接的代码: http//kturley.com/simple-multi-threaded-web-server-written-in-c-using -pthreads /

Have call two different bind() for different ports in your code? 为代码中的不同端口调用两个不同的bind()? As pointed by @Rahul Gautam in his answer . 正如@Rahul Gautam在回答中指出的那样。

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

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