客户端
Socket s=new Socket("本机IP",55568);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader bufi=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x=0;x<10;x++)
{
String line=bufr.readLine();
out.println(line);
String info=bufi.readLine();
if(info.contains("huangying"))
break;
System.out.println(info);
}
bufr.close();
s.close();
}
服务端
ServerSocket sert=new ServerSocket(55568);
while(true)
{
Socket s=sert.accept();
new Thread(new Usert(s)).start();
}
}
class Usert implements Runnable
{
private Socket s;
Usert(Socket s)
{
this.s=s;
}
public void run()
{
String ip=s.getInetAddress().getHostAddress();
try
{
for(int x=0;x<10;x++)
{
BufferedReader bufre=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=bufre.readLine();
if(name==null)
break;
BufferedReader bufin=new BufferedReader(new FileReader("use.txt"));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String line=null;
boolean flag=false;
while((line=bufin.readLine())!=null)
{
if(line.equals(name))
{
flag=true;
break;
}
}
if(flag)
{
System.out.println(name+"yidonglu");
out.println("huanying"+name);
break;
}
else
{
System.out.println("no access"+name);
out.println("bucunzai");
}
}
s.close();
}
catch(Exception e)
{
throw new RuntimeException("shibai "+ip);
}
}
}
use.txt的内容
zhangsan
lisi
wangwu
maxliu
kkk
问题是 为什么我输入lisi就是bucunzai呢
kkk
huanyingkkk
lisi
Exception in thread "main" java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
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 helloword.Test15.main(Test15.java:106)
java 106 指的是 客户端的 :String info=bufi.readLine();
|
|