Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:17:27+00:00 2026-06-01T04:17:27+00:00

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

  • 0

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();

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:17:28+00:00Added an answer on June 1, 2026 at 4:17 am

    Your machine’s port 8080 being used by other program

    Terminate program that use port 8080

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

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

    Example output:

    TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 8704
    

    Then you can find the program using PID 8704 using Task Manager and terminate it.

    Refer here in more details.

    Use other port

    You can change the

    protected int serverPort   = 8080;
    

    to

    protected int serverPort   = 8888;
    

    Or

    MultiThreadedServer server = new MultiThreadedServer(8888);
    

    So it is use port 8888 instead of 8080.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why am I getting these errors? alt text http://img39.imageshack.us/img39/2203/help.tif It says: Error: Request for
I keep getting these 2 errors in my code In function 'int main()': error:
I've been getting these errors intermittently from sending email from my django app. I
Using an opencart plugin I am getting these errors in my apache error log
I'm getting loads of these errors reported when a user tries to download a
i am getting adding some of parameter in query string.value of these param can
I am running some java code on CentOS 5 java Something.java & that creates
I am using TFS 2008 and team build to compile some Java code using
So I'm getting some odd errors such as illegal start of type and variable's
today I am basically trying to create some JAVA code that moves an actor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.