//服务端
public class Server {
public static void main(String[] args) throws Exception {
// 创建服务端并制定端口
ServerSocket server = new ServerSocket(9999);
System.out.println("服务端准备就绪");
// 接受连接该服务端的客户端对象
Socket client = server.accept();
// 显示连接过来的客户端信息
System.out.println("连接的客户端IP:" + client.getInetAddress());
// 获取到客户端的输出流对象给客户端输出数据
boolean accept = true;
while (accept) {
String data = "密码?";
PrintStream out = new PrintStream(client.getOutputStream());
out.print(data);
out.close();
}
server.close();
}
}
//客户端
public class Client {
public static void main(String[] args) throws Exception, IOException {
// 创建客户端,并制定接收的服务端IP地址和端口
Socket client = new Socket("localhost",9999 );
// 获取客户端的输入流对象
Scanner sc = new Scanner(client.getInputStream());
while (sc.hasNextLine()) {
String str = sc.nextLine();
System.out.println(str);
}
sc.close();
//获取服务端的输入对象流给服务端输入数据
//PrintStream out = new PrintStream(server.out)
client.close();
}
}
共有 0 条评论