在套接字java上发送对象?

时间:2016-08-20 20:48:09

标签: java android sockets object

我正在尝试发送对象并通过套接字接收对象但是出现了一些错误。

早些时候我正在尝试这种方式,但工作正常,但我无法发送重型对象。下面是代码 发送时

            ObjectOutputStream mOut=null;
            mSocket = new Socket();
            mSocket.bind(null);
            mSocket.connect((new InetSocketAddress(mPeerIP, Constants.WELCOME_SOCKET_PORT)), 3000);
            mOut =  new ObjectOutputStream(mSocket.getOutputStream());
            mOut.writeObject(mMsg);
            mOut.flush();

接收

mIn = new ObjectInputStream(new BufferedInputStream(mSocket.getInputStream()));
            Object receivedMsg = mIn.readObject();

在stackoverflow上搜索后,我开始知道以字节的形式发送对象,以便也可以传输大文件:下面是代码。

发送方

ObjectOutput mOut=null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream out=null;
mSocket = new Socket();
mSocket.bind(null);
mSocket.connect...
out = mSocket.getOutputStream();
mOut =  new ObjectOutputStream(baos);
mOut.writeObject(mMsg);
byte[] bytes = baos.toByteArray();
out.write(bytes);

在接收方

InputStream in = null;
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ByteArrayInputStream bis = null;
in=mSocket.getInputStream();
byte buffer[] = new byte[1024];
for(int s; (s=in.read(buffer)) != -1; ) **//At this line i am getting Error**
{
    baos1.write(buffer, 0, s);
}
byte result[] = baos1.toByteArray();
bis=new ByteArrayInputStream(result);
mIn = new ObjectInputStream(bis);
Object o = mIn.readObject();

但是使用这个我不断得到这个错误

08-21 01:15:40.267 5391-6930/? W/System.err: java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
08-21 01:15:40.267 5391-6930/? W/System.err:     at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:588)
08-21 01:15:40.267 5391-6930/? W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:552)
08-21 01:15:40.267 5391-6930/? W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
08-21 01:15:40.267 5391-6930/? W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
08-21 01:15:40.267 5391-6930/? W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
08-21 01:15:40.267 5391-7057/? W/System.err: java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
08-21 01:15:40.267 5391-7057/? W/System.err:     at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:588)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:552)java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:588)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:552)
08-21 01:15:40.277 5391-7005/? W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
08-21 01:15:40.277 5391-7005/? W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
08-21 01:15:40.277 5391-7005/? W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
08-21 01:15:40.277 5391-7005/? W/System.err:     at java.io.InputStream.read(InputStream.java:162)
08-21 01:15:40.277 5391-7005/? W/System.err:     at com.nearpeer.app.NewConnectionWorkerThread.SendDiscoveryMessage(NewConnectionWorkerThread.java:426)
08-21 01:15:40.277 5391-7005/? W/System.err:     at com.nearpeer.app.NewConnectionWorkerThread.ActiveDiscoveryProcedure(NewConnectionWorkerThread.java:386)
08-21 01:15:40.277 5391-7005/? W/System.err:     at com.nearpeer.app.NewConnectionWorkerThread.InitiateRelevantActiveTransaction(NewConnectionWorkerThread.java:682)
08-21 01:15:40.277 5391-7005/? W/System.err:     at com.nearpeer.app.NewConnectionWorkerThread.run(NewConnectionWorkerThread.java:78)
08-21 01:15:40.277 5391-7005/? W/System.err: Caused by: android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.Posix.recvfromBytes(Native Method)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.Posix.recvfrom(Posix.java:189)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
08-21 01:15:40.277 5391-7005/? W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:549)

如果你有想法,你也可以建议我更好地做到这一点。

0 个答案:

没有答案