We are developing a network application based on C/S, in this application the server needs to transfer files to clients and sometimes receive files from clients. So in the server application, for every client it needs to create a control socket and many transfer sockets.
My question is how we can create and define a socket as role of control socket or transfer socket? ( below are deleted: And how can the server application distinguish between the control socket and the transfer sockets? ). Currently the servers use a listening socket, and for all new connections established by accept(listening socket), it stores them equally in an array, finally lets the client register them to the server as control sockets or transfer sockets(means before transferring a file, the client needs to tell which socket it wants to use as control socket and which sockets it want to use as transfer sockets).
I don't think this is a good patter for creating control sockets and transfer sockets between server application and the clients. So is there any other patters that is suitable for this kind of problems?
how we can create this control socket and all the other transfer sockets?
Same way you create any other socket. I don't understand the question.
And how can the server application distinguish between the control socket and the transfer sockets?
By being told by the client, and remembering it in some sort of data structure per client. Or maybe you should use different listening ports for control sockets and transfer sockets. Or maybe the transfer socket should be a callback as in FTP.
Related
I am using a websocket library on server for establishing socket connection.
https://github.com/websockets/ws
I have a more than one server in cluster, I want to know how can I use same socket connection object on another server in cluster.
And also I want to know what is the best option for webchat implementation native websocket or socket.io
You cannot use the same actual socket object across multiple servers. The socket object represents a socket connection between a client and one physical server process. It is possible to build a virtual socket object that would know what server its connection is on, send that server a message to then send out over the actual socket from that other server.
The socket.io/redis adapter is one such virtual ways of doing this. You set up a node.js cluster and you use the redis adapter with socket.io. It uses a central redis-based store to keep track of which serve process each physical connection is one. Then, when you want to send a message to a particular client from any of the server processes, you send that message through socket.io and it looks up for you in the redis database where that socket is connected, contacts that actual server and asks it to send the message to that particular client over the socket.io connection that is currently present on that other server process. Similarly, you can broadcast to groups of sockets and it will do all the work under the covers of making sure the message gets to the clients no matter which actual server they are connected to.
You could surely build something similar yourself for plain webSocket connections and others have built pieces of it. I'm not familiar enough with what exists out there in the wild to offer any recommendations for a plain webSocket. There plenty of articles on scaling webSocket servers horizontally which you can find with Google and read to get started if you want to do it with a plain webSocket.
I'm working in a project where we need to connect clients to devices behind LAN networks.
Brief description: there are "devices" connected, in a home for example, under a LAN created by a router. These devices create a full webserver, operating under linux, and using nodejs as the backend implementation language. They also have access to Internet, through the public IP of the router. On the other side, there are clients which can choose to which device to connect to.
The goal is to connect the clients with the webServer created by any device.
Up to now, my idea is to try to implement something similar to how TeamViewer works. As I understand, Teamviewer has a central server, which the agents connect to. When an agent connects to the central server, this one gets hold of the TCP connection, keeping it alive. When another client wants to access to the first client, the server bypasses both TCP connections. That way the server acts like a proxy, where it additionally routes the TCP connections. This also allows to connect to clients under LAN or firewalls (because the connections are created always from the clients).
If this is correct, what I would like to implement is a central server, in nodejs as well, which manages a pool of socket connections coming from the different active devices, and when a client wants to connect to one specific device, the server bypasses the incoming TCP connection of the client with the already existing connection of the device.
What I first would like to know is if this is possible in nodejs. My idea is to keep the device connections alive, so clients can inmediately connect to them, creating some sort of pool of device connections.
If implemented in C, I guess I could get hold of the socket descriptor, keeping it alive, and bypassing it to the incoming client request. But in nodejs I can't seem to find any modules that manage TCP connections.
Are there any high level npm packages which do this function? Else, is it possible to use lower level modules (like net) which have those functionalities.
Ideally I would like to implement it with high level modules (express), but if it's not possible, I could always rewrite the server using low level modules.
Thanks in advance
I am trying to design a client server model for chat application.Do I need to have two different socket with different port to send and recieve.Actually I have 3 nodes.1 server and 2 client(or slaves).Message passed by client 1 for client 2 will always routed via server.What kind of port and number of socket i should use.Please exaplain I doing all these things in C++ using winsock and pthread API's.
Typically you use TCP (SOCK_STREAM). You need just one socket to for incoming connections. bind(2) it to a specific port. You can accept(2) any number of connections on it.
Several threads can wait (accept(2)) on the same socket. The OS will dispatch one connection to just one thread ;)
For each outgoing connection you have to create a new socket but you don't have to bind it. The OS will choose an appropriate IP/port itself.
I'm having difficulty finding a robust socket library for doing local tcp socket connections in node.js.
I'm a big fan of using libraries like SockJS or Socket.io for client/server socket connections but I know those use websockets which are different from regular sockets.
I'm wondering if I could use a Websocket library for local connections with similar performance as just using regular sockets or would that include lots of undesired networking overhead?
Basically I want to achieve these three things with sockets and I don't think the native networking module can do them out of the box.
Monitor the health of each socket in it's pool (Alive or dead).
Attach an id to each socket so you know where data is coming from
Build the data from the chunks sent through the sockets
WebSockets are a TCP-like connection, but which actually runs on top of an established HTTP(s) connection (which itself runs within a TCP-connection). This means:
There is additional overhead: all data gets put into special frames, also you have the HTTP connection establishment additionally to the normal TCP connection establishment.
They are not compatible with normal sockets, e.g. you need a WebSockets-aware peer on the other side of the connection.
Apart from that they add no additional reliability or features to the underlying TCP connection. E.g. your requirements are already possible with normal sockets.
How web servers serve multiply requests simultaneously. I think that sockets are used during the communication betwen client and server. But what if two clients try to connect one socket at one time? This socket will be used and second client could not be connected?
Servers can accept and service multiple client connections at the same time, usually by using multiple threads, overlapped/asynchronous I/O, or even forking multiple processes. But in any case, multiple clients are serviced in parallel to each other. A new client connection is refused only if the server has run out of resources to accept more clients.