本帖最后由 于启会 于 2012-9-16 08:46 编辑
- <p>package com.users;</p><p>import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;</p><p>public class UsersLogin {
- public static void main(String[] args) {
- try {
- Socket s=new Socket("127.0.0.1",9527);
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
- BufferedReader reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
- for(int i=0;i<3;i++){
- String line=br.readLine();
- if(line==null){
- break;
- }
- pw.println(line);
-
- String read=reader.readLine();
- if(read.contains("欢迎")){
- System.out.println(read);
- break;
- }else{
- System.out.println(read);
- }
- }
- br.close();
- s.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- package com.users;</p><p>import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;</p><p>public class UserService {
- public static void main(String[] args) {
- try {
- ServerSocket ss=new ServerSocket(9527);
- while(true){
- Socket s= ss.accept();
- new Thread(new User(s)).start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- package com.users;</p><p>import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;</p><p>public class User implements Runnable{
- private Socket s;
- public User(Socket s){
- this.s=s;
- }
- public void run(){
- String ip=s.getInetAddress().getHostAddress();
- try {
- for(int i=0;i<3;i++){
- BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
- BufferedReader read=new BufferedReader(new FileReader(new File("E:\\学习记录<a href="file://\\User.txt">\\User.txt</a>")));
- boolean bl=false;
- String r_read=null;
- while((r_read=read.readLine())!=null){
- if(br.readLine().equals(r_read)){
- bl=true;
- break;
- }
- }
- if(bl){
- System.out.println(ip+":"+r_read+":登陆成功");
- pw.println(r_read+"欢迎登陆");
- break;
- }else{
- System.out.println(ip+":"+r_read+":尝试登陆");
- pw.println(r_read+"登陆失败");
- }
- read.close();
- }
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }
- </p>
复制代码 运行在输入的时候回车了 还是继续输入。没找到什么原因,求解 |