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

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

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

Antworte allen(2)
PHPzhong

你想要完成什么样的逻辑呢?你这个程序看起来是这样:

  1. 连接服务端,然后发送数据,之后进入while循环;

  2. 阻塞读,读到数据之后做一些处理,把flag设成false,然后再阻塞在读上。

黄舟

你的server线程有问题,只写了一次数据,所以造成client在第一次读数据之后,之后的read阻塞住了。
你的server线程可以这样改,一个while循环,当read到数据,就write,否则阻塞在读上。
建议server和client分开。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!