Seeing multiple servers on a network - java

I am busy with a project where I'm creating a basic client/server chat application which allows a user to create a server on their local network on a port of their choice and then have multiple clients connect to that server by specifying the IP and port number of the server(So far so good).
I would like to how I can have clients see all the possible servers they can connect to on their local network when there are multiple servers running on their local network over different ports and then allowing them to connect to one. I am using basic Java socket programming for this project.

You could have
use a UDP based protocol where each server publishes it's IP:port every second.
you could have a service where each server registers. You could chat with that to get the list of all services.
The nice approach of the later option is you can use one of your chat services for server discovery. When you want to get the list, you send a message to a channel on that server which all the servers are listening to and they respond with a chat message.

Related

How can I use java sockets to communicate between different networks?

I have java programs for my client and server, and they work fine within the same wifi network. But I need the clients to be able to connect to the server from the open internet. In questions like these
How to connect client and server with the help of ip address which are connected to internet through different wifi?
https://coderanch.com/t/667020/java/Socket-connections-networks
the solution is to manually reroute a port to the server from the router, making it open to connections from the outside. Is there a way to do this with just software on the server? I don't understand why manually dedicating ports is necessary since of course other applications on my computer (like games) that I install communicate with their servers back and forth without me having to manually go in and flip switches.
How can I achieve this with just software running on my server?
If there isn't another way, how do other applications communicate openly without manual router changes, and will opening up ports through my router result in security issues?
You would need to change the architecture of your application. Currently, your server is behind a firewall which blocks connections from the internet - you want this! If you allowed all traffic from the internet to connect indiscriminately to your server, it would be very vulnerable to attack.
Other applications install and communicate without port-forwarding because the developer provides a server on the internet to act as a proxy between clients. The client connects out to the internet which is generally not blocked on home networks. Internal connections going out are considered less harmful than connections coming in.

How to use a server for redirecting clients to direct connecting to each other

I want to be able to connect (java)clients to each other without opening ports in their router or forwarding on their local network. I want to mimic the behavior of webtrc but in java, using a server for signaling. The server will have to receive session keys from both clients that wants to connect to each other, sending the first clients key to the second client and vice versa. After the signaling, the both clients can connect to each other using this key, and the packets will find their way through the router without opening ports and forwarding.
The goal is to make a framework out of this to be able to write applications that don't require a server with huge bandwidth that can read from one client and write to the other, for every pair of clients that wants to send data to each other. What I'm asking for is simply guidelines for this could be implemented.
TLDR: I want to be able to directly connect two clients, in two completely different networks, with the help of a public server and without any router configuration for the clients.
This technique is called Hole punching
"Hole punching is a computer networking technique for establishing communications between two parties in separate organizations who are both behind restrictive firewalls. Used for applications such as online gaming, P2P and VoIP, both clients establish a connection with an unrestricted third-party server that uncovers external and internal address information for them. Since each client initiated the request to the server, the server knows their IP addresses and port numbers assigned for that session, which it shares one to the other." from wikipedia
also take a look on this answer

Socket communication between Gears on OpenShift

I have 2 DIY Gears running simple a Java Client-Server Application with Sockets.
What I want to do is sent a command via Socket to Gear1, which sends a command via Socket to Gear2, then sends back to the user whatever data Gear2 sent back.
The problem is that I can't connect to Gear2 from Gear1.
Gear1 address = 127.13.55.1:16000
Gear2 address = 127.7.21.129:16001
I forwarded port 16000 on Gear1 so that I can communicate with it from a local client. But when I try to connect to Gear2 from inside Gear1 I get the error message: java.net.NoRouteToHostException: No route to host.
Is there some configuration I forgot about?
The two gears will not be able to communicate with each other on those ports. Inter-gear communication is blocked on non standard ports, except in special cases with scaled applications. You would have to use port forwarding between the gears for this to work, the same as you do from your local machine.

Connect two clients without port forwarding

I wonder how to connect two clients over the internet without opening ports on the server side. If this is impossible, how does e.g. Skype establish a connection between the talking people? (Speech goes directly to the clients)
Is there a way to use a "master server" where both clients connect to and then getting a direct connection together?
Thanks for every answer.
Skype uses the concept of Supernodes, that relay communications on behalf of two clients. The Skype user directory is decentralized and distributed among the supernodes in the network.
The Supernodes try to make both ends to negotiate directly the connection, but if it's not possible due to firewalls, NAT, etc they relay the traffic on behalf of two or more clients.

Java nat traversal for chat application

I am trying to create a java chat application for my networking class. As of right now I am stuck trying to connect to someone behind a different router. The way I have my project right now is I have a client program and a server program. The client programs first logs into the server program which logs their IP and port in a database and then the server gives them back the list of their friends with their IPs and ports. Then the client closes down the connection to the server and tries to connect to another client using the information the server sent back. So far my program only works connecting to the server and getting the friends IP and port but when I use those values to connect to the other client I cant connect.
socket = new Socket();
socket.setReuseAddress(true);
socket.setKeepAlive(true);
socket.setSoLinger(true, 10);
socket.bind(new InetSocketAddress(Port));
socket.connect(new InetSocketAddress(host, SERVER_PORT));
reusePort = socket.getLocalPort();
Above is a snippet of java code used to connect to the server then below is what i do on the client side.
ss = new ServerSocket(reusePort);
So now technically I am listening on the same port I used to connect to the server with which is logged in and is retrievable to another client and is in the NAT table with my ip and port. I am not sure what I am missing or if there is some protocol or something that I have to do. I have looked at TCP and UDP hole punching but I am not sure how that is actually accomplished or how to implement it.
Any suggestions would be appreciated.
If you want to send a message you'll need to set up port forwarding on any device that acts as a server (any device which creates a socket server). Port forwarding is done on the Router. The reason you cannot connect to the other client is because they are hidden behind their routers firewall. Their address to the rest of the world is actually the address of the router, not of their physical computer. On their local network they have a different address then what the rest of the world sees, and the router figures out what messages from the outside world need to be sent to the client based on an address translation table.
Given your architecture, this would mean that all clients need to have their routers doing port forwarding, which is of course unfeasible (imagine gtalk or aim requiring users to do port forwarding).
The more common architecture is to have the Server do the work of rebroadcasting messages to the connected clients and maintain tables to lookup whose talking with who. This way there is a single server which will need a static ip (or be port forwarded), and all users are simply clients which connect to the server socket and read messages from it.
For actual code describing the second architecture please see http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html. Then the machine which is running the server code either needs a static ip or if it is behind a router needs traffic from the port it is listening on to be forwarded.
So on the server code you will bind to the ip assigned from your router (something like 192.168.1.2 at some port say 5000). Then go to your routers configuration page (it may be 192.168.1.1 see http://www.wikihow.com/Port-Forward/Open-Ports-on-a-Linksys-Router), and forward port 5000 to the address 192.168.1.2.
The Interactive Connectivity Establishment (ICE) protocol combines various NAT traversal utilities such as the STUN and TURN protocols in order to offer a powerful mechanism that allows Offer/Answer based protocols such as SIP and XMPP to traverse NATs.
This project provides a Java implementation of the ICE protocol that would be usable by both SIP and XMPP applications. The project also provides features such as socket sharing and support for Pseudo TCP.
ice4j is maintained by the Jitsi community.
ice4j

Categories

Resources