怎么办套接字工作用C? [英] How Do Sockets Work in C?

查看:128
本文介绍了怎么办套接字工作用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑用C套接字编程。

I am a bit confused about socket programming in C.

您创建一个套接字,将其绑定到一个接口和一个IP地址,并把它听。我发现一对夫妇在该网页资源,并理解它的罚款。特别是,我发现了一篇文章 网络编程在Unix系统 是非常丰富的。

You create a socket, bind it to an interface and an IP address and get it to listen. I found a couple of web resources on that, and understood it fine. In particular, I found an article Network programming under Unix systems to be very informative.

什么混淆我是到达插座上的数据的时间。

What confuses me is the timing of data arriving on the socket.

您如何判断数据包到达,而数据包有多大,你必须尽一切繁重的自己呢?

How can you tell when packets arrive, and how big the packet is, do you have to do all the heavy lifting yourself?

我在这里的基本假设是,数据包可以是可变长度的,所以一旦二进制数据开始出现下降的插座,你如何开始从构造的数据包?

My basic assumption here is that packets can be of variable length, so once binary data starts appearing down the socket, how do you begin to construct packets from that?

推荐答案

简短的回答是,你必须做所有繁重自己。可以通知您有可用的读取数据,但你不会知道有多少字节可用。在使用可变长度的数据包最IP协议,将有与ppended到分组的已知固定长度$ P $标题。这个报头将包含分组的长度。你读头,将数据包的长度,然后读取数据包。重复此模式(读头,然后读取数据包),直到通讯完成。

Short answer is that you have to do all the heavy lifting yourself. You can be notified that there is data available to be read, but you won't know how many bytes are available. In most IP protocols that use variable length packets, there will be a header with a known fixed length prepended to the packet. This header will contain the length of the packet. You read the header, get the length of the packet, then read the packet. You repeat this pattern (read header, then read packet) until communication is complete.

当从套接字读取数据时,将请求一定的字节数。读出的调用可能阻塞直到字节的请求数量是阅读,但它可以比被要求什么回报较少的字节。发生这种情况时,只需重新尝试读取,请求剩余的字节。

When reading data from a socket, you request a certain number of bytes. The read call may block until the requested number of bytes are read, but it can return fewer bytes than what was requested. When this happens, you simply retry the read, requesting the remaining bytes.

下面是一个典型的C函数用于从套接字读取的字节集数:

Here's a typical C function for reading a set number of bytes from a socket:

/* buffer points to memory block that is bigger than the number of bytes to be read */
/* socket is open socket that is connected to a sender */
/* bytesToRead is the number of bytes expected from the sender */
/* bytesRead is a pointer to a integer variable that will hold the number of bytes */
/*           actually received from the sender. */
/* The function returns either the number of bytes read, */
/*                             0 if the socket was closed by the sender, and */
/*                            -1 if an error occurred while reading from the socket */
int readBytes(int socket, char *buffer, int bytesToRead, int *bytesRead)
{
    *bytesRead = 0;
    while(*bytesRead < bytesToRead)
    {
        int ret = read(socket, buffer + *bytesRead, bytesToRead - *bytesRead);
        if(ret <= 0)
        {
           /* either connection was closed or an error occurred */
           return ret;
        }
        else
        {
           *bytesRead += ret;
        }
    }
    return *bytesRead;
}

这篇关于怎么办套接字工作用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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