• 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

  • 相关阅读:
    #2051:Bitset(进制转化)
    #2054:A == B ?(水题坑人)
    #2045:不容易系列之三LELE的RPG难题(dp递推)
    #2037:今年暑假不AC
    #2036:改革春风吹满地
    OJ中的语言选项里G++ 与 C++的区别
    如何在CSDN上如何快速转载博客
    Python之路(第八篇)Python内置函数、zip()、max()、min()
    Python编程笔记(第一篇)Python基础语法
    Python之路(第七篇)Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
  • 原文地址:https://www.cnblogs.com/bmbh/p/9023458.html
Copyright © 2020-2023  润新知