本帖最后由 郑世光 于 2012-10-12 09:01 编辑
package day1011;
import java.io.*;
import java.net.*;
public class LoginServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(10009);
while (true)
{
Socket s = ss.accept();
new Thread(new ClientLogin(s)).start();
}
}
}
class ClientLogin implements Runnable
{
private Socket s;
public ClientLogin(Socket s)
{
super();
this.s = s;
}
@Override
public void run()
{
try
{
BufferedReader readfile=
new BufferedReader(new FileReader("g://client.txt"));
BufferedReader bufread=
new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pwrite=new PrintWriter(s.getOutputStream(),true);
for(int a=0;a<3;a++)
{
String name=bufread.readLine();//获取客户端的name
if(name==null)
break;
boolean flag=false;
String line=null;
while((line=readfile.readLine())!=null)
{
if(line.equals(name)) //如果改为if(name.equals(line))
//服务器端下方的语句就像客户端发送的是null 为什么就不对?不都是比较2个字符是否相等吗?
{
flag=true;
break;
}
}
if(flag)
{
System.out.println(name+"已登录");
pwrite.println(name+"欢迎登录");//将上面的line和name调换位置后 发送客户端的就是null了!
break;
}
else
{
pwrite.println(name+"不存在");
}
}
s.close();
readfile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} |