求助,为啥socket收不到ack信息

本文探讨了 Android 客户端使用 Socket 与服务器交互时遇到的问题,即客户端发送消息后无法接收到服务器的 ACK 响应。文章提供了完整的代码示例,并详细描述了客户端发送数据的过程及预期接收响应的行为。
求助:按下按钮后,会socket送出一个讯息给服务器,服务器收到之后,会回复一个ack!
用putty可以看到服务器是有回复的,但为啥客户端收不到,哪错了?求解~
奉上程序码:

按钮事件
public void onClick(View v) {
        if (v == recmac)
        {
            
            String hgcmd;
            hgcmd = "RsrS@H=1=FFFFFF=4=2=2=0=0D=0A=WIFI=1234";
/*send cmd*/
                    startService(
                            new Intent(this, NotificationService.class)
                                    .setAction(NotificationService.ACTION_SENT)
                                    .putExtra(NotificationService.EXTRA_PARAM_TARGET_ADDR, "192.168.168.254")
                                    .putExtra(NotificationService.EXTRA_PARAM_TARGET_PORT, 12098)
                                    .putExtra(NotificationService.EXTRA_PARAM_TARGET_DATA, hgcmd));
            //recv ack;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startService(new Intent(MainActivity.this, NotificationService.class)
                            .setAction(NotificationService.ACTION_RECV_MPRMAC));
                }
            },1000L);
        }
}


NotificationService.class
private static ConcurrentLinkedQueue<Map<String, Object>> mQueueSend = new ConcurrentLinkedQueue<>();

private static Thread mThreadConn = null;
private static Thread mthreadRecvMpr = null;
private static Thread mthreadRecvData = null;

private static final Lock mMutexSocket = new ReentrantLock(true);
private static final Lock mMutexTxData = new ReentrantLock(true);
private static final Lock mMutexRxData = new ReentrantLock(true);

private static InetSocketAddress address = new InetSocketAddress(MPR_TARGET_ADDR_DEF, TARGET_PORT_DEF);

private static Socket socket = null;
private static BufferedWriter writer = null;
private static BufferedReader reader = null;


    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(this.getClass().getSimpleName(), "socket:onStartCommand~~~");
        if ( null != intent && null != intent.getAction() ) {
            Map<String, Object> map = new HashMap<>();
            if ( ACTION_SENT.equals(intent.getAction()) ) {
                if ( true == intent.hasExtra(EXTRA_PARAM_TARGET_ADDR) &&
                        true == intent.hasExtra(EXTRA_PARAM_TARGET_PORT) &&
                        true == intent.hasExtra(EXTRA_PARAM_TARGET_DATA) ) {
                    mMutexSocket.lock();
                    address = new InetSocketAddress(
                            intent.getStringExtra(EXTRA_PARAM_TARGET_ADDR),
                            intent.getIntExtra(EXTRA_PARAM_TARGET_PORT, TARGET_PORT_DEF));
                    mMutexSocket.unlock();         
                    mThreadConn = new Thread(mRunnableConnDev);
                    mThreadConn.start();               
                    map.put(EXTRA_PARAM_TARGET_ADDR, intent.getStringExtra(EXTRA_PARAM_TARGET_ADDR));
                    map.put(EXTRA_PARAM_TARGET_PORT, intent.getIntExtra(EXTRA_PARAM_TARGET_PORT, TARGET_PORT_DEF));
                    map.put(EXTRA_PARAM_TARGET_DATA, intent.getStringExtra(EXTRA_PARAM_TARGET_DATA));
                    
mMutexTxData.lock();
                    mQueueSend.add(map);
                    mMutexTxData.unlock();
                }
            }

            if ( ACTION_RECV_MPRMAC.equals(intent.getAction()) ) {
                mthreadRecvMpr = new Thread(mRunnableRecvMPRMAC);
                mthreadRecvMpr.start();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }


public final Runnable mRunnableRecvMPRMAC = new Runnable() {
        @Override
        public void run() {
            try{
                mMutexRxData.lock();
                socket = new Socket ("192.168.168.254", 12098);
                reader = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
System.out.println("reader");
                if (reader.readLine() == null)
                {
                    System.out.println("recv null");
                }else
                {
                    System.out.println(reader.readLine());
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                mMutexRxData.unlock();
            }
        }
    };

    private static final Runnable mRunnableConnDev = new Runnable() {
        public void run() {
            try {
                mMutexSocket.lock();
                socket = new Socket("192.168.168.254", 12098);               
                writer = new BufferedWriter(new OutputStreamWriter(
                        socket.getOutputStream()));
                Map<String, Object> map = null;
                if (false == mQueueSend.isEmpty()) {
                    map = mQueueSend.peek();                    
                    try {
                        mMutexTxData.lock();
                        final byte[] bytes = map.get(EXTRA_PARAM_TARGET_DATA).toString().getBytes();
                        String cmd = new String(bytes, StandardCharsets.UTF_8);
                        writer.write(cmd + "\n");
                        writer.flush();
                    } catch (IOException e) {
                    } finally {
                        mMutexTxData.unlock();                        
                    }
                    mQueueSend.poll();
                }
            } catch (SocketException e) {
                Log.d(this.getClass().getSimpleName(), "new Socket ---> NG!");
            } catch (UnknownHostException e) {
                Log.d(this.getClass().getSimpleName(), "new Socket ---> Exception!");
            } catch (IOException e) {
                Log.d(this.getClass().getSimpleName(), "new Socket ---> Exception!");
            } finally {
                mMutexSocket.unlock();
            }
        }
    };



程序只会停在System.out.println("reader");
Putty 画面可看得服务器是有送出讯息,后面的讯息不会显示,不知为什么会这样,求解


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值