import java.io.*;
import java.net.*;
class kefu
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("127.0.0.1",10003);
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 x=0; x<3; x++)
{
String line = bufr.readLine();
if (line==null)
break;
out.println(line);
String info = bufIn.readLine();
System.out.println("info:"+info);
if(info.contains("欢迎"));
break;
}
bufr.close();
s.close();
}
}
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s=s;
}
public void run()
{
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"已连接");
try
{
for (int x=0;x<3;x++)
{
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=bufIn.readLine();
BufferedReader bufr= new BufferedReader(new FileReader("user.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.println(name+",欢迎光临");
break;
}
else
{
System.out.println(name+" 尝试登陆");
out.println(name+",用户名不存在");
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException("校验失败");
}
}
}
class fuwu
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10003);
while(true)
{
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
运行结果 如下
D:\java0217\day23>javac denglu.java
D:\java0217\day23>java fuwu
127.0.0.1已连接
haha 尝试登陆
null 尝试登陆//这里的空是因为回车吗,怎么解决啊 ???高手指点啊
null 尝试登陆
D:\java0217\day23>java kefu
haha
info:haha,用户名不存在
D:\java0217\day23>
这里怎么就停止了啊 ???高手指点
|