Can I open 2 ports simultaneously with single websocket server

白昼怎懂夜的黑 提交于 2019-12-12 14:05:23

问题


I am a newbie at websocket programming. Currently I am working on a simple websocket server,using c, that can respond to a websocket client. I managed to get that working with 1 client and 1 server on a single port. I want to know if I could open 2 ports, so different clients could connect to the the different ports.

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

int main(int argc, char *argv[])
{
  int listenfd = 0, connfd = 0;
  struct sockaddr_in serv_addr;

  char sendBuff[1025];

  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&serv_addr, '0', sizeof(serv_addr));
  memset(sendBuff, '0', sizeof(sendBuff));

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY );
  serv_addr.sin_port = htons(8000);

  bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));

  listen(listenfd, 10);

  while (1)
  {
    connfd = accept(listenfd, (struct sockaddr*) NULL, NULL );

    close(connfd);
    sleep(1);
  }
}

回答1:


If you want multiple clients for 1 server, you need to use threading, you listen, then when you accept a client, create a thread and have him manage the request, and make the main thread go back to listening. Different ports is kinda difficult, because client needs to connect to a specific port, so how do you know which port to connect to, and thinking you want an undefined number of clients, or how do you manage which ports are free, and communicate it with to the client(and not iterate 1 by 1, on a port range).

I don't remember the C p_thread lib, but that would be a start.




回答2:


Yes, you can do that, but you will have to create different threads for the different ports. It may be difficult to implement.

refer to this link for more solutions




回答3:


Read up on how sockets work in general.

What accept does is create a connection between the connecting client and the server, this connection is in the form of a new socket. So, you have a socket listening for connections, and when one comes in, it will return a new socket for the client to talk to the server. The initial socket will continue to sit there, listening for further new connections.

That link gives a set of good information about how it all hangs together.

However, websockets aren't really like that - they use a single port (usually port 80) on the server side to read data from a connection, the client doesn't create a new socket, it reuses the existing connection that is channelled over the existing connection it has to the server (you don't 'create' a websocket, you upgrade a HTTP connection to a persistent TCP websocket one) and data that is read is then processed by yourself as a stream of messages on the single socket - not a socket per client. Its a message-based abstraction unlike TCPs stream-based abstraction.




回答4:


Yes ,Try adding listeners to different ports on the same server script.



来源:https://stackoverflow.com/questions/19812865/can-i-open-2-ports-simultaneously-with-single-websocket-server

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