- import java.io.*;
- import java.net.*;
- class Loadclient
- {
- public static void main(String[] args) throws Exception
- {
- Socket s=new Socket("192.168.191.1",10007);
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- PrintWriter out=new PrintWriter(s.getOutputStream(),true);
- BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
- for (int x=0;x<3 ;x++ )
- {
- String line=br.readLine();
- if (line==null)
- {
- break;
- }
- out.println(line);
- String info=in.readLine();
- System.out.println(info);
- if (info.contains("欢迎"))
- {
- break;
- }
- }
- br.close();
- s.close();
- }
- }
- class Loadserver
- {
- public static void main(String[] args)throws Exception
- {
- ServerSocket ss=new ServerSocket(10007);
- while (true)
- {
- Socket s=ss.accept();
- new Thread(new Loaduser(s)).start();
- }
- }
- }
- class Loaduser implements Runnable
- {
- private Socket s;
- Loaduser(Socket s)
- {
- this.s=s;
- }
- public void run()
- {
- try
- {
- for (int x=0;x<3 ;x++ )
- {
- BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
- String name=in.readLine();
- BufferedReader br=new BufferedReader(new FileReader("Load.txt"));
- PrintWriter out=new PrintWriter(s.getOutputStream(),true);
- boolean flag=false;
- String line=null;
- while ((line=br.readLine())!=null)
- {
- if (line.equals(name))
- {
- flag=true;
- break;
- }
- }
- if (flag)
- {
- System.out.println(name+"已登录");
- out.println(name+"欢迎光临");
- break;
- }
- else
- {
- System.out.println(name+"尝试登陆");
- out.println(name+"该用户不存在");
- break;
- }
-
- }
- s.close();
-
- }
- catch (Exception e)
- {
- throw new RuntimeException("查询失败");
- }
- }
- }
复制代码
第一次输入错误的名字时候会报不存在第二次再输就直接报异常跳出来了,输对名字的话可以登录异常信息如下求告诉怎么回事
D:\javase dk\edit cunfang\day24>java Loadclient
lisi
lisi欢迎光临
D:\javase dk\edit cunfang\day24>java Loadclient
ddd
ddd该用户不存在
dffd
Exception in thread "main" java.net.SocketException: Software caused connection
abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Loadclient.main(LoadDemo.java:19)
|
|