Search code examples
javaphpdatabasehttptunnel

How do I create a server to client connection on a non-http protocol using Java?


I have two java applications (a client and a server). The server application and the database are on the same server. However, the client application could be anywhere. My problem is that the server blocks all connections (except http), so I cannot access my database with my client application, is it possible to use something like a tunnel via HTTP(or ftp) to make my client application able to ask my database?


Solution

  • Take a look at Sockets. It's low level compared to modern frameworks, but you can connect to a specific port and send data packets easily from client to server.

    You will need to create a Server Socket and listen to connections on port 80 (HTTP).

    The client will have it's own Client Socket and connect to the server socket at that port, and begin sending data, which can be from raw bytes to whole serialized objects.

    You then handle these packages from the Server, and, if needed, send a response to the client.

    All About Sockets

    PS: You may also want to take a look at Java Threading for handling multiple clients. Please note, that without Thread support, you'd only be able to have one connected client at a time