简体   繁体   English

将代码从C ++迁移到C#

[英]Migrating code from C++ to C#

I am trying to write a C# Version of a Server. 我正在尝试编写服务器的C#版本。

Both the original Client and Server were written in C++. 原始的客户端和服务器都是用C ++编写的。

From my C++ Client, I can connect to the new C# Server. 从我的C ++客户端,我可以连接到新的C#服务器。 But since my C++ Client sends data using pointers, I am getting some trouble to read it properly. 但是由于我的C ++ Client使用指针发送数据,所以在正确读取它方面遇到了一些麻烦。

The C++ Client sends a Char Ptr (char *pData) to my Server. C ++客户端将Char Ptr(char * pData)发送到我的服务器。 This data contains a Header (that informs what kind of data I am sending, ie: Request Login) and a Body (with the actual data, ie: ID and Password). 该数据包含一个Header(用于通知我发送的数据类型,即:请求登录)和一个Body(具有实际数据,即:ID和密码)。

Checking the old server, I can see it reading the data like this: 检查旧服务器,我可以看到它正在读取如下数据:

DWORD *dwpMsgID;
dwpMsgID = (DWORD *)(pData + DEF_INDEX4_MSGID); // define DEF_INDEX4_MSGID  0 because its the header position
switch (*dwpMsgID)
{
   ...
   case MSGID_REQUEST_LOGIN: doSomething(); // define MSGID_REQUEST_LOGIN 0x0DF3074F
   ...
}

After that, it then checks the Login/Password like this: 之后,它会像下面这样检查登录名/密码:

cp = (char *)(pData + DEF_INDEX2_MSGTYPE + 2);

memcpy(cAccountName, cp, 10);
cp += 10;
memcpy(cAccountPass, cp, 10);
cp += 10;

My doubt is: since I am getting a byte[] from C# Sockets, how could I check that Header to see if it matchs with "0x0DF3074F" and then get the AccountName and Password ? 我的疑问是:由于我从C#套接字获取一个byte [],如何检查Header以查看其是否与“ 0x0DF3074F”匹配,然后获取AccountName和Password? I've tryed to convert that byte[] into a String (using Encoding.Unicode.GetString() or Encoding.ASCII.GetString()) but I got no success. 我尝试将byte []转换为字符串(使用Encoding.Unicode.GetString()或Encoding.ASCII.GetString()),但没有成功。

Thanks in advance! 提前致谢! Any help will be welcome at this point! 欢迎您提供任何帮助! Regards 问候

Your problem is not that data is being sent using pointers, but rather that you're sending binary data. 您的问题不是使用指针发送数据,而是发送二进制数据。 Your C# server needs to convert 4 bytes into a number. 您的C#服务器需要将4个字节转换为数字。

You can use BitConverter.ToInt32 for this. 您可以为此使用BitConverter.ToInt32

You already figured out how to process the string part of the payload. 您已经弄清楚了如何处理有效负载的字符串部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM