Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems trying to implement Java Sockets example

I am trying to implement this example here: Reading from and Writing to a Socket

I copied and pasted the code into NetBeans. I changed the port name "taranis" to "localhost" and tried to run the example, but I got the error:

run: Couldn't get I/O for the connection to: localhost. Java Result: 1 BUILD SUCCESSFUL (total time: 1 second)

I also tried to substitute localhost for my actual hostname of my laptop, but it gives the similar error. Can you help pinpoint what I am doing wrong?

Edit: In regards to Mark's recommendation, when I substitute

System.err.println("Couldn't get I/O for " + "the connection to: localhost.");

with

e.printStackTrace();

I get:

run:
java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at EchoClient.main(EchoClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
like image 817
O_O Avatar asked Dec 05 '25 02:12

O_O


2 Answers

The echo service is not listening. Why not write your own? Run the application below and change your client to connect to the same port (8000).

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 EchoServer {

    private static final int PORT = 8000;

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

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
        }
        catch (IOException e) {
            System.err.println("Could not listen on port: " + PORT);
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        }
        catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.println("Echo server started");

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println("echoing: " + inputLine);
            out.println(inputLine);
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

Btw, the next example (knock-knock server) does work and gives a nice example of using a 'protocol' class.

like image 94
Adriaan Koster Avatar answered Dec 07 '25 14:12

Adriaan Koster


I don't think the echo service is running by default, when I tried a quick test it on my Win XP client, it did not work:

H:\>telnet localhost 7
Connecting To localhost...Could not open connection to the host, on port 7: 
Connect failed

H:\>

So to make your code work, you could try pointing it to a server that has the echo service running.

like image 29
beny23 Avatar answered Dec 07 '25 14:12

beny23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!