c# - 如何使用udp从C#客户端与C服务器通信?

标签 c# c sockets udp client-server

我的问题描述如下:

我的 C# 客户端使用 sendto 发送 udp 数据包。因为这是 C#,所以这是我构造的字节数组。

C 服务器使用 recvfrom 接收数据包。通过wireshark,我确认C#程序正在发送数据包,并且C程序正在接收数据包。 recvfrom 不会抛出任何类型的错误。但我无法从收到的数据包中提取任何数据。我以字符数组的形式收到它。

具体细节: 我发送的UDP数据包是一个请求。 c 程序和 C# 程序都知道请求的结构。但由于我通过线路发送字节数组,因此该结构是无关紧要的(我认为)。

代码: C#: 构建数据包: ``

            byte[] tmpB;
            int j = 0;
            char[] reqStr = rsField.reqStr;

            for (; j < reqStr.Length; j++)
            {                    
                tmpB = BitConverter.GetBytes((char)reqStr[j]);
                for (int ctr = 0; ctr < tmpB.Length; ctr++)
                    retVal[i++] = tmpB[ctr];

            }//end of for

            tmpB = BitConverter.GetBytes((UInt32)rsField.fieldLength);
            for (int ctr = 0; ctr < tmpB.Length; ctr++)
                retVal[i++] = tmpB[ctr];

            tmpB = BitConverter.GetBytes((UInt32)rsField.fieldType);
            for (int ctr = 0; ctr < tmpB.Length; ctr++)
                retVal[i++] = tmpB[ctr];

            //------------------------------------------------------------


            tmpB = BitConverter.GetBytes((UInt32)ctField.camType);
            for (int ctr = 0; ctr < tmpB.Length; ctr++)
                retVal[i++] = tmpB[ctr];

            tmpB = BitConverter.GetBytes((UInt32)ctField.fieldLength);
            for (int ctr = 0; ctr < tmpB.Length; ctr++)
                retVal[i++] = tmpB[ctr];

            tmpB = BitConverter.GetBytes((UInt32)ctField.fieldType);
            for (int ctr = 0; ctr < tmpB.Length; ctr++)
                retVal[i++] = tmpB[ctr];

... `` 如您所见,我混合了字符串和数字字段。 (这些不是我拥有的唯一字段,代码被缩短以提高可读性)

C: ``

            char buf[1000];
    memset(&buf,'\0',sizeof(buf));
    length = recvfrom (i, &buf, sizeof(buf), 0,
               (struct sockaddr *)&remoteAddr, &fromLen);


    now = time(0);
    if (length == -1) {
        fprintf (stderr, "%s: recvfrom error - %m\n", ctime(&now));
        continue;
    }
    unsigned long fieldType1 = strtoul(bufPtr,0,10);
    bufPtr += 4 * sizeof(char);
    unsigned long fieldLength1 = strtoul(bufPtr,0,10);
    bufPtr += 4 * sizeof(char);
    unsigned long res = strtoul(bufPtr,0,10);

    printf("fieldType: %ld \t fieldLength: %ld \t res: %ld \t \n",fieldType1,fieldLength1,res);

... `` 我确信我错过了整个事情的一个基本方面。请帮我找出它是什么。任何帮助将不胜感激。

谢谢。

最佳答案

一旦这些位被写入线路,它就不是 C、C++、C#、Java、FORTRAN、COBOL、lisp、scheme、scala 或其他任何语言。它是电线上的碎片。这是你所缺少的基本方面。线路上的数据没有结构。

读取数据后,您就可以获取该字节集合并对其应用结构。这意味着您需要知道前四个字节代表一个数字,低位字节存储在距有效负载开头的偏移量 0 处,下一个高位字节位于偏移量 1 处,下一个高位字节位于偏移量处2,最高位字节位于偏移量 3。您的具体示例可能与上面的描述没有任何共同之处,但如何打包成字节和解包出字节的想法是您需要仔细注意的。

验证您是否在最低级别写入所有字节。保留哪些数据位于哪些字节中的映射,然后独立于刚刚编写的规范来编写通信的每一端。不要试图将整个整数/空头/数据结构写入数据包而不将其转换为字节;否则,你会发现两端的机器可能对整数/短整型/数据结构的表示方式有不同的看法。

关于c# - 如何使用udp从C#客户端与C服务器通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6916042/

相关文章:

c# - 在 Excel 加载项中实现 AppSheetChanged 时启用撤消

c# - 为什么要编写符合 CLS 的代码?

c - 在 Linux 上连接硬件安全模块

sockets - 使用 Tokio futures 的多播 UDP 数据包

c# - 数据库函数 "cannot be translated into a LINQ to Entities store expression"

c - 为什么编译器生成 4 字节负载而不是 1 字节负载,而更宽的负载可能会访问未映射的数据?

返回 C.CString 时出现 CGo 段错误

python多线程服务器无法从客户端接收数据

使用公共(public) IP 地址通过套接字进行 Java 连接

c# - AddDbContext 在 .NET Core 的 IServiceCollection 中不可用