Вопрос

i'm trying to send an object (ClientModel) through an UDP socket. The socket part works fine because if I try to send a simple string it works. I reach an exception, where's the error? The client says "Client: Fail.". Here's the code, for the moment only the client side because the problem is the sending process, than I can think about receiving this stuff :)

Thanks

    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;
    }
}

Here's the log, sorry:

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
Это было полезно?

Решение

You need to learn more about serialization in java.

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

Другие советы

Don't forget UDP is connection less and will not get your data their 100% of the time in practace. check out http://cs.nyu.edu/~bacon/phd-thesis/diss/node32.html

So here is what I would do. Verify that your serialziation is working, but serializing the object in memory and then deserializing it from memory. If that works you know its a network issue.

If its a network issue, you might try switching to TCP based streams and sending that data that way. Also you might want to add a checksum to your data to make sure its arriving correctly.

    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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top