3

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

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

谢谢,

埃里克

4

3 回答 3

4

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

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

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

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

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

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

  1. 你需要正确地 hton/ntoh
  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?

于 2009-06-19T14:04:13.437 回答
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.

于 2009-06-19T14:05:04.293 回答
0

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.

于 2009-06-19T16:16:44.347 回答