.NET 相当于recv?

发布于 2024-09-03 00:36:11 字数 372 浏览 5 评论 0原文

我有一部分 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-09-10 00:36:11

Socket 发送和接收 C#

Socket.Receive method

Receive 方法从绑定的 Socket 接收数据到缓冲区。方法
返回接收到的字节数。如果套接字缓冲区为空
发生WouldBlock错误。您应该尝试接收
稍后数据。

以下方法尝试将 size 字节接收到缓冲区中
偏移位置。如果操作持续超过超时时间
毫秒它抛出异常。

public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
  int startTickCount = Environment.TickCount;
  int received = 0;  // how many bytes is already received
  do {
    if (Environment.TickCount > startTickCount + timeout)
      throw new Exception("Timeout.");
    try {
      received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
    }
    catch (SocketException ex)
    {
      if (ex.SocketErrorCode == SocketError.WouldBlock ||
          ex.SocketErrorCode == SocketError.IOPending ||
          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
      {
        // socket buffer is probably empty, wait and try again
        Thread.Sleep(30);
      }
      else
        throw ex;  // any serious error occurr
    }
  } while (received < size);
}

Call the Receive method using code such this:
[C#]

Socket socket = tcpClient.Client;
byte[] buffer = new byte[12];  // length of the text "Hello world!"
try
{ // receive data with timeout 10s
  SocketEx.Receive(socket, buffer, 0, buffer.Length, 10000);
  string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
}
catch (Exception ex) { /* ... */ }

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.

public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
  int startTickCount = Environment.TickCount;
  int received = 0;  // how many bytes is already received
  do {
    if (Environment.TickCount > startTickCount + timeout)
      throw new Exception("Timeout.");
    try {
      received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
    }
    catch (SocketException ex)
    {
      if (ex.SocketErrorCode == SocketError.WouldBlock ||
          ex.SocketErrorCode == SocketError.IOPending ||
          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
      {
        // socket buffer is probably empty, wait and try again
        Thread.Sleep(30);
      }
      else
        throw ex;  // any serious error occurr
    }
  } while (received < size);
}

Call the Receive method using code such this:
[C#]

Socket socket = tcpClient.Client;
byte[] buffer = new byte[12];  // length of the text "Hello world!"
try
{ // receive data with timeout 10s
  SocketEx.Receive(socket, buffer, 0, buffer.Length, 10000);
  string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
}
catch (Exception ex) { /* ... */ }
囍笑 2024-09-10 00:36:11

C# 区分字节数组和 Unicode 字符串。 byte 是无符号的 8 位整数,而 char 是 Unicode 字符。它们不可互换。

recv 的等效项是 套接字.接收。您以托管字节数组的形式分配内存,并将其传递给 Receive 方法,该方法将用接收到的字节填充该数组。不涉及指针(只是对象引用)。

Socket mysocket = // ...;

byte[] commandBuffer = new byte[8];
socket.Receive(commandBuffer);

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).

Socket mysocket = // ...;

byte[] commandBuffer = new byte[8];
socket.Receive(commandBuffer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文