I am trying to make a two-way instant messaging app over two different networks. One of these networks is mine, which has port forwarding enabled(sends traffic on certain port to specific ip address). My problem is that I need a two way connection(sockets can only send to serverSockets, serverSockets can't send to sockets). Is there a way to connect to a computer via a pre-existing connection? Is there a library for this? ie. socket.connect(serverSocket.getConnection, 5001);
(I have made my own classes which handle all the Input/Output Streams and sockets, I just need a library for a function I can put in the class).
Asked
Active
Viewed 151 times
-2

anyGenericProgrammer
- 27
- 7
-
The statement, "One of these is mine, which has port forwarding enabled," is unclear. Do you mean that your firewall is configured to forward incoming traffic to a specific port to your computer? In that case, the other computer can establish a TCP connection, and your computer can receive and send messages with the other, right? Can you clarify what is missing in this arrangement? – erickson Feb 10 '20 at 22:55
-
So, I fixed my problem by using both the input and output streams of the client and server. And now I can send information 2-ways, which was ultimately the base of my problem. – anyGenericProgrammer Aug 12 '20 at 13:57
1 Answers
1
If what you're asking for is to have a computer exposed to the internet to directly connect to a computer behind a NAT, you might get your app working if you are able to implement something similar to reverse ssh tunneling
. See here and here, for a java library.
But I would recommend some sort of client-server approach for this, in which everyone connects to the server, and through the server they connect to each other.

Sir Beethoven
- 348
- 1
- 8
-
1@anyGenericProgrammer: are you seriously going to quibble over someone's spelling? – President James K. Polk Feb 11 '20 at 03:43
-
1
-
By server you mean the computer with public IP? In this case, the answer is no. You don't need reverse ssh to access a computer which has public IP, right? You need a way to reach the one which does not. So you start the connection in the computer which does not have a public IP (opening the way), then once the connection is established to the other computer ("server"), you can open another connection in reverse way (the public IP computer controlling the NAT computer). See this tutorial for a better explanation: https://www.howtogeek.com/428413/what-is-reverse-ssh-tunneling-and-how-to-use-it/ – Sir Beethoven Feb 14 '20 at 01:46
-
@Sir Beethoven, Is there a way to do this with regular non-encrypted sockets?(and yes the server is open to the internet) – anyGenericProgrammer Feb 18 '20 at 22:45
-
I am not sure on how you should implement it, I am rather pointing out a known "architecture" to accomplish the same goal. I guess you should do more research on network connection with Java. For insights, take a look at [this other thread](https://stackoverflow.com/questions/27129268/udp-hole-punching-java-example) – Sir Beethoven Feb 19 '20 at 00:42
-
As far as I can tell I don't think I can use UDP hole punching because I don't have a third-party server(Is there a way to set that up?). As for that I think I will have to learn SSH sockets. – anyGenericProgrammer Feb 20 '20 at 12:17