Topic: socket 怎么发结构体?

  Print this page

1.socket 怎么发结构体? Copy to clipboard
Posted by: zming
Posted on: 2005-01-10 20:36

原本的c socket客户端在向socket server发送数据包时,数据包的类型是一个c 语言中的嵌套结构体类型,例如:
typedef struct person
{
long personid
unsigned long age;
moneycard card;
char otherinfo[20];
}aperson;

typedef struct moneycard
{
char cardid[10];
long money;
}

在c程序的socket client中向server端传送的数据包的类型是person类型,即send(int sockfd, const void *msg, int len, int flags); 中的msg是一个person类型的变量。现在用java实现这个socket client,请教如果做到?(我的部分程序如下,好像有错误)
--------------------------------------------------
........//省略
DataInputStream in=new DataInputStream (socket.getInputStream ());
DataOutputStream out=new DataOutputStream (socket.getOutputStream ());
int personid = 1001;
int age = 15;
String carid ="123456";
int money = 500;
String otherInfo = "hello";
out.writeInt(personid);
out.writeInt(age);
out.writeChars(carid);
out.writeInt(money);
out.writeChars(otherInfo);
..........//省略
----------------------------------------------------
请问:在c语言中struct类型的数据在内存中是如何存储的?如果在struct A 中嵌套struct B,是否是将B中的数据按顺序放入A中相应位置?即如果按上面的两个struct例子,在内存中的存放格式是否为:
personid(long)--age(unsigned long)--carid(char[])--money(long)--otherInfo(char[])

2.Re:socket 怎么发结构体? [Re: zming] Copy to clipboard
Posted by: hitaco
Posted on: 2005-01-11 22:48

没仔细看你的requirement,但我猜你最好refactor struct to Java class, 然后ObjectOutputStream.writeObject()

3.Re:socket 怎么发结构体? [Re: zming] Copy to clipboard
Posted by: kingtrue
Posted on: 2005-01-25 13:25

怎么感觉像RMI或EJB里的内容。在服务器和客户端都定义两个结构体对象,然后定定义两个接口。

4.Re:socket 怎么发结构体? [Re: zming] Copy to clipboard
Posted by: bromon
Posted on: 2005-01-26 10:51

你的问题我实在不想多看,在oo世界不应该还有结构体这个概念。任何可序列化的对象都可以转化成byte流通过网络传递

5.Re:socket 怎么发结构体? [Re: zming] Copy to clipboard
Posted by: zming
Posted on: 2005-04-02 20:28

你这个想法也想过,可以java通过序列化后对象流,并不是通用的。如果让delphi或c来接受,是没法重现转化一个对象的。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923