套接字如何在 C 中工作? [英] How Do Sockets Work in C?

查看:19
本文介绍了套接字如何在 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 协议中,数据包前面都会有一个已知固定长度的标头.此标头将包含数据包的长度.您读取标头,获取数据包的长度,然后读取数据包.您重复此模式(读取标头,然后读取数据包)直到通信完成.

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.

从套接字读取数据时,您请求一定数量的字节.read 调用可能会阻塞,直到读取请求的字节数,但它可以返回比请求的字节少的字节.发生这种情况时,您只需重试读取,请求剩余的字节.

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天全站免登陆