javajakarta-eehostinghole-punchingnat-traversal

Running Java EE application on web-server


Following is a thread which has a SOCKET listening at port 15445. Whenever a Datagram packet is sent to it, it forwards back to the sender's address after adding a String(Reply from SERVER) in it. I want this code to run somewhere on the Internet, but I don't know where to start.

Is it possible? Can I run this code on a Tomcat server or do I need to do something different?

import java.io.*;
import java.net.*;

public class HelloWorld extends Thread {

    protected DatagramSocket socket = null;
    protected BufferedReader in = null;

    public HelloWorld() throws IOException {
        socket = new DatagramSocket(15445);
    }

    public void run() {

        while (true) {
            try {
                byte[] buf = new byte[256];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);

                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                String s = "Reply from SERVER";
                byte[] b= s.getBytes();
                packet = new DatagramPacket(b, b.length, address, port);
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();    
            }
        }

    }

    public static void main(String[] args) throws IOException {
        new HelloWorld().start();
    }
}

Solution

  • No, Tomcat isn't a web server. It's a servlet/JSP engine that happens to have an HTTP server built in.

    This looks like POJO with a main. Why can't you just run that as a service? Why do you think you need a web server?