Pregunta

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();
    }
}
¿Fue útil?

Solución

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?

Otros consejos

Use Jetty (echo server is one of the tutorials afaik) and expose your port to the internet.

If your code works fine forward the necessary port through your router so it's reachable from the internet. If your looking for a hosted solution, vservers are a cheap way to start. http://www.superuser.com is the place to go for network configuration.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top