开发者

Problems trying to implement Java Sockets example

开发者 https://www.devze.com 2023-03-30 05:50 出处:网络
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

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(Sock开发者_运维问答et.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)


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.


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.


For future reference, the echo service is commonly disabled by default. I'm using windows 7, to enable it I followed the instructions found here:

http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html

Example worked fine for me afterwards.

For XP:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sag_tcpip_pro_simptcpinstall.mspx?mfr=true

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号