-1

I am complete beginner and i have assignment to send something to server and get it back while using threads too. My problem is that no matter which port I use, I get connection refused. I think it might something to do with threads, but I am unsure what to do with them as it seems both start properly and client is the one throwing exception. I read somewhere that server should have some time to start connection so I put sleep but same thing again.

Main:

package advancedjavaassignment1;
public class MAIN {
public static void main(String[] args) {
    SERVER.mainServer();
    CLIENT.mainClient();
}
}

SERVER:

package advancedjavaassignment1;
import java.net.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class SERVER {
    static void mainServer()    {
        serverTHREAD serverThread = new serverTHREAD();
        serverThread.start();
        try (
            ServerSocket calcServer = new ServerSocket(10001); //Server created on port 2390
            Socket inSocket = calcServer.accept(); //Server is listening
            DataInputStream FromClient = new DataInputStream(inSocket.getInputStream());
            DataOutputStream ToClient = new DataOutputStream(inSocket.getOutputStream());)    {
            int a = FromClient.readInt();
            ToClient.writeInt(a);
        }
        catch (IOException e) {
            System.out.println(e.getMessage()+ "Server");
            
        } 
        }
    }
}

CLIENT:

package advancedjavaassignment1;
import java.net.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class CLIENT {
    static void mainClient()   {
        serverTHREAD clientThread = new serverTHREAD();
        clientThread.start();
        try {Thread.sleep(2000); System.out.println("break");} catch (InterruptedException ex) { }                
        try (Socket ClientSocket = new Socket("localhost",80);
            DataInputStream FromServer = new DataInputStream(ClientSocket.getInputStream());
            DataOutputStream ToServer = new DataOutputStream(ClientSocket.getOutputStream());) {
            ToServer.writeInt(10);
            int sum = FromServer.readInt();
            System.out.println(sum);
            ClientSocket.close();
        }
        catch(IOException exception)
        {
            System.out.println(exception.getMessage() + " - Client");
        }
    }
}
Dino
  • 11
  • 3
  • Are you creating a server that listens on port **10001** and expect the client, which connects on port **80**, to not get a connection refused? – ernest_k Sep 15 '20 at 17:27
  • Hi @ernest_k, I copied code I experimented on, but even when I put same ports, it still does not work. Any help is much appreciated.Additonally, is it even possible to forward integers this simply to server and back? – Dino Sep 16 '20 at 07:32

2 Answers2

0

Looks like your server runs on port 10001 (although you wrote 2390 in the comment) and the client tries to connect to port 80. The client finds no server listening on that port, hence the connection refused error.

GiulioP
  • 116
  • 6
0

My apology, I forgot to change it, as I was experimenting with everything, this does not work even when ports are same.

I solved this by implementing Runnable. Worked immediately.

Dino
  • 11
  • 3