.NET 相当于recv?
我有一部分 C 代码,我正在尝试将其移植到 C#。
在我的 C 代码中,我创建一个套接字,然后发出接收命令。接收命令
void receive(mysocket, char * command_buffer)
{
recv(mysocket, command_buffer, COMMAND_BUFFER_SIZE, 0);
}
现在,命令缓冲区返回新值,其中包括作为指向字符串的指针的command_buffer[8]
。
我真的很困惑如何在 .NET 中执行此操作,因为 .NET Read() 方法专门接受字节而不是字符。重要的部分是我得到了指向字符串的指针。
有什么想法吗?
I have a portion of C code that I am trying to port over to C#.
In my C code, I create a socket and then issue a receive command. The receive command is
void receive(mysocket, char * command_buffer)
{
recv(mysocket, command_buffer, COMMAND_BUFFER_SIZE, 0);
}
Now, the command buffer is returned with new values including command_buffer[8]
being a pointer to a string.
I'm really confused as to how to do this in .NET because the .NET Read() method specifically takes in bytes and not char. The important part is that I get the pointer to the string.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Socket 发送和接收 C#
Socket.Receive method
Receive 方法从绑定的 Socket 接收数据到缓冲区。方法
返回接收到的字节数。如果套接字缓冲区为空
发生WouldBlock错误。您应该尝试接收
稍后数据。
以下方法尝试将 size 字节接收到缓冲区中
偏移位置。如果操作持续超过超时时间
毫秒它抛出异常。
Socket Send and Receive C#
Socket.Receive method
Receive method receives data from a bound Socket to your buffer. The method
returns number of received bytes. If the socket buffer is empty a
WouldBlock error occurs. You should try to receive the
data later.
Following method tries to receive size bytes into the buffer to
the offset position. If the operation lasts more than timeout
milliseconds it throws an exception.
C# 区分字节数组和 Unicode 字符串。 byte 是无符号的 8 位整数,而 char 是 Unicode 字符。它们不可互换。
recv
的等效项是 套接字.接收。您以托管字节数组的形式分配内存,并将其传递给 Receive 方法,该方法将用接收到的字节填充该数组。不涉及指针(只是对象引用)。C# makes a distinction between byte arrays and Unicode strings. A byte is an unsigned 8-bit integer, while a char is a Unicode character. They are not interchangeable.
The equivalent of
recv
is Socket.Receive. You allocate memory in form of a managed byte array and pass it to the Receive method which will fill the array with the received bytes. There are no pointers involved (just object references).