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

时间:2015-03-09 10:32:10

标签: java android c sockets network-programming

我制作了一个充当客户端的Android套接字程序。正在运行的c套接字服务器在套接字上执行recv。 读取会多次执行,即使我只发送一次有效负载结构。

发送以下数据:

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

读取多次执行,即使我只发送一次有效负载结构。

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

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

1 个答案:

答案 0 :(得分:0)

代码

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

一次读取1个字节。很可能那些不是你想要的东西。

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

假设dp被定义为

struct dali_payload dp;

你可以使用像

这样的东西
 read_size = recv(client_sock ,&dp ,sizeof(dp), 0);