从结构体发送数据,套接字编程 [英] Sending data from a struct, socket programming

查看:31
本文介绍了从结构体发送数据,套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学校的作业,其中一部分是使用套接字编程将一组整数、字符 char* 从客户端发送到服务器.发送整数或字符工作得很好,但是有没有办法将整个结构作为一个数据包发送?阅读有关序列化的信息,但我似乎无法使其正常工作.这是一个代码片段:

I have an assignment for school, and one part is to send a set of ints, chars char* from a client to a server using socket programming. Sending ints or chars works just fine, but is there any way to send the entire struct as one packet? Read about serializing, but I cant seem to make it work. Here's a code snippet:

The struct looks like this:
struct Msg
{
        int a;
        char b;
        char *user;
};

Client:
init variables and such...
int netcom(char* ip, int port) {
    sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    // clear the structure
    bzero(&serveraddr, sizeof(struct sockaddr_in));
    serveraddr.sin_family = AF_INET;
    // add the server adress
    inet_pton(AF_INET, ip, &serveraddr.sin_addr);
    // add the port number
    serveraddr.sin_port = htons(port);
    // connect 
    connect (sd,(struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in));
}

int sendPkg(struct Msg msg) {
    send(sd, &msg, sizeof(msg), 0); 
}

服务器接收的部分是这样的:

And the part of the server that receives looks like this:

char buf[100];
recv(sd[i], buf, sizeof(buf)-1, 0);

客户端发送正常,服务器接收正常.但我不知道我要发送什么,以及如何正确阅读.所以这就是我的问题.如何使用套接字从 struct 正确发送数据.

The client sends perfectly fine, and the server receives fine. But I have no idea what I'm sending, and how to read it properly. So that's my question. How to properly send over the data from a struct using sockets.

推荐答案

直接发送结构体似乎很有吸引力,因为您可以在一次调用中完成……更糟糕的是:有时它实际上效果很好!不过这是个陷阱.

Sending a struct directly seems appealing because you can do it in a single call... and worse: sometimes it will actually work out great! It's a trap though.

这是陷阱的第一个原因是,例如,在您的情况下,其中一个元素是指针.除非在极少数情况下,接收方将获得在该端无用的指针——该指针指向对发送进程有效的内存.

The first reason this is a trap is that, in your case for example, one of the elements is a pointer. Except in very rare cases, the receiver will get that pointer which is useless on that end -- the pointer points to memory that is valid for the sending process.

这是一个陷阱的第二个不太明显的原因是套接字一侧的结构布局(在内存中)在另一侧可能不相同.这是机器体系结构和编译器设置的一个功能,使得相信它没问题"是不安全的.这个陷阱在一段时间内很容易摆脱,尤其是在开发过程中,您可能在测试的每一端都有兼容的架构.

The second, and less obvious reason this is a trap is that the layout (in memory) of a structure on one side of a socket might not be identical on the other side. This is a function of machine architectures and compiler settings, making it unsafe to trust that it will "just be ok". This trap is easy to get away with for a while, especially during development, where you're likely to have compatible architectures on each side of your test.

最好的办法是单独发送每个字段,尽管这很痛苦.你可以通过为你的代码应用一点面向对象的设计来减少它的痛苦,通过为这个结构创建一个专用的发送器和接收器函数.这些函数非常了解内容和顺序,并将其打包成较小的发送(对于您的 char *,可能需要在字符串数据前包含一个长度).

Your best bet is to send each field individually, even though that is painful. You can make it less painful by applying a little object oriented design to your code though, by creating a dedicated sender and receiver function for this structure. These functions have intimate knowledge of the contents and order, and package it into smaller sends (which, in the case of your char *, probably need to include a length before the string data).

只要结构没有从套接字的一侧更改到另一侧,我描述的打包发送就可以了,但是您可能要警惕一端具有不同版本的结构.. 说一个新的(或不存在的)字段.为了解决这个问题,您可以考虑标记您发送的数据(而不是假设我们首先发送 a,然后发送 b 等").

A packaged send as I described is fine as long as the structure hasn't changed from one side of the socket to the other, but you might want to be wary of having a different version of the structure on one end... say a new (or not present) field. To handle this, you could consider tagging the data you send (rather than assuming "first we send a, then b, etc.").

查看许多 JSON 库来管理序列化会大有裨益.这为您提供了一种易于阅读的格式,可以解决我在此处提出的所有问题,并且几乎每种常见的编程语言都有可用的 JSON 库.

It would be beneficial to look into the many JSON libraries to manage your serialization. This gives you an easy to read format that addresses all of the issues I've presented here, and there are JSON libraries available for nearly every common programming language.

这篇关于从结构体发送数据,套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆