如何建立一个缓冲区在C读取数据插座 [英] How to create a buffer for reading socket data in C

查看:98
本文介绍了如何建立一个缓冲区在C读取数据插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C / C ++ socket编程,而读(插座,缓冲区,BUFSIZE)的方法。究竟什么是缓冲我知道char和字节是一样的,但确实它不管字节数组有多少个元素中有?是否缓冲区需要能够容纳整个消息,直到空字符?

Using C / C++ socket programming, and the "read(socket, buffer, BUFSIZE)" method. What exactly is the "buffer" I know that char and byte are the same thing, but does it matter how many elements the byte array has in it? Does the buffer need to be able to hold the entire message until the null character?

推荐答案

BUFSIZE应等于字节的缓冲区的大小。当读取缓冲区已满()就会停止阅读。下面是一个例子:

BUFSIZE should be equal to the size of your buffer in bytes. read() will stop reading when the buffer is full. Here is an example:

#define MY_BUFFER_SIZE 1024

char mybuffer[MY_BUFFER_SIZE];
int nBytes = read(sck, mybuffer, MY_BUFFER_SIZE);

这篇关于如何建立一个缓冲区在C读取数据插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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