Binding a socket connection to different port on different machine? - java

I have proxy server (on windows machine) that accepts client requests (using java sockets) and I have several internal nodes(unix machines) for processing these requests (in local area network). How to bind the incoming socket connection to a different machine on different port ?
for example I have an incoming connection from client (xxx.xxx.xxx.xxx:5000) to my proxy server (yyy.yyy.yyy.yyy:6000) and I want to bind this TCP Connection to a node on (zzz.zzz.zzz.zzz:7000).
Please let me know different possible ways in achieving this scenario ?
Thanks in Advance !

You cannot bind a connection to another machine. A proxy is supposed to:
accept an inbound connection from a client
create its own client connection to the next server (typically the client would specify this, unless you handle this in your proxy's configuration)
pass data back and forth between the two connections as needed
So, a client would connect to your proxy at yyy.yyy.yyy.yyy:6000, then your proxy would connect to zzz.zzz.zzz.zzz:7000 and start monitoring both connections for inbound data. Any data received on either connection would need to be sent to the other connection. Repeat until one of the connections is closed, then close the other connection.

Related

Can we create two sockets with same portnumber

I am trying to create a client-server model using socket programming in Java. I have multiple clients connecting to a server socket, but once the connection is lost, I need to reconnect to the server but using the same port number for the client. I have data stored on the server with respect to the port number through which it came. Is it possible to get the same port number for a socket again?
The server has no control over which port a client connects from.
On the client side, however, a socket can be bind()'ed to a specific local IP/Port before it is then connect()'ed to the server. Just note that it may take some time for the OS to release the port from the previous connection before it can be reused again. And also, if the client has to connect through a proxy/router to reach the server, the IP the server sees will be the proxy/router's IP, not the client's IP, and there is no guarantee that the port which the server sees will be the same port which the client is using.
The real question is, why are you relying on something unreliable like a client ip/port to store your data? I would suggest using a unique ID to identify the data, like say a user login, or a server-generated ID that is given to the client. If the client disconnects and reconnect, it can just login/send back the same ID.

Socket Connection Issue

I know one socket connection are established by both Server Socket and Client Socket.
And I read some documents said one Server Socket could serve many Client Sockets, means one Server Port could server multi Client Ports.
1.But I wonder that does Server use random ports to server different Clients after connection under hood, or Server just uses the same port listening and serving many client's connections ?
2.If so, when I implement a Server and Client Socket Connection, could I random a new port to establish a new Server Socket and tell Client to reconnect to new Server Socket, and the listening Server Socket just keep listening other clients ? it means use different port to server different clients ?
3.And what is the advantage of using one Server Socket(port) to server many Client? and advantage of using multi Server Sockets(ports) to server different Clients?
Thank you
The two value that idenify each end point, ip address and port number often called socket.
A server socket listens on a single port. All established client connections on that server are associated with that same listening port on the server side of the connection.Multiple connections on the same server can share the same server-side IP/Port pair as long as they are associated with different client-side IP/Port pairs, and the server would be able to handle as many clients as available system resources allow it to.
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(4200);
Here u can attach your http port with socket.io.
by using a random client-side port, in which case it is possible to run out of available ports if you make a lot of connections in a short amount of time.
for more detail visit this site

Moving socket ownership to another machine on the same LAN

I am thinking to a scenario where there is a front machine, behind a router, that accepts incoming connections with the purpose of doing load balancing.
After having established what is the right server for the job, it simply yield to it the socket so that the designed server can send data directly to the client.
Is that possible to "reparent" in some way an active socket connection, without further passing through the load balancing server, or the only way is to notify the client to open a new connection?
Passing a socket to a process on another machine is not possible. Your load balancer is going to have to participate in the response using the original request socket.
Either that or you will have to develop a protocol between the client and load balancer where the load balance tells the client which server the client should redirect its request to.

Java multiple connection to server in dynamic ports

I need to have a UDP server which allow me to receive/send informations from/to clients which dynamically will open a socket with a free port (so it will be differente from device and device). The client will send and receive in the same port, so the server must be able to communicate with it.
how could I set the server to stay open in every port? if I had 250 thousand users how could I handle them without tails problem and preventing the port to be occupied from another client?
I thought about open every port with different sockets in a different thread, but I don't know if this is a correct way.
A UDP Server can listen and be open on only one port. All clients can send data to that port. The server will have to handle each data and respond if needed to the peer who sent its data. This should happen even if more than one client wish to send data to server. In UDP context one client will not hog the server port.(Unless the application is badly written).

Multiple java socket connections

I have created this game bot where it connects to the game, and starts playing.
My problem is that i can't start more than one of these as the other then won't work.
Is is possible that if i run 2 instances of the same program the sockets are interfering with each other ? After all, they do connect to the same IP with the same port ?
And sometimes after i close(just closing cmd) the program is unable to connect again. Is that cause i didn't close the connections right ?.
I hope this is enough else i'll just have to post my source code
Best regards.
It's possible to connect to the same socket/port several times. Actually a socket is a double peer: {client ip/ client port}{server ip/server port}. When you connect to a server, your client port is assigned dynamically. You will have a new and different client port per client. So it should work unless the server side forbid it.
You should have a server that listens for multiple connections. A server is bound to a port and once that port is in use another application cannot use it. So for the server just have one instance. Multiple clients can connect to this IP/Port as long as the Server accepts multiple connections.
If a client connects to the server and the other clients stop working this may be because the server does not support multiple clients. To do this you need to use multi threading in the server. The server should accept a client socket and create a new instance of a client with it's own StreamReader/Writer objects.
http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html
if you are working with a specific TCP port, then there is a close-wait period that this port cannot be claimed temporariliy for some time. also multiple programs cannot listen the same TCP port. Use threads.

Categories