本帖最后由 陈淑飞 于 2012-8-1 15:52 编辑
急,想了好久,问题依旧没解决,只好把问题从13期移动12期了。
问题描述如下:
小测试代码,目的是发送端从键盘中,输入字符。接收端接到字符后转成大写。当发送端发"over"时,接受端接到打开并结束,关闭流掉。
但测试代码存在问题,while循环停不了。下面是代码:
代码一: while循环用 类静态变量FLAG控制:- import java.net.*;
- import java.io.*;
- import java.util.Date;
- public class UdpSocketTest{
- public static boolean FLAG = true ;
- public static void main(String[] args){
- }
- }
- class SerSocket{
- public static void main(String[] args) throws Exception{
- DatagramSocket ds = new DatagramSocket(40009);
- while(UdpSocketTest.FLAG)
- {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- InetAddress ia = dp.getAddress();
- String ip = ia.getHostAddress();
- System.out.println(ip+"..... connected");
- String content = new String(dp.getData(),0,dp.getLength());
- content = content.toUpperCase();
- System.out.println("content=="+content);
- Thread.sleep(5000); //先暂停5秒,等if over先执行,置FLAG为false
- Date d = new Date();
- System.out.println(d.toString()+"Ser执行UdpSocketTest.FLAG==::"+UdpSocketTest.FLAG);
- }
- ds.close();
- }
- }
- class CliSocket{
- public static void main(String[] args) throws Exception{
- DatagramSocket ds = new DatagramSocket();
- InetAddress ia = InetAddress.getByName("127.0.0.1");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line=br.readLine())!=null){
- byte[] buf = line.getBytes();
- DatagramPacket dp = new DatagramPacket(buf,buf.length,ia,40009);
- ds.send(dp);
- if("over".equals(line))
- {
- UdpSocketTest.FLAG = false;
- Date d = new Date();
- System.out.println(d.toString()+"Cli执行UdpSocketTest.FLAG==::"+UdpSocketTest.FLAG);
- break ;
- }
- }
- ds.close();
- br.close();
- }
- }
复制代码 代码二: while循环用 单例 Flag成员变量 控制:- import java.net.*;
- import java.io.*;
- import java.util.Date;
- public class UdpSocketTest2{
- //public static boolean FLAG = true ;
- private static final UdpSocketTest2 ut = new UdpSocketTest2(); //郁闷,换成单例类,还是一样存在问题。
- private boolean flag = true ;
- private UdpSocketTest2(){
- }
- public static UdpSocketTest2 getInstance(){
- return ut ;
- }
- public void setFlag(boolean flag){
- this.flag = flag;
- }
- public boolean getFlag(){
- return flag ;
- }
- public static void main(String[] args){
- }
- }
- class SerSocket{
- public static void main(String[] args) throws Exception{
- DatagramSocket ds = new DatagramSocket(40009);
- UdpSocketTest2 ut = UdpSocketTest2.getInstance();
- while(ut.getFlag())
- {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- InetAddress ia = dp.getAddress();
- String ip = ia.getHostAddress();
- System.out.println(ip+"..... connected");
- String content = new String(dp.getData(),0,dp.getLength());
- content = content.toUpperCase();
- System.out.println("content=="+content);
- Thread.sleep(5000); //先暂停5秒,等if over先执行,置FLAG为false
- Date d = new Date();
- System.out.println(d.toString()+"Ser 执行ut.getFlag()==::"+ut.getFlag());
- }
- ds.close();
- }
- }
- class CliSocket{
- public static void main(String[] args) throws Exception{
- UdpSocketTest2 ut = UdpSocketTest2.getInstance();
- DatagramSocket ds = new DatagramSocket();
- InetAddress ia = InetAddress.getByName("127.0.0.1");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line=br.readLine())!=null){
- byte[] buf = line.getBytes();
- DatagramPacket dp = new DatagramPacket(buf,buf.length,ia,40009);
- ds.send(dp);
- if("over".equals(line))
- {
- ut.setFlag(false);
- Date d = new Date();
- System.out.println(d.toString()+"Cli 执行ut.getFlag()==::"+ut.getFlag());
- break ;
- }
- }
- ds.close();
- br.close();
- }
- }
复制代码 Why?为什么循环停不了,测试结果如图:
图上,显示证明了,FLAG已经被置为flase了,为什么5秒后打印却是true呢?
用单例模式也是一样的,求解?
----
ps: 不要说在接收端也弄个over的判断,再break循环。 我就想知道,为什么以上两种方式的while循环结束不了,为什么5秒前FLAG为false,5秒后为true了。
|
|