求大神指点!!
- package com.cg.tcp;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- class LoginClient {
- public static void main(String[] args) throws Exception {
- Socket s=new Socket(InetAddress.getLocalHost(),11000);
- BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
- PrintWriter out=new PrintWriter(s.getOutputStream(),true);
- BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
- for(int j=0;j<3;j++){
- String str=bufr.readLine();
- if(str==null)
- break;
- out.println(str);
- String line=bufIn.readLine();
- System.out.println(line);
- if(line.contains("欢迎登陆!"))
- break;
- }
- bufr.close();
- s.close();
-
- }
- }
- class UserInfo implements Runnable{
- private Socket s;
- UserInfo(Socket s){
- this.s=s;
- }
- public void run(){
- String ip=s.getInetAddress().getHostAddress();
- System.out.println(ip+"-----Connected!");
- try {
- for(int i=0;i<3;i++){
- BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
- String name=bufIn.readLine();
- //if(name==null)
- // break;
- BufferedReader bufr=new BufferedReader(new FileReader("G:\\MyEclipse\\WorkPlace2\\Info.txt"));
- PrintWriter Out=new PrintWriter(s.getOutputStream(),true);
- String line=null;
- boolean flag=false;
- while((line=bufr.readLine())!=null){
- if(line.equals(name))
- {
- flag=true;
- break;
- }
- }
- if(flag){
- System.out.println(name+","+"已经成功登陆!");
- Out.write("欢迎登陆!");
- break;
- }
- else{
- System.out.println(name+","+"尝试登陆!");
- Out.write("该用户不存在,请重新输入!");
- break;
- }
- }
- s.close();
- } catch (IOException e) {
- throw new RuntimeException("校验失败!");
- }
- }
- }
- class LoginServer{
- public static void main(String[] args) throws Exception {
- ServerSocket ss=new ServerSocket(11000);
- while(true)
- {
- Socket s=ss.accept();
- new Thread(new UserInfo(s)).start();
- }
- }
- }
复制代码
|
|