黑马程序员技术交流社区

标题: Server与Client端都是循环的,但是程序只能执行一次,第二次就卡住 [打印本页]

作者: xxxqian1111    时间: 2015-3-17 22:41
标题: Server与Client端都是循环的,但是程序只能执行一次,第二次就卡住
TCP问题,请求帮忙解决!!!
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Server {
// Server ser = new Server();//创建本类对象
String str = null;// 存放客户端输入内容。
boolean bool = false;// 判断客服端输入内容是否为数字;true表示为数字。

boolean over =false;//判断游戏是否结束。
int array[] = new int[5];// 存放随机数
Socket client = null;
ServerSocket ss = null;
DataOutputStream dos = null;
// main方法启动Server功能
public static void main(String[] args) {
   new Server();// 创建本类对象,以调用构造方法。
}
// 构造方法
public Server() {
   try {
    ss = new ServerSocket(3001);
    this.ranMethod();// 生成5个随机数
    System.out.println();
   } catch (IOException e) {
    System.out.println("端口被占用!请重启服务器!");
e.printStackTrace();   
   }
   while (over!=true) {
    try {System.out.println("监听客户端");
     client = ss.accept();// 监听客户端
   
    } catch (IOException e) {
     System.out.println("监测客户端连接异常。");
     e.printStackTrace();
    }
    str = this.accpetMethod();// 输出客户端信息
    System.out.println(str);
    bool = this.message(str);// 判断客户端输入是否为数字,是返回true,否返回false.
    if (bool == true) {
     bool = this.judgement(bool, str);
     this.sendMethod(bool);
    } else {
     try {
      dos = new DataOutputStream(client.getOutputStream());
      dos.writeUTF("输入错误,请输入0~100间的数字!");
      dos.flush();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
}
// 服务器产生随机数并打印。
public void ranMethod() {
   Random ran = new Random();
   System.out.print("随机数字为:");
   for (int i = 0; i < 5; i++)// 生成5个随机数
   {
    int b = ran.nextInt(100);
    for (int j = 0; j <= i; j++)// 处理随机数可能重复问题
    {
     if (array[j] == b) {
      i--;
      break;
     }
     if (j == i) {
      array[i] = b;
      System.out.print(" " + array[i]);
     }
    }
   }
}
// 读取客户端信息
public String accpetMethod() {
   try {//读取客户端发送的信息
    DataInputStream dis = new DataInputStream(client.getInputStream());
    str = dis.readUTF();
    // 显示客户端信息
   
    String ip = client.getInetAddress().toString().trim();
    int port = client.getPort();
    System.out.println("客户端IP是:" + ip + "端口是: " + port);
   
   } catch (IOException e) {
    System.out.println("客户端信息读取异常!");
   }
   return str;
}
// 判断客户端输入内容是否为数字。
public boolean message(String str) {
   this.str = str;
   System.out.println("接收的客户端信息为: " + str);
   if (str != null) {
    for (int i = 0; i < str.length(); i++) {
     int a = str.charAt(i);
     if (a > 47 && a < 58)
      bool = true;
     else {
      bool = false;
      break;
     }
    }
   } else
    bool = false;
   return bool;
}


public boolean judgement(boolean bool, String str) {
   this.bool = bool;
   this.str = str;
   for (int i = 0; i < 5; i++) {
    if (array[i] == Integer.parseInt(str)) {
     bool = true;
     break;
    } else
     bool = false;
   }
   return bool;
}
// 服务器返回结果给客户端。
public void sendMethod(boolean bool) {
   this.bool = bool;
   try {
    dos = new DataOutputStream(client.getOutputStream());
    if (bool == true) {
     dos.writeUTF("恭喜你,猜对了!");
     dos.flush();
     Thread.sleep(1000);
     over=true;
    } else
     dos.writeUTF("很遗憾你猜错了,请继续!");
    dos.flush();
   
   } catch (IOException e) {
    System.out.println("给客户端发送信息异常!");
    e.printStackTrace();
   } catch (InterruptedException e) {
    System.out.println("线程睡眠异常。");
    e.printStackTrace();
   }
}
}
…………………………………………………………………………………………
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
String str = null;// 存放键盘输入。
    String str1=null;//服务器返回内容。
boolean bool = true;// 判断游戏是否结束!
boolean close=false;//当连接异常时关闭程序。
Socket sk = null;    //
DataInputStream dis = null;

BufferedReader br = null;

DataInputStream diserver = null;



// main方法启动Server功能
public static void main(String[] args) {
   new Client();// 创建本类对象,以调用构造方法。
}
// 构造方法
public Client() {
   
   try {
    sk = new Socket("192.168.1.36", 3001);
   } catch (UnknownHostException e) {
    System.out.println("连接服务器异常,请检查地址是否正确!");
    e.printStackTrace();
    close=true;
   } catch (IOException e) {
    System.out.println("连接服务器失败。");
    e.printStackTrace();
    close=true;
   }
   if(close==false){//确认成功连接服务器后才允许执行操作。
   
   System.out.println("欢迎参与猜数字游戏!");
  
   while(bool==true&&close==false){
    System.out.println("1");
    this.readMethod();
    System.out.println("2");
    this.sendMethod();
    System.out.println("3");
    this.accpetMethod();
    System.out.println("4");
   
   }
}
}
// 读取键盘输入信息
public void readMethod() {
  
   // 客户端读取键盘输入
   System.out.println("请输入0~100的数字!");
   dis = new DataInputStream(System.in);
   br = new BufferedReader(new InputStreamReader(dis));
   // 保存输入内容
   try {
    str = br.readLine();
   } catch (IOException e) {
    System.out.println("读取键盘输入异常。");
    e.printStackTrace();
   }
}
// 发送信息给服务器!
public void sendMethod() {
   DataOutputStream dos = null;
   OutputStream os=null;
   try {
    os = sk.getOutputStream();
    dos = new DataOutputStream(os);
    dos.writeUTF(str);
    dos.flush();
    System.out.println(str);
   } catch (IOException e) {
    System.out.println("信息发送异常。\n服务器可能关闭。\n\n");
    e.printStackTrace();
    close=true;
   }
}
// 读取服务器返回信息,并打印!
public void accpetMethod() {
  
   try {
    diserver = new DataInputStream(sk.getInputStream());
    if(str!=null)
    str1 = diserver.readUTF();
    System.out.println(str1);
   } catch (IOException e) {
    System.out.println("服务器返回信息读取异常!\n服务器可能关闭。");
    e.printStackTrace();
    close=true;
   }
   if(str1.equals("恭喜你,猜对了!")){
    System.out.println("游戏结束!");
    bool=false;
    try {
     dis.close();
     diserver.close();
     br.close();
    } catch (IOException e) {
   
     System.out.println("数据流关闭异常。");
     e.printStackTrace();
    }
   }
System.out.println("………………………………………………………………");
}
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2