【问题标题】:Android Client send object to C socket server failsAndroid 客户端向 C 套接字服务器发送对象失败
【发布时间】:2015-05-10 11:32:21
【问题描述】:

我已经制作了一个作为客户端的 Android 套接字程序。一个 c 套接字服务器正在运行,它在套接字上执行 recv。 read 被执行多次,即使我只发送一次有效载荷结构。

它发送以下数据:

import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class DataToServer implements Externalizable {

    public int id;
    public int cmd;
    public byte active;
    public byte level;
    public byte group;

    public DataToServer(int id, int cmd, byte active, byte level, byte group) {
        super();
        this.id = id;
        this.cmd = cmd;
        this.active = active;
        this.level = level;
        this.group = group;
    }

    public DataToServer() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public byte getActive() {
        return active;
    }

    public void setActive(byte active) {
        this.active = active;
    }

    public byte getLevel() {
        return level;
    }

    public void setLevel(byte level) {
        this.level = level;
    }

    public byte getGroup() {
        return group;
    }

    public void setGroup(byte group) {
        this.group = group;
    }

    @Override
    public void readExternal(ObjectInput input) throws IOException,
            ClassNotFoundException {
        this.id = input.readInt( );
        this.cmd = input.readInt();
        this.active = input.readByte();
        this.level = input.readByte(); 
        this.group = input.readByte();
    }

    @Override
    public void writeExternal(ObjectOutput output) throws IOException {
        output.writeInt(id);
        output.writeInt(cmd);
        output.writeByte(active);
        output.writeByte(level);
        output.writeByte(group);
    }   
}

接下来我通过下面的Android代码发送:

public class MainActivity extends Activity {

    private Socket socket;
    private static final int SERVERPORT = 3456;
    private static final String SERVER_IP = "192.168.1.10";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new ClientThread()).start();

    }

    public void sendData(View view){
        try{
            OutputStream objStream = socket.getOutputStream();
            ObjectOutputStream ObjOpStream = new ObjectOutputStream(objStream);
            byte active = 65;
            byte level = (byte)150;
            byte group = (byte) 255;
            DataToServer data = new DataToServer();
            data.setId(10);
            data.setCmd(15);
            data.setActive(active);
            data.setLevel(level);
            data.setGroup(group);
            ObjOpStream.writeObject(data);
            ObjOpStream.close();
            objStream.close();

        }catch (UnknownHostException e) {
            e.printStackTrace();
        }catch (IOException e) {
           e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class ClientThread implements Runnable {
        @Override
        public void run() {
            try {
                InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddress,SERVERPORT);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

在 C 服务器端,我有以下代码要接收。

struct dali_payload
{
        int id;
        int cmd;
        char active;
        char level;
        char group;
};



  printf("Payload size: %d\n", sizeof(dp));
    while(1)
    {
        read_size = read(client_sock ,&dp ,sizeof(dp));
        if(read_size <= 0)
            break;
        printf("Received bytes: %d\n", read_size);
        printf("Id: %x\n", (dp.id));
        printf("cmd: %x\n",(dp.cmd));
        printf("active: %x\n", (dp.active));
        printf("level: %x\n", (dp.level));
        printf("group: %x\n", (dp.group));
    }

OUTPUT:
Socket created
bind done
Waiting for incoming connections...
Connection accepted
Payload size: 12
Received bytes: 2
Id: edac
cmd: 0
active: 0
level: 0
group: 0
Received bytes: 2
Id: 500
cmd: 0
active: 0
level: 0
group: 0
Received bytes: 1
Id: 573
cmd: 0
active: 0
level: 0
group: 0
Received bytes: 12
Id: 632a0072
cmd: 652e6d6f
active: 78
level: 61
group: 6d
Received bytes: 12
Id: 612e656c
cmd: 6f72646e
active: 69
level: 64
group: 73
Received bytes: 12
Id: 74656b63
cmd: 6f6d6564
active: 2e
level: 44
group: 61
Received bytes: 12
Id: 536f5461
cmd: 65767265
active: 72
level: 87
group: 2b
Received bytes: 12
Id: e51d8e7a
cmd: cc0
active: 78
level: 70
group: 77
Received bytes: 12
Id: a000000
cmd: f000000
active: 41
level: 96
group: ff

read 被执行多次,即使我只发送一次有效载荷结构。

输出显示每次接收到的一些常量数据。只有最后 12 个字节是正确的数据。

问题出在哪里。发送对象的 Android 代码是否正确。或者我的读取调用有一些我忽略的错误。

【问题讨论】:

    标签: java android c sockets network-programming


    【解决方案1】:

    在您的代码中

    recv(client_sock ,&dp ,1, 0);
    

    将一次读取1 字节。这很可能不是你想要的。

    理想情况下,您应该将提供的缓冲区的长度作为第三个参数。

    假设dp被定义为

    struct dali_payload dp;
    

    你可以使用类似的东西

     read_size = recv(client_sock ,&dp ,sizeof(dp), 0);
    

    【讨论】:

    • @ianand 我说的是socklen_t*)&amp;c 部分。它没有匹配的起始 (
    • 当我将其格式化为一行时被删除。对此感到抱歉。请验证
    • 收到了不是我的数据包的常量数据。只有最后 12 个字节是我的数据包。
    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2012-12-01
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多