//需求:用TCP协议,验证密码并给予反馈
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 ServerDemo {
public static void main(String[] args) throws IOException {
// 创建服务器Socket对象
ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
while(true){
String passworld = br.readLine();
if ("345678".equals(passworld)) {
pw.println("密码正确");
} else {
pw.println("密码有误");
}
}
s.close();//有错
ss.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
// 创建客户端Socket对象
Socket s = new Socket("192.168.3.185", 10000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String passworld = null;
for(int x = 0;x<3;x++){
System.out.println("请输入密码:");
passworld = br.readLine();
}
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(passworld);
BufferedReader brServer = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String server = brServer.readLine();
System.out.println("server:" + server);
br.close();
s.close();
}
}
|