通过套接字发送NULL数据 [英] Sending NULL data over a socket

查看:116
本文介绍了通过套接字发送NULL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过基于TCP / IP的套接字与外部应用程序通信。我已经成功建立了与客户端的连接并收到一些数据。本手册此处说明


接收到此命令后,客户端必须从守护程序读取确认
八位字节。肯定确认是零比特的八位字节。否定确认是任何其他模式的八位字节。


我想发送一个肯定的确认, b $ b我的服务器侦听代码从此处获得

  void WriteData(std :: string content)
{
send(newsockfd,content.c_str(),content.length 0);
}

WriteData(00000000);我的问题是如果我发送这个数据corectly(八位字节零位)?



更新:



我已阅读这篇文章这里
它说明发送只允许发送一个char *数组。所以我不知道如何我可以通过套接字发送一个字节。我知道我可以做这样的事情

  std :: bitset< 8> b1; // [0,0,0,0,0,0,0,0] 

解决方案

尝试

  WriteData(std :: string(\0,1)); 

使用您的函数或甚至:

  const char null_data(0); 
send(newsockfd,& null_data,1,0);

直接发送。






  WriteData(00000000); 

实际上会发送8个八位字节的48 [十进制](假设你的平台是ASCII,所有现代系统。



但是 \0 是字符串文字中用于表示空字符的转义序列这是零八位字节)。



在C ++中有一个混合(混合在一起)的字符和字节的概念,可追溯到C的最早的日子,是漂亮很多烤。


I am currently attempting to communicate with an external application over TCP/IP based socket. I have successfully established a connection with the client and received some data. This manual here states that

After this command is received, the client must read an acknowledgement octet from the daemon. A positive acknowledgement is an octet of zero bits. A negative acknowledgement is an octet of any other pattern.

I would like to send a positive acknowledgment and I am sending it this way My server listening code was obtained from here

void WriteData(std::string content)
{
    send(newsockfd,content.c_str(),content.length(),0);
}

WriteData("00000000");

My question is if I am sending this data corectly (octet of zero bits) ?

Update:

I have read this post here which states that send only allows to send a char* array. So I am not sure how I can send a byte over a socket. I know i could do something like this

std::bitset<8> b1 ;  // [0,0,0,0,0,0,0,0]

but i am not sure how i would send that over a socket.

解决方案

Try

WriteData(std::string("\0",1));

using your function or even:

const char null_data(0);
send(newsockfd,&null_data,1,0);

to send it directly.


WriteData("00000000");

Will actually sends 8 octets of 48 [decimal] (assuming your platform is ASCII which all modern systems are compatible with).

However \0 is the escape sequence used in string literals to represent the null character (that is the zero octet).

There's a conflation (mixing together) in C++ between the notions of characters and bytes that dates back to the earliest days of C and is pretty much baked in.

这篇关于通过套接字发送NULL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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