java - Android 客户端向 C 套接字服务器发送对象失败

标签 java android c sockets network-programming

我制作了一个充当客户端的 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 代码是否正确。或者我的读取调用有一些我忽略的错误。

最佳答案

在你的代码中

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

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

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

假设 dp 定义为

struct dali_payload dp;

你可以使用类似的东西

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

关于java - Android 客户端向 C 套接字服务器发送对象失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28939830/

相关文章:

java - Android 如何将.raw数据转换为.mp3文件?

android - Galaxy Nexus 上的布局渲染是怎么回事?

c - 我们什么时候应该在 C 中使用断言?

c - 如何在我的 Objective-C 头文件中定义一个数组?

c - 如何使用 sscanf 从字符串文字中提取 long unsigned int

Java写法定义

java - RMIC 无法找到类,尽管它就在那里

Java递归和 super 素数

java - 为什么 Android 类加载器允许从另一个包反射访问包私有(private)类的公共(public)字段?

android - 无法解析 com.google.code.gson :gson:2. 8.2。由于防火墙