Error : “Transport endpoint is already connected”

大城市里の小女人 提交于 2020-01-24 18:46:30

问题


I am trying to develop a small chat server with C.

For a simple chat server,

  • ( Transport endpoint ) === ( socket ) ?
  • Do i have to use one socket per client, or can I reuse a socket for multiple clients ? If so, how ?
  • Is there a standard way of doing this ?
  • Any good references available ?

Can i get to see some sample implementations ? I have to use gcc compiler and c language for this assignment.


回答1:


You need one socket/client and no, you cannot reuse sockets. If you have to handle multiple clients you can:

  • create one thread per client and use blocking I/O (preferably with timeout).
  • create single threaded program and use demultiplexing with select/poll/epoll/kqueue and use non-blocking I/O.
  • use asynchronous I/O.

For C socket communication examples The Unix Network Programming book is probably the best source. It has ample of example programs and explanation.




回答2:


  1. ( Transport endpoint ) === ( socket ) ?

NO. "Endpoint" means IP address with Port number. Socket presents one "Session" and session consists of two endpoints, local endpoint(IP, port) and remote endpoint(IP, port).

  1. Do i have to use one socket per client, or can I reuse a socket for multiple clients ? If so, how ?

One socket per one session. That means a server needs to create a new socket for each remote endpoint( client ). You can reuse socket when it's not in use anymore. Look for SO_REUSEADDR socket option.

  1. Is there a standard way of doing this ?

Not sure what you are asking. A standard way for chat service or for server/client model? For chat service, look for IRC. Server/Client programming model is well documented. You can Google it.

  1. Any good references available ?

http://beej.us/guide/bgnet/

Now I believe you understand what the error message means.



来源:https://stackoverflow.com/questions/7140438/error-transport-endpoint-is-already-connected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!