Android通过UDP套接字发送对象

时间:2012-11-29 15:54:15

标签: java android sockets udp bytearrayoutputstream

我正在尝试通过UDP套接字发送对象(ClientModel)。套接字部分工作正常,因为如果我尝试发送一个简单的字符串它的工作原理。我达成了一个例外,错误在哪里?客户说“客户:失败”。这是代码,目前只有客户端,因为问题是发送过程,而不是我可以考虑接收这些东西:)

由于

    public class ClientActivity extends Activity {
    public static final int SERVERPORT = 5050;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client);

        try {
            InetAddress serverAddr = getBroadcastAddress();

            DatagramSocket socket = new DatagramSocket();

            //byte[] buf = "whatsup bitches".getBytes();

            ClientModel sendingMessage = new ClientModel();
            sendingMessage.data = 999;
            sendingMessage.name = "sample";
            System.out.println(sendingMessage);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(sendingMessage);
            byte[] buf = baos.toByteArray();


            DatagramPacket packet = new DatagramPacket(buf, buf.length,
            serverAddr, SERVERPORT);
            socket.send(packet);
            Log.d("UDP", "Client: Packet sent to server on address: " + serverAddr);
        }
        catch (Exception e) {
            Log.d("UDP", "Client: Fail");
        }
    }

    private InetAddress getBroadcastAddress() throws IOException {
        WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        DhcpInfo myDhcpInfo = myWifiManager.getDhcpInfo();
        if (myDhcpInfo == null) {
            System.out.println("Could not get broadcast address");
            return null;
        }
        int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask)
                    | ~myDhcpInfo.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
        return InetAddress.getByAddress(quads);
    }

    class ClientModel {
        public int data;
        public String name;
    }
}

这是日志,对不起:

11-29 17:10:09.933: I/System.out(5700): com.example.ClientActivity$ClientModel@4054b800
11-29 17:10:09.964: W/System.err(5700): java.io.NotSerializableException: com.example.ClientActivity$ClientModel
11-29 17:10:09.964: W/System.err(5700):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
11-29 17:10:09.964: W/System.err(5700):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
11-29 17:10:09.964: W/System.err(5700):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
11-29 17:10:09.964: W/System.err(5700):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
11-29 17:10:09.964: W/System.err(5700):     at com.example.ClientActivity.onCreate(ClientActivity.java:39)
11-29 17:10:09.964: W/System.err(5700):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 17:10:09.964: W/System.err(5700):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
11-29 17:10:09.964: W/System.err(5700):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
11-29 17:10:09.964: W/System.err(5700):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
11-29 17:10:09.964: W/System.err(5700):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
11-29 17:10:09.964: W/System.err(5700):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 17:10:09.964: W/System.err(5700):     at android.os.Looper.loop(Looper.java:130)
11-29 17:10:09.964: W/System.err(5700):     at android.app.ActivityThread.main(ActivityThread.java:3835)
11-29 17:10:09.964: W/System.err(5700):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 17:10:09.964: W/System.err(5700):     at java.lang.reflect.Method.invoke(Method.java:507)
11-29 17:10:09.964: W/System.err(5700):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
11-29 17:10:09.964: W/System.err(5700):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
11-29 17:10:09.964: W/System.err(5700):     at dalvik.system.NativeStart.main(Native Method)
11-29 17:10:09.964: D/UDP(5700): Client: Fail

3 个答案:

答案 0 :(得分:3)

您需要了解有关java中序列化的更多信息。

class ClientModel implements Serializable{
    public int data;
    public String name;
}

答案 1 :(得分:0)

不要忘记UDP连接较少,并且不会在100%的时间内获得数据。看看http://cs.nyu.edu/~bacon/phd-thesis/diss/node32.html

所以这就是我要做的。验证您的serialziation是否正常工作,但是在内存中序列化对象,然后从内存中反序列化它。如果可行,您就会知道它的网络问题。

如果是网络问题,您可能会尝试切换到基于TCP的流并以这种方式发送该数据。您还可以为数据添加校验和,以确保其正确到达。

答案 2 :(得分:0)

    ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
    ObjectInputStream ois = new ObjectInputStream(bais); 
    bais.close(); // move this to end of code
    ois.close();  // move this to end of code

    receivingMessage = (ClientModel) ois.readObject(); //you can't read if you've closed the streams