求助:按下按钮后,会socket送出一个讯息给服务器,服务器收到之后,会回复一个ack!
用putty可以看到服务器是有回复的,但为啥客户端收不到,哪错了?求解~
奉上程序码:
按钮事件
NotificationService.class
程序只会停在System.out.println("reader");
Putty 画面可看得服务器是有送出讯息,后面的讯息不会显示,不知为什么会这样,求解

用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 画面可看得服务器是有送出讯息,后面的讯息不会显示,不知为什么会这样,求解

本文探讨了 Android 客户端使用 Socket 与服务器交互时遇到的问题,即客户端发送消息后无法接收到服务器的 ACK 响应。文章提供了完整的代码示例,并详细描述了客户端发送数据的过程及预期接收响应的行为。
3119

被折叠的 条评论
为什么被折叠?



