本帖最后由 kfcka 于 2015-2-4 10:58 编辑
import java.io.*;
import java.net.*;
class LoginClient
{
public static void main(String[] args) throws IOException
{
//建立Socket服务,并指定主机和端口号
Socket s = new Socket("127.0.0.1",8801);
for(int i=0;i<3;i++)
{
//建立键盘录入输入流
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//得到Socket服务的输出流
PrintWriter bufw = new PrintWriter(s.getOutputStream());
String line=bufr.readLine()+"\r\n";
bufw.println(line);
}
}
}
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
//得到连进来的IP
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....Connected");
//建立字符读取流
BufferedReader bufr = new BufferedReader(new FileReader("G:\\UserInfo.txt"));
//得到Socket服务输入流
BufferedReader bufrIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
//得到Socket服务输出流
PrintWriter pw = new PrintWriter(s.getOutputStream());
String line = null;
boolean flag=false; String name=bufrIn.readLine();////?????怎么这里总是读不到数据?
while((line=bufr.readLine())!=null)
{
System.out.println(line);
if(line.equals(name)
{
flag=true;
}
}
if(flag)
{
System.out.println("登陆成功");
pw.println("欢迎光临");
}
else
{
System.out.println(ip+"尝试登陆");
pw.println("用户不存在!请重新输入");
}
}
catch(Exception e)
{
throw new RuntimeException("操作失败");
}
}
}
class LoginServer
{
public static void main(String[] args) throws IOException
{
//建立ServerSocket服务,指定端口号
ServerSocket ss = new ServerSocket(8801);
while(true)
{
//得到客户端Socket服务
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
怎么if(line.equals(bufrIn.readLine()))////????么这里总是读不到数据?PrintWriter构造函数时,没有写true不好意思,怎么帖子不能删除
|
|