[java]代码库
//客户端
class LoginClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.4.121", 10008);
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);
System.out.println("22!!!!!!!!!");
String info = bufIn.readLine();System.out.println("33!!!!!!!!!");
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());
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+" 尝试登录");System.out.println("4!!!!!!!!!");
out.println(name+",用户名不存在!\n\t");System.out.println("5!!!!!!!!!");
}
}
s.close();
}
catch (Exception e)
{
System.out.println(e);
//throw new RuntimeException(ip+":检验失败!!");
}
}
}
class LoginServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10008);
while(true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
//源代码片段来自云代码http://yuncode.net