本帖最后由 忄雾飞扬 于 2013-10-30 09:05 编辑
import java.io.*;
import java.net.*;
//为什么 会一直处于阻塞状态呢?
//昨天晚上我自己调试找到了阻塞的地方在哪了,但解决不了?
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s){
this.s = s;
}
public void run(){
try
{
for (int x = 0;x<3 ;x++ )
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "come in !!!");
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String name = bufIn.readLine();
System.out.println("名字发过来了");//就是这,为什么readLine()没有读到信息?
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
if(name == null)
break;
String line = null;
boolean flag = false;
while((line = bufr.readLine())!=null){
if(line.equals(name)){
flag = true;
break;
}
}
if(flag){
System.out.println(name+",已登录");
pw.println("欢迎"+name+"的归来");
break;
}
else
{
System.out.println(name+",尝试登录");
pw.println("鄙视"+name+"的盗号");
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException();
}
}
}
class UserClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",10011);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x = 0; x < 3; x++){
String names = bufr.readLine();
if(names == null)
break;
pw.write(names);
System.out.println("内容发送");
String str = bufIn.readLine();
System.out.println(str);
if(str.contains("欢迎"))
break;
}bufr.close();
s.close();
}
}
class UserServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10011);
while(true){
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
|