我试图在不使用任何服务器套接字的情况下在两个套接字之间建立通信。我知道服务器 Socket 有等待传入连接的 ServerSocket.accept() 方法。但是当我尝试打开一个没有服务器套接字的 ip 的套接字时,我不断收到异常:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at client1.<init>(client1.java:13)
at client1Main.main(client1Main.java:9)
这是我执行的第一个代码:
public class client1 {
public client1() {
try {
Socket socket = new Socket("127.0.0.1",15123);
BufferedReader in
= new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out
= new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()));
while(true) {
System.out.println(in.readLine());
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class client1Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new client1();
}
}
有什么我想念的吗?