c - 管理通信协议本地套接字 c
问题描述
我正在尝试使用 C 中的 AF_UNIX 套接字实现我的第一个客户端-服务器应用程序。
对于从客户端发送的查询,我必须遵守特定的通信协议。可以使用三种类型的查询:
1) <command_name> <object_name> <obj_len> \n <obj_data>
2) <command_name> <object_name> \n
3) <command_name> \n
请注意,除了第一个查询之外,每个查询都以 \n 特殊字符结尾,并且每条消息的长度是 unknown 。
我阅读 2) 和 3) 没有问题,因为我知道消息将以 \n 结尾,所以我只需要检查缓冲区中读取的最后一个字节是否为 \n,如果不是,我将继续从套接字读取并将后续调用附加到缓冲区。
而对于第一个操作,上述方法不起作用,因为 \n 字符不在消息的末尾,而是在缓冲区的任意点。我要做的是读取(在这种情况下)直到我到达 \n ,然后从缓冲区解析obj_len令牌并从包含 obj_data 全部内容的套接字中最后读取obj_len字节。
这是我的 readLine 函数的代码,它只适用于 1) 和 2) 类型的消息:
char * readLine(int fd){
char * buf=NULL; //here i store the content
char * tmp=calloc(CHUNK,sizeof(char));
if(!tmp)
return NULL;
if(!buf)
return NULL;
buf[0]='\0';
size_t byte_read=-1;
int len=0;
do{
bzero(tmp,CHUNK); //reset tmp for read
byte_read=read(fd,(void *)tmp,CHUNK);
if(byte_read==-1){
perror("read");
if(tmp)free(tmp);
if(buf)free(buf);
return NULL;
}
len=len+byte_read; //update the len of message
buf=realloc(buf,len+1);
if(!buf){
perror("realloc");
if(tmp)free(tmp);
return NULL;
}
buf=strncat(buf,tmp,byte_read);//append each call of read
if(byte_read>0 && buf[byte_read-1]=='\n')
//the last byte read is the special character
break;
}
while(byte_read!=0);
if(byte_read==0)
//read error SIGPIPE
if(tmp)free(tmp);
return buf;
}
您认为我应该怎么做才能实现从套接字读取格式为 2)、3) 和 1) 的消息的功能?
解决方案
我建议采用不同的方法。首先读取一行,然后确定它是什么类型的消息,然后才决定这是结束还是您需要读取更多字节。
所以让我们从读一行开始:
int socket_readline(int fd, char *buf, size_t *inout_size)
{
char c = 0;
size_t capacity = *inout_size;
*inout_size = 0;
while(c != '\n') {
int rc = read(fd, &c, 1);
if (rc != 1) return -1;
buf[*inout_size] = c;
*inout_size += 1;
if (capacity - *inout_size == 0) return -1;
}
buf[*inout_size] = '\0';
return 0;
}
此例程0
在成功时返回,-1
否则返回。
在成功的情况下,缓冲区是NUL
终止的,inout_size
它将是缓冲区中字符串的长度(不包括NUL
终止字节)。如果出现错误,您应该检查inout_size
值。如果发生溢出(行太长),它将等于给定缓冲区的大小(输入缓冲区不会被NUL
终止!),对于读取错误,它将包含在错误发生之前读取了多少字节。
现在,一旦我们有了一个读取衬里例程,我们就可以使用它来解析传入的行:
int rc;
size_t size;
char buf[MAX_INPUT_LENGTH];
int object_len;
char command_name[MAX_INPUT_LENGTH], object_name[MAX_INPUT_LENGTH];
do {
size = sizeof(buf);
rc = socket_readline(fd, buf, &size);
if (rc == 0) {
int tokens_matched = sscanf(buf, "%s %s %d \n", command_name, object_name, &object_len);
switch(tokens_matched) {
case 1:
printf("This is a command: %s\n", command_name);
break;
case 2:
printf("This is a command: %s with object name: %s\n", command_name, object_name);
break;
case 3:
printf("This is a command: %s with object name: %s of length %d\n", command_name, object_name, object_len);
printf("TODO read up remaining: %d bytes\n", object_len);
break;
default:
printf("Error unable to match command format\n");
break;
}
} else if (size == sizeof(buf)) {
printf("Overflow! Bytes read: %lu. \n", size);
// TODO How to handle overflows?
} else {
printf("Read error must have occured. Read %lu bytes.\n", size);
}
} while (rc == 0 || size == sizeof(buf));
推荐阅读
- php - PHP:在 cURL GET 请求之后立即发出 cURL POST 请求
- javascript - Redux 期望监听器起作用
- c# - 如何调整 Cheet.NET 以绑定到我的 C# 表单?
- sql - BigQuery:检索两列中唯一的行,否则检索第三列最大的行
- sql - 筛选日期范围内退货的项目列表
- windows - Ansible 文件检查多个服务器和文件
- php - 获取并显示 WordPress 上帖子的类别选项
- android - 将反应原生版本更新到 0.59.4 后,未定义不是评估 (_reactnative.Modal.propTypes.animationType) 的对象
- ruby - 如何根据长度将路径数组转换为嵌套数组或散列
- python - 如何显示来自不同模型的对象?