请问下有没有大神遇到过这种问题Android与服务器通过socket通讯时出现的EPIPE (Broken pipe)跟ECONNRESET (Connecti

leikko 2016-08-08 11:31:56
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:609)
at libcore.io.IoBridge.recvfrom(IoBridge.java:581)
at java.nio.SocketChannelImpl.readImpl(SocketChannelImpl.java:327)
at java.nio.SocketChannelImpl.read(SocketChannelImpl.java:289)
at com.dsppr.www.socket.SocketInputThread.readSocket(SocketInputThread.java:99)
at com.dsppr.www.socket.SocketInputThread.run(SocketInputThread.java:60)
Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:140)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:159)
at libcore.io.IoBridge.recvfrom(IoBridge.java:571)


java.net.SocketException: sendto failed: EPIPE (Broken pipe)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:545)
at libcore.io.IoBridge.sendto(IoBridge.java:528)
at java.nio.SocketChannelImpl.writeImpl(SocketChannelImpl.java:391)
at java.nio.SocketChannelImpl.write(SocketChannelImpl.java:349)
at com.dsppr.www.socket.TCPClient.sendMsg(TCPClient.java:180)
at com.dsppr.www.socket.TCPClient.canConnectToServer(TCPClient.java:245)
at com.dsppr.www.socket.SocketHeartThread.run(SocketHeartThread.java:54)
Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:155)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:169)
at libcore.io.IoBridge.sendto(IoBridge.java:523)


08-08 11:25:55.685: E/BroadcastQueue(2905): processNextBroadcast log test
08-08 11:25:55.685: E/BroadcastQueue(2905): processNextBroadcast log test
08-08 11:25:55.685: E/BroadcastQueue(2905): processNextBroadcast log test
08-08 11:25:55.685: E/BroadcastQueue(2905): processNextBroadcast log test
08-08 11:25:55.715: W/System.err(9005): java.net.SocketException: sendto failed: EPIPE (Broken pipe)
08-08 11:25:55.715: E/BroadcastQueue(2905): processNextBroadcast log test
08-08 11:25:55.715: W/System.err(9005): at java.net.PlainSocketImpl.sendUrgentData(PlainSocketImpl.java:473)
08-08 11:25:55.715: W/System.err(9005): at java.net.Socket.sendUrgentData(Socket.java:994)
08-08 11:25:55.715: W/System.err(9005): at com.dsppr.www.socket.TCPClient.canConnectToServer(TCPClient.java:251)
08-08 11:25:55.715: W/System.err(9005): at com.dsppr.www.socket.SocketHeartThread.run(SocketHeartThread.java:54)
08-08 11:25:55.715: W/System.err(9005): Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
08-08 11:25:55.715: W/System.err(9005): at libcore.io.Posix.sendtoBytes(Native Method)
08-08 11:25:55.715: W/System.err(9005): at libcore.io.Posix.sendto(Posix.java:160)
08-08 11:25:55.715: W/System.err(9005): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
08-08 11:25:55.715: W/System.err(9005): at java.net.PlainSocketImpl.sendUrgentData(PlainSocketImpl.java:471)
08-08 11:25:55.715: W/System.err(9005): ... 3 more
...全文
792 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我也有这个错误 ,楼主找到什么原因了吗
leikko 2016-08-09
  • 打赏
  • 举报
回复
leikko 2016-08-08
  • 打赏
  • 举报
回复
贴下这几个报错地方的代码!!!下面这3个方法对应的是上面的3个错误!!!在模拟器上没有显示这些错误,一到真机上就出现了!!求大神,各种调教!!!1
public void readSocket()
	{
		Selector selector = TCPClient.instance().getSelector();
		if (selector == null)
		{
			return;
		}
		try
		{
			// 如果没有数据过来,一直柱塞
			while (selector.select() > 0)
			{
				for (SelectionKey sk : selector.selectedKeys())
				{
					// 如果该SelectionKey对应的Channel中有可读的数据
					if (sk.isReadable())
					{
						// 使用NIO读取Channel中的数据
						SocketChannel sc = (SocketChannel) sk.channel();
						int size=1024*30;
						ByteBuffer buffer = ByteBuffer.allocate(size);
						//byte[] buffer=new byte[size];
						try
						{
							sc.read(buffer);
						} catch (IOException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
							// continue;
						}
						buffer.flip();
						String receivedString = "";
						// 打印收到的数据
						/*try
						{*/
							byte[] b=new byte[buffer.remaining()];
							buffer.get(b, 0, b.length);
							receivedString=ConvertUtils.byte2HexString(b);
						//	System.out.println(receivedString);
							//receivedString=ConvertUtils.byte2HexString(buffer);
							/*receivedString =Charset.forName("UTF-16")
									.newDecoder().decode(buffer).toString();*/
							CLog.e(tag, receivedString);
							Intent i = new Intent(Const.BC);
							i.putExtra("response", receivedString);
							UIUtils.getContext().sendBroadcast(i);
						/*} catch (CharacterCodingException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}*/
						buffer.clear();
						buffer = null;
						try
						{
							// 为下一次读取作准备
							sk.interestOps(SelectionKey.OP_READ);
							// 删除正在处理的SelectionKey
							selector.selectedKeys().remove(sk);
							
						} catch (CancelledKeyException e)
						{
							e.printStackTrace();
						}
						
					
					}
				}
			}
			// selector.close();
			// TCPClient.instance().repareRead();
			
		} catch (IOException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClosedSelectorException e2)
		{
		}
	}
/**
	 * 发送数据
	 * 
	 * @param bytes
	 * @throws IOException
	 */
	public void sendMsg(byte[] bytes) throws IOException
	{
		ByteBuffer writeBuffer = ByteBuffer.wrap(bytes);
		System.out.println(ConvertUtils.byte2HexString(bytes));
		if (socketChannel == null)
		{
			throw new IOException();
		}
		socketChannel.write(writeBuffer);
	}
/**
	 * 服务器是否关闭,通过发送一个socket信息
	 * 
	 * @return
	 */
	public boolean canConnectToServer()
	{
		try
		{
			if (socketChannel != null)
			{	byte[] pinpon = { 0x00, 0x02, 0x00, 0x00, (byte) 0xD0, 0x00,
					0x00 };
				sendMsg(CRCUtils.initData(pinpon));
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		catch (Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
leikko 2016-08-08
  • 打赏
  • 举报
回复
自顶一下,明天看看有没有牛人帮忙回帖,感激不尽

80,349

社区成员

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

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