关于android socket通信接收服务器数据问题

ziyu1da 2014-02-17 10:43:12
先说一下我遇到的问题,我想通过android手机socket通信接收server发送过来的16进制数据,下面是我客户端的代码,当我写的应用程序只有这一个activity时,测试是可是实现 从server发送的数据的。但是当我把这个activity加入到一个应用程序中,通过A界面跳转到这个界面后,再点击button,程序就会一直没响应,通过logcat,发现程序停在了nReadLen = dis.read(buffer, nIdx, nTotalLen - nIdx);处。不知道问题出现在什么地方求解释,为什么相同的代码,经过这么一挪动们就出现这个问题,麻烦各位给分析一下问题所在,谢谢,

package com.carman.activity;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.carman.activity.R;

public class ReceiveDataActivity extends Activity {

private TextView textview1;
private Button btn;

byte[] relayState = new byte[2];//保存终端设备状态
HandlerThread handlerThread = new HandlerThread("handler_thread");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Socket socket = null;
int objContentleng = 24;
//fc 4b 1f 25
byte[] buffer = new byte[objContentleng];
byte[] relayState = new byte[2];// 第一位是0x25地址的状态,第二位为0x4b地址的状态
int nIdx = 0;
int nTotalLen = buffer.length;
int nReadLen = 0;
try {
socket = new Socket("192.168.1.123",502);
DataInputStream dis = new DataInputStream(socket.getInputStream());
while (nIdx < nTotalLen)
{
nReadLen = dis.read(buffer, nIdx, nTotalLen - nIdx);
if (nReadLen > 0)
{
nIdx = nIdx + nReadLen;
}
else
{
break;
}
}
for(int i = 0; i < objContentleng;i++)
System.out.println(buffer[i]);
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

textview1.setText(buffer.toString());
}
});


}
public void findView(){

textview1 = (TextView)findViewById(R.id.textView1);
btn = (Button)findViewById(R.id.button1);


}
}
...全文
462 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziyu1da 2014-03-28
  • 打赏
  • 举报
回复
结贴晚了,我已经发现问题所在,上述代码没问题,是我之前建立了一个通信,在用完之后并没有关闭,谢谢楼上各位答疑
kevinVane 2014-03-05
  • 打赏
  • 举报
回复
read是阻塞读,一定要放在线程里面
afairycell 2014-03-05
  • 打赏
  • 举报
回复
while (nIdx < nTotalLen) 这里一直卡死了吧?转头又卡在read上了,没跑下去
  • 打赏
  • 举报
回复
read阻塞了,你从一个跳转过去出问题,要么就是权限问题或者什么东西忘记关了
Birds2018 2014-02-17
  • 打赏
  • 举报
回复
看起来代码没有什么问题, nReadLen = dis.read(buffer, nIdx, nTotalLen - nIdx); 检查一下是否是下标有问题。

	public static byte[] readInputStream(InputStream ins) {
		if (ins == null) {
			return null;
		}
		BufferedInputStream bis = new BufferedInputStream(ins);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			byte[] buffer = new byte[128];
			int n = -1;
			while ((n = bis.read(buffer)) != -1) {
				bos.write(buffer, 0, n);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return bos.toByteArray();
	}

读完了所有的数据流以后 再进行byte数组操作。 建议把网络操作放到线程里面。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧