黑马程序员技术交流社区
标题:
我的TCP-客户端并发登陆的程序怎么运行不了???
[打印本页]
作者:
汪伟楠
时间:
2013-12-30 17:49
标题:
我的TCP-客户端并发登陆的程序怎么运行不了???
本帖最后由 汪伟楠 于 2014-1-1 11:45 编辑
import java.net.*;
import java.io.*;
class SocketLogin
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("127.0.0.1",10009);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream());
BufferedReader bufrin=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=bufrin.readLine();
System.out.println("info:"+info);
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
}
class ServerSocketLogin
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10009);
while(true)
{
Socket s=ss.accept();
new Thread(new LoginThread(s)).start();
}
}
}
class LoginThread implements Runnable
{
private Socket s;
LoginThread(Socket s)
{
this.s=s;
}
public void run()
{
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"......connected");
try
{
for (int x=0;x<3 ;x++ )
{
BufferedReader bufrin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=bufrin.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 (false)
{
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(ip+"校验失败");
}
}
}
大家看看到底哪里有问题啊!!!
作者:
LuckyQS
时间:
2013-12-31 14:12
import java.net.*;
import java.io.*;
class LoginClient
{
public static void main(String[] args)throws Exception
{
Socket s = new Socket("127.0.0.1",10123);
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();
try
{
System.out.println(ip+"..connected");
for(int x=0; x<3; x++)
{
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String name = bufIn.readLine();
if(name==null)
break;
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+"用户名不存在");
}
}
}
catch (Exception e)
{
throw new RuntimeException(ip+"校验失败");
}
}
}
class LoginServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(10123);
while(true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
这是我写的代码,你的代码 out.println(name+",用户名不存在");下面不应该有break;
PrintWriter out=new PrintWriter(s.getOutputStream());应该是
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2