简体   繁体   English

在 C# 中使用 .net UDP 套接字发送一个字节

[英]Sending a byte with .net UDP sockets in C#

So i'm following a tutorial on how to setup a .net UDP Server and Client.所以我正在学习如何设置 .net UDP 服务器和客户端的教程。 So far it's going alright I can send a string to the server just like bellow.到目前为止一切顺利,我可以像下面那样向服务器发送一个字符串。

byte[] send_buffer = Encoding.ASCII.GetBytes("Hello Server :)");
sending_socket.SendTo(send_buffer, sending_end_point);

However I ran into a slight issue.但是我遇到了一个小问题。 I want to send a byte instead of the string but I can't seem to work out how to do it.我想发送一个字节而不是字符串,但我似乎无法弄清楚如何去做。 When I say byte I mean like an enum like bellow.当我说字节时,我的意思是像下面这样的枚举。

((byte)MyEnum.Enum1);

First you need to make sure that the underlying type for the enum you are casting is a byte.首先,您需要确保您正在转换的枚举的基础类型是一个字节。 Otherwise you can run into problems.否则,您可能会遇到问题。

If what you need is a byte array from your enum, use the following:如果您需要的是枚举中的字节数组,请使用以下命令:

byte[] send_buffer = { ((byte)MyEnum.Enum1) };

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

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