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 8678551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:45:00+00:00 2026-06-12T20:45:00+00:00

I am writing a client/server application that uses a threadpool, so that several clients

  • 0

I am writing a client/server application that uses a threadpool, so that several clients can communicate with the server at the same time and send commands to it:

But it does not work, even with an extremely simple client:

here is my client:

    if(args.length==0) {
        host="127.0.0.1";
        port=11111;
    } else {
        host=args[0];
        String portString = args[1];
        try {
            port = Integer.parseInt(portString);
        } catch(Exception e) {
            e.printStackTrace();
            port = 11111;
        }
    }

    try {
        System.out.println("Client connects to host=" + host + " port=" + port);
        Socket skt = new Socket(host, port);//line 30 in the exception
        BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        PrintWriter myOutput = new PrintWriter(skt.getOutputStream(), true);

        myOutput.println("Hello I am the Client!");
        String buf=myInput.readLine();
        System.out.println(buf + "=buf");


        if(buf!=null) {
            System.out.println("Client receives " + buf + " from the Server!");
            buf=myInput.readLine();
            System.out.println(buf);

        }
        myOutput.close();
        myInput.close();
        skt.close();

but I get:

Client connects to host=127.0.0.1 port=11111
java.net.ConnectException: Connection refused: connect at
java.net.DualStackPlainSocketImpl.connect0(Native Method) at
java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at
java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at
java.net.AbstractPlainSocketImpl.connect(Unknown Source) at
java.net.PlainSocketImpl.connect(Unknown Source) at
java.net.SocksSocketImpl.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.(Unknown Source) at
java.net.Socket.(Unknown Source) at
client.Client.main(Client.java:30)//look at the comment at the code

so I thought my server is probably the mistake, because I cannot see it in the client:

ServerMain.java

public static void main(String[] args) {

    try {
        clientlistener = new ClientListener(server);
        //serversocket = new ServerSocket();
    } catch (IOException e) {
        System.out.println("Could not listen on tcpPort: " + tcpPort);
        e.printStackTrace();
        System.exit(-1);
    } 
}

here i open the clientlistener with a new

private static Server server = new Server();

and want to add it to the serverSocket and than to the threadPool:

public ClientListener(Server server) throws ServerException{
    this.server=server;

     try {
            serverSocket = new ServerSocket(server.getTcpPort());
        } catch (IOException e) {
            throw new ServerException(e.getMessage());
        }

    System.out.println(String.format("Accepting new clients " +
            "on %s", serverSocket.getLocalSocketAddress()));
}

/**
 * Spawns new threads on incoming connection requests from clients.
 * */
@Override
public void run() {
    while(true){
        try {
            ClientCommunicator cc = new ClientCommunicator(serverSocket.accept(), pool);
            threadPool.execute(cc);
            clientComms.add(cc);
        } catch (SocketException e) {
            System.out.println(e.getMessage());
            return;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return;
        }
    }
}

The ClientCommunicator is just a class to read from the command line.

My question is:

Whats wron with my socket connection?

I am opening one and accepting it from the client?

I really much appreaciate your answer!!!

  • 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-12T20:45:01+00:00Added an answer on June 12, 2026 at 8:45 pm

    I think your server is down and that’s why you receive a connection refused. I don’t see your server.accept() call that makes server socket wait for new clients to come and returns a Socket object not SocketServer.

    I have done this code for you. I hope it helps.

    package com.sockets.exam1;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class ServerCode {
        public static void main(String[] args) {
            try {
                ServerSocket server = new ServerSocket(11111);
                while (true) {                                              
                    new ThreadSocket(server.accept());
                }
            } catch (Exception e) {
            }
        }
    }
    class ThreadSocket extends Thread{
        private Socket insocket;    
        ThreadSocket(Socket insocket){
            this.insocket = insocket;
            this.start();
        }       
        @Override
        public void run() {     
            try {
                System.out.println("procesing request");
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        insocket.getInputStream()));
                PrintWriter out = new PrintWriter(insocket.getOutputStream(),
                        true);
                String instring = in.readLine();
                out.println("The server got this: " + instring.toUpperCase());
                insocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a client application that sends images to a server via a webservice. As
I'm writing a client application to communicate with a server program via UDP. The
Background: We have a client/server application that uses a persistent connection to the server.
I am writing a netty client and server application that will write JVM GC
I'm writing a server application that's communication with a local sql server. Each client
I am writing a TCP server application using Winsock. The client that connects to
I'm writing a simple client/server application and I found that using DataInputStream to read
I'm writing a client/server application that requires the server needs to be able to
I've written a simple Application that utilizes WCF to communicate between client and server.
I'm writing a small client/server application in c++ with winsock and I can't explain

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.