只贴了主要的部分
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader stdIn = null;
try {
echoSocket = new Socket("localhost", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
}
}
结果:
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 test.test.main(test.java:60)
------解决方案--------------------
localhost -> 127.0.0.1 或者换成你的局域网ip
------解决方案--------------------
你确定7端口是你socket服务器开的端口么?你看看你的socket服务器没开起来,而是报了端口被占用的异常了?
------解决方案--------------------
你确定echo服务开启了么。。
------解决方案--------------------
你是不是误会了什么。。。
------解决方案--------------------
当然不是。。。怎么开启echo服务google一下吧
------解决方案--------------------
错误是必然的 !!!
-------------------------
你只开了Echo
判断理由:
java.net.ConnectException: Connection refused: connect
--------------------------
你自己回忆下,是不是一运行,等10秒就报错啊哥们儿 ...