简体   繁体   中英

Connection refused from client to server

I am learning about Sockets in Java and I am only missing how to receive back response from the server.

Following How to send string array object and How to send and receive serialized object in socket channel , I am trying to ask the user to pick as much as he wants out of choices:

1 - 2 - 3 - 4

And then I want to split and send them as an array to a server, where the server should send back the number of choices the user has picked.

For example if the user chose

2 3 4 1 2 3 4 1

Server should return

8

server is sending response fine, but on the client side I get error:

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.(Unknown Source) at java.net.Socket.(Unknown Source) at ClientClass.main(ClientClass.java:26)

I am not sure why the issue. Any help?

My Client:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class ClientClass {

    public static void main(String args[]) throws IOException, ClassNotFoundException 
    {
        // take order and convert it to array of choices
        Scanner myScanner = new Scanner(System.in); // take from user
        System.out.println("Enter all your choices seperated by space:");
        System.out.println("Choices are: 1- 2- 3- 4");
        String orderString = myScanner.next();
        orderString += myScanner.nextLine();
        String orderArray[] = orderString.split(" ");

        // send request to server
        Socket mySocket = new Socket("127.0.0.1", 4444); // create a socket
        ObjectOutputStream out = new ObjectOutputStream(mySocket.getOutputStream());
        out.writeObject(orderArray);  

       // get response from server
       InputStream is = mySocket.getInputStream();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);
       String message = br.readLine();
       System.out.println("Message received from the server : " +message);

    }
}

My Server:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class ServerClass {

    public static void main(String args[]) throws IOException, ClassNotFoundException
    {
        // receive
        ServerSocket myServerSocket = new ServerSocket(4444); // create a server socket
        Socket mySimpleSocket = myServerSocket.accept(); // accept requests
        ObjectInputStream ois = new ObjectInputStream(mySimpleSocket.getInputStream());
        String[] choices = (String[]) ois.readObject();


       // send back response
       OutputStream os = mySimpleSocket.getOutputStream();
       OutputStreamWriter osw = new OutputStreamWriter(os);
       BufferedWriter bw = new BufferedWriter(osw);
       bw.write(choices.length);
       System.out.println("Message sent to the client is "+choices.length);
       bw.flush();

    }
}

server is sending response fine

Rubbish. The server isn't even getting an incoming connection, let alone reading the request, let alone sending a response.

but on the client side I get error:

Exception in thread "main" java.net.ConnectException: Connection refused: connect at 

On this evidence the server wasn't even running. Certainly not running in the same host as the client, which is what is required by new Socket("127.0.0.1", 4444) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM