Java network problem: java.net.ConnectException: connection rejected: connect

I can’t deal with any network in Java, when I run a simple network program, I can’t get a connection even when using the local host, Error:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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 EchoClient.main(EchoClient.java:8)

This program:

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

public class EchoClient{
    public static void main(String[] args) {
        try{
            Socket client = new Socket(InetAddress.getLocalHost(), 1234);
            InputStream clientIn = client.getInputStream();
            OutputStream clientOut = client.getOutputStream();
            PrintWriter pw = new PrintWriter(clientOut);
            BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
            Scanner stdIn = new Scanner(System.in);
            System.out.println("Input Message:");
            pw.println(stdIn.nextLine());
            System.out.println("Recieved Message:");
            System.out.println(br.readLine());
            pw.close();
            br.close();
            client.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

I am using Windows 7, I have bypassed the Windows firewall and I do not have an antivirus.

+3
source share
3 answers

As I wrote in my comment, check that the server (something like EchoServer?) Is up and running.


But there are other problems when you can connect. pw.println(stdIn.nextLine());may not send content to the server, you need to do pw.flush();to really send the content, or you can create your own PrintWriterusing autorun:

 pw = new PrintWriter(clientOut, true);

EchoServer, , , , :

public class EchoServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(1234);

        while (true) {
            // accept the connection 
            Socket s = ss.accept();

            try {
                Scanner in = new Scanner(s.getInputStream());
                PrintWriter out = new PrintWriter(s.getOutputStream(), true);

                // read a line from the client and echo it back
                String line;
                while ((line = in.nextLine()) != null) 
                    out.println(line);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                s.close();
            }
        }
    }
}

telnet localhost 1234.

+2

, EchoServer 1234. , .

+1

My solution on Windows 7 was to enable Simple TCP / IP Services. Described here: http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html

+1
source

Source: https://habr.com/ru/post/1792447/


All Articles