Java Server Error Examples

Getting these errors in some java code

http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html

Exception in thread "Thread-0" java.lang.RuntimeException: Cannot open port 8080
at servers.MultiThreadedServer.openServerSocket(MultiThreadedServer.java:61)
at servers.MultiThreadedServer.run(MultiThreadedServer.java:22)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:353)
at java.net.ServerSocket.bind(ServerSocket.java:336)
at java.net.ServerSocket.<init>(ServerSocket.java:202)
at java.net.ServerSocket.<init>(ServerSocket.java:114)
at servers.MultiThreadedServer.openServerSocket(MultiThreadedServer.java:59)
... 2 more
     Exception in thread "main" Stopping Server
  java.lang.NullPointerException
at servers.MultiThreadedServer.stop(MultiThreadedServer.java:51)
at servers.Dispatch.main(Dispatch.java:18)

      

I made these files

WorkerRunnable.java

package servers;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;

/**

*/
public class WorkerRunnable implements Runnable{

protected Socket clientSocket = null;
protected String serverText   = null;

public WorkerRunnable(Socket clientSocket, String serverText) {
    this.clientSocket = clientSocket;
    this.serverText   = serverText;
}

public void run() {
    try {
        InputStream input  = clientSocket.getInputStream();
        OutputStream output = clientSocket.getOutputStream();
        long time = System.currentTimeMillis();
        output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " +
                this.serverText + " - " +
                time +
                "").getBytes());
        output.close();
        input.close();
        System.out.println("Request processed: " + time);
    } catch (IOException e) {
        //report exception somewhere.
        e.printStackTrace();
    }
  }
 }

      

MultiThreadedServer.java

package servers;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;

public class MultiThreadedServer implements Runnable{

protected int          serverPort   = 8080;
protected ServerSocket serverSocket = null;
protected boolean      isStopped    = false;
protected Thread       runningThread= null;

public MultiThreadedServer(int port){
    this.serverPort = port;
}

public void run(){
    synchronized(this){
        this.runningThread = Thread.currentThread();
    }
    openServerSocket();
    while(! isStopped()){
        Socket clientSocket = null;
        try {
            clientSocket = this.serverSocket.accept();
        } catch (IOException e) {
            if(isStopped()) {
                System.out.println("Server Stopped.") ;
                return;
            }
            throw new RuntimeException(
                "Error accepting client connection", e);
        }
        new Thread(
            new WorkerRunnable(
                clientSocket, "Multithreaded Server")
        ).start();
    }
    System.out.println("Server Stopped.") ;
}


private synchronized boolean isStopped() {
    return this.isStopped;
}

public synchronized void stop(){
    this.isStopped = true;
    try {
        this.serverSocket.close();
    } catch (IOException e) {
        throw new RuntimeException("Error closing server", e);
    }
}

private void openServerSocket() {
    try {
        this.serverSocket = new ServerSocket(this.serverPort);
    } catch (IOException e) {
        throw new RuntimeException("Cannot open port 8080", e);
    }
}

}

      

Dispatch.java

 package servers;

 public class Dispatch {

/**
 * @param args
 */
public static void main(String[] args) {
    MultiThreadedServer server = new MultiThreadedServer(9000);
    new Thread(server).start();

    try {
        Thread.sleep(20 * 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Stopping Server");
    server.stop();

}

}

      

+3


source to share


3 answers


Your machine port is being 8080

used by another program

End program using port 8080

You can find out the program with the following command (Windows):

netstat -o -n -a | findstr 0.0:8080

      

Output example:

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 8704

      

Then you can find the program by PID 8704

using Task Manager and terminate it.

See here for details .



Use a different port

You can change

protected int serverPort   = 8080;

      

to

protected int serverPort   = 8888;

      

or

MultiThreadedServer server = new MultiThreadedServer(8888);

      

So the 8080

port is used instead 8888

.

+2


source


It is possible that the port is 8080

already in use by another service on your machine (usually used by web servers like Tomcat ). Try starting the server on a different port number.



+3


source


Your code is error prone and misleading. You specify the server port as 9000

and print it as 8080

in the exception block and initialize it 8080

in the class declaration. The real port that is in use is the port 9000

, not 8080

. Try another port - try 8080

.

+2


source







All Articles