本帖最后由 张政 于 2013-2-18 21:58 编辑
- import java.io.*;
- import java.net.*;
- class LoginClient{
- public static void main(String[] args)throws IOException{
- Socket sk = new Socket("192.168.1.101",10008);
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- BufferedReader brin = new BufferedReader(new InputStreamReader(sk.getInputStream()));
- PrintWriter out = new PrintWriter(sk.getOutputStream(),true);
-
- String line = null;
- for(int i = 0; i<3; i++){
- line = br.readLine();
- System.out.println(line);
- out.write(line);
-
- //关闭Socket服务输出流
- sk.shutdownOutput();
-
- line = brin.readLine();
- System.out.println(line);
- if(line.contains("欢迎"))
- break;
- }
- }
- }
- class LoginServer{
- public static void main(String[] args) throws IOException {
- //创建服务端socket服务,并绑定端口
- ServerSocket ss = new ServerSocket(10008);
- Socket sk = ss.accept();
- new Thread(new Login(sk)).start();
-
- }
- }
- class Login implements Runnable{
- private Socket sk;
- Login(Socket sk){
- this.sk = sk;
- }
- public void run(){
- String ip = sk.getInetAddress().getHostAddress();
- System.out.println(ip + ".......connected");
-
- try{
- BufferedReader brin = new BufferedReader(new InputStreamReader(sk.getInputStream()));
- PrintWriter out = new PrintWriter(sk.getOutputStream());
- BufferedReader br = new BufferedReader(new FileReader("user.txt"));
-
- String name = null;
- boolean flag = false;
- for(int i = 0; i<3; i++){
- name = brin.readLine();
- if(name == null){
- System.out.println("名字为空");
- break;
- }
- while(true){
- if(name.equals(br.readLine())){
- flag = true;
- break;
- }
- }
- if(flag){
- System.out.println(name + "登陆");
- out.println(name + "欢迎登陆");
- break;
- }else{
- System.out.println(name + "尝试登陆");
- }
- }
-
- }catch(IOException e){
- throw new RuntimeException("校正失败");
- }
- }
-
- }
复制代码 |
|