我有一张wifi sd卡,在我用相机拍摄照片后,会向我发送TCP包,其中包含有关新内存图像的信息.
在终端上,我可以使用此netcat命令读取输入流. netcat连接到ip 192.168.11.254并侦听端口5566.第二行是我收到的图像路径.
$nc 192.168.11.254 5566>/mnt/sd/DCIM/109_0302/imgP0101.JPG
在我的应用程序上,我有一个Java客户端套接字连接到相同的IP和端口.但我没有像netcat那样收到输入流,没有任何反应.
voID startListenForTCP (){ Thread TCPListenerThread = new Thread(new Runnable() { @OverrIDe public voID run() { Boolean run = true; String serverMessage = null; InetAddress serverAddr = null; try { serverAddr = InetAddress.getByname("192.168.11.254"); Socket clIEntSocket = new Socket(serverAddr, 5566); try{ BufferedReader in = new BufferedReader(new inputStreamReader(clIEntSocket.getinputStream())); while (run) { serverMessage = in.readline(); mHandler.post(new displayToast(context, "TCP : " + serverMessage)); } } catch(UnkNownHostException e) { mHandler.post(new displayToast(context, "UnkNownHostException")); } catch(IOException e) { mHandler.post(new displayToast(context, "IOException")); } finally { clIEntSocket.close(); } } catch (IOException e) { e.printstacktrace(); } } }); TCPListenerThread.start();}
解决方法:
试试这可能有效
步骤1
删除这两行:
serverAddr = InetAddress.getByname("192.168.11.254");Socket clIEntSocket = new Socket(serverAddr, 5566);
添加此行:
Socket clIEntSocket = new Socket("192.168.11.254", 5566);
第2步
删除此代码:
try { BufferedReader in = new BufferedReader(new inputStreamReader(clIEntSocket.getinputStream())); while (run) { serverMessage = in.readline(); mHandler.post(new displayToast(context, "TCP : " + serverMessage)); }}
添加此代码:
try { char[] buffer = new char[2048]; int charsRead = 0; BufferedReader in = new BufferedReader(new inputStreamReader(clIEntSocket.getinputStream())); while ((charsRead = in.read(buffer)) != -1) { String message = new String(buffer).substring(0, charsRead); Log.e("In While", "msg :"+message); }}
总结 以上是内存溢出为你收集整理的java – TCP客户端套接字.连接并等待输入流全部内容,希望文章能够帮你解决java – TCP客户端套接字.连接并等待输入流所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)