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

本人对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();
        }
迷茫
迷茫

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

reply all(2)
PHPzhong

What kind of logic do you want to accomplish? Your program looks like this:

  1. Connect to the server, then send data, and then enter the while loop;

  2. Block reading, do some processing after reading the data, put flag设成false, and then block reading again.

黄舟

There is a problem with your server thread. It only writes data once, so after the client reads the data for the first time, subsequent reads are blocked.
Your server thread can be changed like this, a while loop, when the data is read, write, otherwise it will be blocked in reading.
It is recommended to separate server and client.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!