java - 用Socket二次发送消息失败,怎么办?
迷茫
迷茫 2017-04-18 09:45:39
0
2
541

本人对socket真的是小白级的,因为程序要求才硬着头皮写了个函数,结果第一次引用函数时,socket还能发送消息,但之后一直都被阻塞(?)了,直到我把整个程序关掉才发送出去。来这里看看有没有大侠可以解答一下我的问题。
以下是该函数代码:

        Status<Area> status=new Status<Area>();
       
        InetAddress addr;
        addr = InetAddress.getByName(HOST_NAME);
        Socket mSocket;
        mSocket = new Socket(addr, PORT_NO);
        out = mSocket.getOutputStream();
        mFlag = true;
        
        try
        {
                Area a = new Area();
                byte typeChosen=(byte) 0xff;
                byte [] output=new byte[10];
                //**中间省略output的内容**
                out.write(output);
                
                InputStream mInput = null;
                byte[] buffer = new byte[65536];
                int size = -1;
                boolean flag=true;
                while (flag) {
                    try {
                    // 此处为测试代码,测试代码每次运行能成功执行。
                    //所以问题应该不在parseFrontEndMsg函数中,所以此函数我就不放上来了。
//                        if(flag){
//                            byte[] realBuffer = new byte[18];
//                            realBuffer=new byte[] {0x00,0x04,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x09};
//                            parseFrontEndMsg(realBuffer,typeChosen);
//                            flag=false;
//                        }
                        if (size < 0) {
                            System.out.println("等待接收前置器发送信息....");
                        } else {
                            byte[] realBuffer = new byte[size];
                            System.arraycopy(buffer, 0, realBuffer, 0, size);
                            System.out.print("Message from front end: ");
                            printBytes(realBuffer);
                            parseFrontEndMsg(realBuffer,typeChosen);
                            flag=false;
                        }
                        //**问题可能出在这两句。(我猜
                        mInput = mSocket.getInputStream();
                        size = mInput.read(buffer);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }            
                }
                    
//                mServerThread = new ServerThread();
//                mServerThread.init(addr,PORT_NO,typeChosen);
//                out.write(output);
//                mServerThread.start();
                
                FRAME_ID++;
            }
            status.setCode(1);
            status.setList(areaList);
            return status;

        }
         catch (Exception e) {
             e.printStackTrace();
            return new Status(0, e.toString());
        }
        finally 
        {
            System.out.println("close the Client socket and the io.");
            mSocket.close();
        }
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(2)
PHPzhong

你想要完成什麼樣的邏輯呢?你這個程式看起來是這樣:

  1. 連接服務端,然後發送數據,之後進入while循環;

  2. 阻塞讀,讀到資料之後做一些處理,把flag设成false,然後再阻塞在讀上。

黄舟

你的server線程有問題,只寫了一次數據,所以造成client在第一次讀數據之後,之後的read阻塞住了。
你的server執行緒可以這樣改,一個while循環,當read到數據,就write,否則阻塞在讀上。
建議server和client分開。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!