Direct TCP connection to Heroku application - java

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

Related

Java socket client port forwarding

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

Is it possible to run springboot app on port 8081 but use port 8080 as url?

I plan to run my app on port 8081 since port 8080 is used by a separate local tomcat server but users are more concerned about not changing the URL that they are used to. that url includes port 8080 since the legacy app runs on the local tomcat server. Now, would it be possible to connect the new app to port 8081 but just on the url it would still be port 8080?
You need some kind of Proxy for this.
Every Port on your computer can be listened to by one application. Though you need an application that occupies this port and then forwards the request to some other application / port.
This can be done using for example Apache Webserver or Nginx. Or you can write a simple Spring Boot application that does the job: Run (and listen) at port 8080, and then use #Controller logic (or a Filter) to either forward the requests to port 8081 or to the Tomcat port.

CloudFoundry websocket failed: Establishing a tunnel via proxy server failed

Note: I am not using Pivotal CF.
I have a java application deployed on CloudFoundry. I am using embedded Jetty to host my Jersey REST API. This API is by default exposed on port 8080 by cloud foundry.
My application also needs some websockets to stream data to the browser. I am using Java-WebSocket (https://github.com/TooTallNate/Java-WebSocket) for this. On my local machine, I was using port 8887 for my websocket connection. Everything worked fine.
After deploying on CloudFoundry, I can access my REST API but not my websocket. After searching a bit online, I found that websocket connections are only allowed on port 4443 (http://docs.run.pivotal.io/release-notes/)
I changed my server side to reflect this
import org.java_websocket.server.WebSocketServer;
public class MyWebSocket extends WebSocketServer {
public MyWebSocket() throws UnknownHostException {
super(new InetSocketAddress(4443));
}
#Override
public void onOpen(org.java_websocket.WebSocket websocket, ClientHandshake handshake) {
// Handle this
}
}
On my client side, I am connecting the websocket using the following
wss://my_cf_app.com:4443/
But I am getting the following exception.
WebSocket connection to 'wss://my_cf_app.com:4443/' failed:
Establishing a tunnel via proxy server failed
I also tried to connect the websocket on server side using "PORT" environment variable of CF but I get "Address already in use" error in Java-WebSocket.
I have tried many different things but I am unable to figure this out. Any help would be awesome.
After deploying on CloudFoundry, I can access my REST API but not my websocket. After searching a bit online, I found that websocket connections are only allowed on port 4443 (http://docs.run.pivotal.io/release-notes/)
Port 4443 is specific to Pivotal Web Services (and some installs of CF that run on AWS). Most PCF installs do not have a separate port for WSS, but just use 443 along with the HTTPS traffic. The port used ultimately depends on the load balancer being used in front of the CF installation and what it supports.
You would never have your application listen on port 4443. Port 4443 is the external port for traffic where the load balancer listens. This traffic will be directed to the port assigned to your application, which is $PORT (env variable).
I also tried to connect the websocket on server side using "PORT" environment variable of CF but I get "Address already in use" error in Java-WebSocket.
This is the correct behavior, i.e. you should be listening on the port assigned through $PORT env variable. What the error is telling you is that something is already listening on this port and you cannot have two things listening on the same port.
There is only one port available per application at this time (likely to change in the future). For now, if you have two separate applications listening on two separate ports then you need to push them to CF as two separate applications.
What you can do to make them appear like one application to end users is to map each one to a specific path. See the --route-path argument of cf push or docs for cf create-route.
https://docs.cloudfoundry.org/devguide/deploy-apps/routes-domains.html#create-route

Incoming connection on port 80 to webserver application

I made a Java web application running on tomcat, what I'm trying to do now is letting people outside my net acceding my application.
I've read lot of guides and I understood I need to forward incoming traffic on port 80 to the raspberry hosting it (listening on port 8080).
What I did was going to the router panel and make a port forwarding with the following parameters:
Are all the parameters right?
What should i put in the remote IP address?
What I meant to do is redirecting request on port 80 to local port 8080 on which the raspberry is listening (if tomcat is listening to port 8080 if i write something like ip/webappname will I access my application?)
By the way, with the following settings, if I write my "outside" ip as url, I get prompted with a user\password form. I tried to make some tcpdump to try to understand if it's raspberry or router related, but I got tons of packets and couldn't find a way out, what could it be? Thanks.

Java wrap server ssh tunnel

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

Categories