C# 网络通信大小端转换类

using System;

namespace Framework.NetPackage.Common
{
    /// <summary>
    /// 字节序转换
    /// </summary>
    public static class Endian
    {
        public static short SwapInt16(this short n)
        {
            return (short)(((n & 0xff) << 8) | ((n >> 8) & 0xff));
        }

        public static ushort SwapUInt16(this ushort n)
        {
            return (ushort)(((n & 0xff) << 8) | ((n >> 8) & 0xff));
        }

        public static int SwapInt32(this int n)
        {
            return (int)(((SwapInt16((short)n) & 0xffff) << 0x10) |
                          (SwapInt16((short)(n >> 0x10)) & 0xffff));
        }

        public static uint SwapUInt32(this uint n)
        {
            return (uint)(((SwapUInt16((ushort)n) & 0xffff) << 0x10) |
                           (SwapUInt16((ushort)(n >> 0x10)) & 0xffff));
        }

        public static long SwapInt64(this long n)
        {
            return (long)(((SwapInt32((int)n) & 0xffffffffL) << 0x20) |
                           (SwapInt32((int)(n >> 0x20)) & 0xffffffffL));
        }

        public static ulong SwapUInt64(this ulong n)
        {
            return (ulong)(((SwapUInt32((uint)n) & 0xffffffffL) << 0x20) |
                            (SwapUInt32((uint)(n >> 0x20)) & 0xffffffffL));
        }
    }
}

C# 与 JAVA  / Erlang / AS3 通信 需要转化大小端,  与C++通信不需要。

转自 : http://www.cnblogs.com/endure/p/3425140.html

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C知道:很高兴回答您关于C#中AsyncTcpServer封装的问题。在C#中,您可以使用以下代码示例来创建一个基于异步TCP通信的服务器端封装: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; public class AsyncTcpServer { private TcpListener listener; public async Task StartServerAsync(int port) { IPAddress ipAddress = IPAddress.Any; listener = new TcpListener(ipAddress, port); listener.Start(); Console.WriteLine("Server started. Listening on port {0}...", port); while (true) { TcpClient client = await listener.AcceptTcpClientAsync(); HandleClientAsync(client); } } private async Task HandleClientAsync(TcpClient client) { Console.WriteLine("Client connected: {0}", client.Client.RemoteEndPoint); byte[] buffer = new byte[1024]; NetworkStream stream = client.GetStream(); while (true) { int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); if (bytesRead == 0) break; string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received from {0}: {1}", client.Client.RemoteEndPoint, dataReceived); // 在此处进行自定义逻辑处理 byte[] responseBuffer = Encoding.ASCII.GetBytes("Server response"); await stream.WriteAsync(responseBuffer, 0, responseBuffer.Length); } Console.WriteLine("Client disconnected: {0}", client.Client.RemoteEndPoint); client.Close(); } public void StopServer() { if (listener != null) { listener.Stop(); Console.WriteLine("Server stopped."); } } } ``` 您可以通过调用`StartServerAsync`方法来启动服务器,并通过传递所需的端口号作为参数。在`HandleClientAsync`方法中,您可以自定义处理接收到的数据的逻辑。通过调用`StopServer`方法,您可以停止服务器。 请记得在使用此封装时添加必要的异常处理和其他逻辑以满足您的需求。希望对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值