C# Socket模拟发送接收

Socket简介

通过TCP/IP与仪器或设备通讯,在C#语言中,我们通常采用Socket。本项目是一个简单的Socket建立服务监听与Socket作为客户端请求的一个示例。

项目结构

  • 客户端项目 SocketClient

    主要负责作为Socket客户端发起连接请求,并发送数据

  • 服务端项目 SocketDemo

    主要负责作为Socket服务端,监听端口并接收连接请求,并返回应答数据

项目演示

  • 先运行SocketDemo进行服务监听
            Console.WriteLine("Starting:Creating Socket object");
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
            listener.Listen(10);

            while (true)
            {
                Console.WriteLine("Waiting for connection on port 2112");
                Socket socket = listener.Accept();
                string receivedValue = string.Empty;

                while (true)
                {
                    byte[] receivedBytes = new byte[1024];
                    int numBytes = socket.Receive(receivedBytes);
                    Console.WriteLine("Receiving.");
                    receivedValue += Encoding.ASCII.GetString(receivedBytes, 0, numBytes);
                    if (receivedValue.IndexOf("[FINAL]") > -1)
                    {
                        break;
                    }
                }

                Console.WriteLine("Received value:{0}", receivedValue);
                string replyValue = "Message successfully received.";
                byte[] replyMessage = Encoding.ASCII.GetBytes(replyValue);
                socket.Send(replyMessage);
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
  • 运行SocketClient进行模拟连接,并发送接收数据。
            byte[] receivedBytes = new byte[1024];
            IPHostEntry ipHost = Dns.Resolve("127.0.0.1");
            IPAddress ipAddress = ipHost.AddressList[0];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 2112);
            Console.WriteLine("Starting:Creating Socket object");

            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sender.Connect(ipEndPoint);
            Console.WriteLine("Successfully connected to {0}", sender.RemoteEndPoint);

            string sendingMessage = "Hello World Socket Test";
            Console.WriteLine("Creating Message;Hello World Socket Test");

            byte[] forwardMessage = Encoding.ASCII.GetBytes(sendingMessage + "[FINAL]");


            sender.Send(forwardMessage);

            int totalBytesReceived = sender.Receive(receivedBytes);
            Console.WriteLine("Message provided from server: {0}", Encoding.ASCII.GetString(receivedBytes, 0, totalBytesReceived));

            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
            Thread.Sleep(1000);

            Console.ReadLine();

后记

H5支持WebSocket,预计将来在通讯领域应用会更加广泛,示例程序见博客:[C# WebSocket模拟发送接收](http://www.cnblogs.com/bmbh/p/5174884.html)

GitHub

BMBH/.NET-Demo

posted @   北冥冰皇  阅读(386)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示