0

I am getting this error when I'm running my client program. I was unable to recognise the problem yet.I've changed the port numbers but there is no use. I saw the previous posts regarding the same error but I didn't figured it out.

Server.java

import java.io.*;
import java.net.*;
class Server{

 public static void main(String argv[]) throws Exception 
 {

   String clientSentence;
   String capitalizedSentence;
   ServerSocket welcomeSocket = new ServerSocket(8080);

   while (true) 
   {
      Socket connectionSocket = welcomeSocket.accept();
       BufferedReader inFromClient = new BufferedReader(new 
      InputStreamReader(connectionSocket.getInputStream()));
       DataOutputStream outToClient = new 
       DataOutputStream(connectionSocket.getOutputStream());
       clientSentence = inFromClient.readLine();
        System.out.println("Received: " + clientSentence);
        capitalizedSentence = clientSentence.toUpperCase() + '\n';
         outToClient.writeBytes(capitalizedSentence);
     }
  }
  }

Client.java

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

class Client
{
 public static void main(String argv[]) throws Exception 
 {
      String sentence;
      String modifiedSentence;
      BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
      Socket clientSocket = new Socket("localhost", 8080);    
      DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      sentence = inFromUser.readLine();
      outToServer.writeBytes(sentence + '\n');
      modifiedSentence = inFromServer.readLine();
      System.out.println("FROM SERVER: " + modifiedSentence);
      clientSocket.close();
 }
 }

I am getting an error when I run the client.java

Exception in thread "main" 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.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:11)

Can anyone help with this error?

8
  • When it's a question you use a question mark not three exclamation marks Commented Apr 11, 2018 at 11:52
  • You open both of your sockets on the same port. Commented Apr 11, 2018 at 11:52
  • Yes, I used the same port. Commented Apr 11, 2018 at 11:55
  • Use other port than 8080. Commented Apr 11, 2018 at 12:00
  • I guess the server is started before the client, right? Commented Apr 11, 2018 at 12:02

1 Answer 1

1

The Client and Server Program runs perfectly fine for me.

  1. Can you check if there is some process already listening on that port.
Sign up to request clarification or add additional context in comments.

8 Comments

Was about to say the same. Copy&Past --> Works just fine. Under Windows you can use the "netstat" cmd line tool to check if the port is used. With "-b" it will even tell you which application (requires admin privileges)
No, I've checked
I am getting "The requested operation requires elevation." when I am using <netstat -b> @Rainer
Can you check again if your server program is indeed running. I guess server program is not even starting in your case. When i terminated my server program and then ran the client, I got the same stack as yours.
When I run my server program there is no response it is running still... @piy26
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.