1

I am trying to use Android's VpnService to forward packets to their original destination after i receive them. After writing data to the socket with the correct destination address, I am trying to get the response like this:

Socket socket = SocketChannel.open().socket();
socket.connect(new InetSocketAddress(ipPacket.getDestinationIp(), ipPacket.getDstPort()));

ByteBuffer serverResponse = ByteBuffer.allocate(65535);
InputStream socketInputStream = socket.getInputStream();
int responseLength = socketInputStream.read(serverResponse.array());

However the line int responseLength = socketInputStream.read(serverResponse.array()); is causing the following issue:

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

How can i fix this?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
TychoTheTaco
  • 664
  • 1
  • 7
  • 28
  • Tell the server on the other end (=the peer) not to close the connection :) ["what does reset by peer mean"](http://stackoverflow.com/questions/1434451/what-does-connection-reset-by-peer-mean) Whatever the error causing this is, it's not in the few lines of code you show. How do you get that `Socket`? Did you `.close()` it or a stream attached to it earlier? – zapl Jun 07 '16 at 21:05
  • @zapl I have created the socket like this: `Socket socket = SocketChannel.open().socket();` and `socket.connect(new InetSocketAddress(ipPacket.getDestinationIp(), ipPacket.getDstPort()));` where the destination IP and port are from the packet received by the VPN's `tun` interface – TychoTheTaco Jun 07 '16 at 21:23
  • @EJP Sorry i am confused about what you are trying to say – TychoTheTaco Jun 07 '16 at 23:28
  • OT but why are you using this bizarre mixture of NIO and streams and ByteBuffers and byte arrays? Make up your mind. Use one or the other. If you're a beginner in networking I would definitely use sockets and streams.. – user207421 Jun 07 '16 at 23:34
  • @EJP I was using `SocketChannel.open()` because my `protect()` method in `VpnService` wouldn't work without it. I have changed it and now it does work without errors. Thank you for the help! – TychoTheTaco Jun 07 '16 at 23:53
  • Never mind, it seemed like it was working but apparently it is still giving the same error – TychoTheTaco Jun 08 '16 at 01:14

0 Answers0