I have a simple server Java app that listens on a port, processes commands and replies.
I would like to make it behave like an ssh server, so that clients connect as if by ssh and all comms are forwarded to my app. This sounds like it could easily be done via ssh port forwarding, but my attempts have failed.
The client of your app would do:
> ssh -L1234:localhost:1234 your.host.com
Where 1234 is the port you are listening on. Then clients' connecting to localhost:1234 will be forwarded to your app via SSH
Related
I'm currently coding a client-server java application. I use the java socket api for sending data from the client to the server and vice-versa.
The server is running on windows on port 9001.
The client is in a docker container on the same machine.
What port do I have to bind in docker so that the client in the docker container can connect to the server?
I've tried binding the server port, which obviously didn't work because it's already taken by the server.
The client needs to connect to the host on port 9001.
The common way to do that is to add the host-gateway as an extra host by adding --add-host=host.docker.internal:host-gateway as a parameter to your docker run command.
Then the client can connect to your server program using host.docker.internal as the hostname (and 9001 as the port name).
Connect to the server's IP:9001. What won't work is if you try to connect to localhost, as from inside the docker container it would look like connecting to the same container.
Of course your server process needs to listen to IP:9001, and listening to localhost:9001 is not sufficient.
Regarding binding: The server opens a port for listening, and that is called binding. The client opens a port for sending (but this sending port address is taken from the ephemeral port range and thus irrelevant) and uses that port to connect to the server port.
See also:
https://docs.docker.com/network/
https://docs.docker.com/network/host/
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
I have a Spring Application with several REST endpoint running on Heroku on port 8888. It works correctly and the base url is something like xxx.herokuapp.com.
Now I added a TCP server to my application on port 6666 by using java.net.ServerSocket. I can reach it locally with netcat localhost 6666 <SampleRequest.txt and it works properly.
How can I send a direct TCP request to the application deployed on Heroku?
I'd like to have other clients (not on my machine) to contact the TCP server on Heroku. Such clients do not implement HTTP.
You can use the Heroku Exec feature with a command like heroku ps:forward 6666
I have telnet server and telnet client and i am making requests and responses.
Now instead of telnet client i need to use SSH client. For the ssh client I am using Jsch. I am trying to connect my client to the telnet server but I have errors.
So my question is: Is it possible to connect SSH client to telnet server and to make requests and responses.
No, this is not possible as telnet and ssh are different protocols.
I have a simple java written client and server chat application(with sockets). When running on the same network/computer it works fine. However when i try to run the client from a different network it doesn't connect. I tried using the public IP address of the server to connect the client to the server but without luck. How would I be able to connect to the server app from a different network? any help would be appreciated.
It sounds like you have more of a firewall issue than a problem with the application. Instead of trying to connect with the Java client, first try connecting with Telnet to the server from the same computer, then from the other computer. The first effort will show you what to expect when it works. For instance, if your server is running on port 999, use telnet server.example.com 999.
If the machines are Linux boxes, use iptables -L to see whether there is a block on the port you are trying to access.
If you're still having problems reaching the server, run tcpdump -i tcp:999 on the server host to see what traffic is making it to your server socket, then run the telnet commands again. You should see the tcp connection established when connecting from the local machine, maybe or maybe not when connecting from other machines. If you don't see it while connecting from other machines, run tcpdump there too to make sure the client is definitely sending the traffic to the server.
After you are sure that the server can receive traffic and that your client is sending the traffic, there are no mysteries about what is actually going on and you should find your problem.
It worked after I did port forwarding on router. Most of the ISP provided modem/routers wont let you manipulate ports so had to buy my own modem/router, forwarded the port and worked like a charm. Information on what port forwarding is can be found here : http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/
I've a server (Java) and a number of clients (c++), connected by sockets.
I would like to set the ports automatically.
Assuming the IP is already known.
In the Java side I can make :
ServerSocket s = new ServerSocket(0);
So now I've a random free port on the server.
How can I know in the C++ side, what port is the server listening to?
I think is not possible, if you want establish a connection with a server, you must know in which port is the server listening, there are programs like nmap that shows you a list of opened ports in a server, but a server can have many opened ports at the same time and then, How do you know what is the port opened by your server? and in any case, is too slow and inefficient to call external tool, read and parse its output. For what reason do you need a random port service?
Other option can be get the opened socket in the server side calling to s.getLocalPort() and send it via UDP for any listening node in the network with broadcasting, and re-program the client side to listen in broadcast and when it receives a message, check if it is a port number and connect to the server using that port.
You can't, not reliably. In IP, a machine is identified by an address. A server (ie, a service) is identified by an address and a port. You clients need some form of "known service" that they can connect to.
If you, for whatever reason, absolutely want to have dynamic listening port, you could combine it with a "locator" service on a known port. For instance, have a web service/servlet on the standard http port (80). Your clients connect to the "locator" service (always on port 80) and asks which port your application is currently listening on. This is a not entirely uncommon pattern. RMI works is a similar way where you have a registry on a known port. Clients connect to the registry and asks for the location of RMI endpoints.