java - 用Socket二次发送消息失败,怎么办?

查看:141
本文介绍了java - 用Socket二次发送消息失败,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

解决方案

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

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

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

这篇关于java - 用Socket二次发送消息失败,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆