cantiaozi
cantiaozi
2017-03-30 05:14

模拟客户端和服务端通信的的java小程序,但运行时显示ConnectException,求教

  • java
  • socket

服务端程序:
import java.net.*;
import java.io.*;
public class TestServer {
public static void main( String []args) {
try {
ServerSocket ss = new ServerSocket(7777);
while (true){

        Socket s = ss.accept();
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeUTF("hello " + s.getInetAddress() + s.getPort() + 

"byebye");
dos.flush();
dos.close();
s.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

客户端程序:
import java.net.*;
import java.io.*;
public class TestClient {
public static void main(String []args) {
try {
Socket s = new Socket("127.0.0.1",7777);
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}

两个程序运行在不同的命令行窗口上,但运行客户端程序的窗口显示java.net.ConnectException.这是为什么?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答