C 中数据包的读/写处理示例

发布于 2024-07-24 16:13:47 字数 224 浏览 6 评论 0 原文

我对 C 有点陌生,但我已经完成了我的作业(一些教程、书籍等),并且我需要编写一个简单的服务器来处理来自客户端的请求并与数据库交互。 我已经阅读了 Beej 的网络编程指南,但我有点不确定如何拼凑和处理来回发送的数据的不同部分。

例如,假设客户端正在发送一些信息,服务器将把这些信息放入多个字段中。 如何将要发送的数据拼凑在一起,然后在服务器端将其分解?

谢谢,

埃里克

I'm a bit new to C, but I've done my homework (some tutorials, books, etc.) and I need to program a simple server to handle requests from clients and interact with a db. I've gone through Beej's Guide to Network programming, but I'm a bit unsure how to piece together and handle different parts of the data getting sent back and forth.

For instance, say the client is sending some information that the server will put in multiple fields. How do I piece together that data to be sent and then break it back up on the server side?

Thanks,

Eric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎樣才叫好 2024-07-31 16:13:47

如果我理解正确的话,您会问“服务器如何理解客户端发送给它的信息”?

如果这就是您所问的问题,答案很简单:双方提前同意双方使用的数据结构将是兼容的。 即,您提前决定您的通信协议。

因此,例如,如果我有一个客户端-服务器应用程序,客户端连接并且可以请求诸如“时间”、“日期”之类的内容,并且可以说“settime”和“setdate”,那么我需要这样编写我的服务器一种让它理解这些命令的方式。

显然,在上面的情况下这是微不足道的,因为它只是一个基于文本的协议。 但是,假设您正在编写一个将返回信息结构的应用程序,即

struct Person {
    char* name;
    int age;
    int heightInInches;
    // ... other fields ...
};

您可能会从服务器/客户端写出整个结构。 在这种情况下,需要注意以下几点:

  1. 您需要正确地 hton/ntoh
  2. 您需要确保您的客户端和服务器都可以理解有问题的结构。
  3. 您可能需要也可能不需要在 4B 边界上对齐(因为如果不这样做,不同的 C 编译器可能会做不同的事情,这可能会在客户端和服务器之间让您感到烦恼,也可能不会)。

不过,一般来说,在编写客户端/服务器应用程序时,最重要的是要正确使用通信协议。

不过,我不确定这是否完全回答了您的问题。 这就是您所追求的,还是您想更多地了解如何使用发送/接收功能?

If I understand correctly, you're asking, "how does the server understand the information the client sends it"?

If that's what you're asking, the answer is simple: it's mutually agreed upon ahead of time that the data structures each uses will be compatible. I.e. you decide upon what your communication protocol will be ahead of time.

So, for example, if I have a client-server application where the client connects and can ask for things such as "time", "date" and can say "settime " and "setdate ", I need to write my server in such a way that it will understand those commands.

Obviously, in the above case it's trivial, since it'd just be a text-based protocol. But let's say you're writing an application that will return a struct of information, i.e.

struct Person {
    char* name;
    int age;
    int heightInInches;
    // ... other fields ...
};

You might write the entire struct out from the server/client. In this case there are a few things to be aware of:

  1. You need to hton/ntoh properly
  2. You need to make sure that your client and server both can understand the struct in question.
  3. You may or may not have to align on a 4B boundary (because if you don't, different C compilers may do different things, which may burn you between the client and the server, or it may not).

In general, though, when writing a client/server app, the most important thing to get right is the communication protocol.

I'm not sure if this quite answers your question, though. Is this what you were after, or were you asking more about how, exactly, do you use the send/recv functions?

女中豪杰 2024-07-31 16:13:47

首先,您定义数据包的外观 - 数据包中包含哪些信息。 确保定义采用架构中立的格式。 这意味着您指定的顺序不取决于机器是大尾数还是小尾数,也不取决于您是否使用 32 位 long 或 64 位进行编译位值。 如果内容是可变长度的,请确保定义包含告知每个部分有多长所需的信息 - 特别是,每个可变长度部分之前应该有一个适当的长度计数。

当您需要打包数据进行传输时,您将获取原始(特定于机器的)值并将它们以适当的格式写入缓冲区(认为是“字符数组”)中的适当位置。

该缓冲区将通过线路发送到接收器,接收器会将其读入另一个缓冲区,然后反转该过程以将缓冲区中的信息获取到局部变量中。

有诸如 ntohs() 之类的函数,用于将“短”(表示 16 位)整数从网络 ('n') 格式转换为主机 ('h') 格式,以及 htonl() 从主机“长”(32 位整数)转换为网络格式 - 等等。

一本关于网络的好书是 Stevens 的“UNIX 网络编程,第 1 卷,第 3 版”。 您可以在其网站上找到有关它的更多信息,包括示例代码。

First, you define how the packet will look - what information will be in it. Make sure the definition is in an architecture-neutral format. That means that you specify it in a sequence that does not depend on whether the machine is big-endian or little-endian, for example, nor on whether you are compiling with 32-bit long or 64-bit long values. If the content is of variable length, make sure the definition contains the information needed to tell how long each part is - in particular, each variable length part should be preceded by a suitable count of its length.

When you need to package the data for transmission, you will take the raw (machine-specific) values and write them into a buffer (think 'character array') at the appropriate positions, in the appropriate format.

This buffer will be sent across the wire to the receiver, which will read it into another buffer, and then reverse the process to obtain the information from the buffer into local variables.

There are functions such as ntohs() to convert from a network ('n') to host ('h') format for a 'short' (meaning 16-bit) integer, and htonl() to convert from a host 'long' (32-bit integer) to network format - etc.

One good book for networking is Stevens' "UNIX Network Programming, Vol 1, 3rd Edn". You can find out more about it at its web site, including example code.

清欢 2024-07-31 16:13:47

如上所述,您需要的是事先商定的沟通方式。 对我有帮助的一件事是使用 xml 进行通信。

例如,您需要时间将时间发送给客户端,然后将其包含在名为时间的标签中。
然后在客户端解析并读取标签值。

最大的优点是,一旦您在客户端安装了解析器,即使您必须发送一些新信息,它们也只需就将在客户端解析的标签名称达成一致。

它对我有帮助,我希望它也对你有帮助。

As already mentioned above what you need is a previously agreed means of communication. One thing that helps me is to use xmls to communicate.

e.g. You need time to send time to client then include it in a tag called time.
Then parse it on the client side and read the tag value.

The biggest advantage is that once you have a parser in place on client side then even if you have to send some new information them just have to agree on a tag name that will be parsed on the client side.

It helps me , I hope it helps you too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文