生信小木屋 2017-08-10 10:34 采纳率: 36.4%
浏览 527

java网络编程对于循环结构的困惑

 package network06;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Sever {
    public static void main(String[] args) {
        new Sever().startup();
    }
    private void startup(){
        ServerSocket ss =null;
        Socket s= null;
        try {
            ss =new ServerSocket(5858);
            while(true){
                s =ss.accept();//来一个就创建一个Socket对象
                SeverThread st = new SeverThread(s);//这两句无非是为了run()方法
                new Thread(st).start();             //
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
                try {
                    if(ss!=null)ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    private class SeverThread implements Runnable{
        private Socket s= null;
        private BufferedReader br=null;
        private PrintWriter pw = null;
        private String name ;
        private boolean flag =true;

        public SeverThread(Socket s) throws IOException {
            this.s =s;
            br =new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw = new PrintWriter(s.getOutputStream(),true);
            name = s.getInetAddress().getHostAddress()+":"+s.getPort();
            System.out.println(name+"已经连接上");
        }
        private void receive() throws IOException{
            String str=null;
            while((str=br.readLine())!=null){
                if(str.equalsIgnoreCase("quite")){
                    stop();
                    //关闭,进行资源的释放
                    pw.println("close");
                    break;
                }
                System.out.println(name+"--"+str);
                pw.println("Receive"+str);

            }
            System.out.println(name+"已经离开");
        }
        private void stop(){
            flag=false;
        }

        public void run() {
            try {
                //while(true){
                //  if(!flag)break;
                    receive();
                //}
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    if(s!=null)s.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}



线程调用run()方法,这个外层循环结构,有什么作用?while((str=br.readLine())!=null)好像可以控制输入循环,就一直等着我们输入
图片说明

  • 写回答

1条回答

  • zqbnqsdsmd 2018-06-14 16:21
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?