zoukankan      html  css  js  c++  java
  • 使用C#中的Socket进行通讯

    class Program
        {
            static void Main(string[] args)
            {
                Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
                serverSocket.Bind(new IPEndPoint(IPAddress.Parse("10.6.0.38"), 88));
    
                serverSocket.Listen(19);
    
                serverSocket.BeginAccept(ClientAccept, serverSocket);
                Console.ReadLine();
    
            }
            static byte[] buffer = new byte[1024];
            static void ClientAccept(IAsyncResult ar)
            {
    
    
                Socket serverSocket = ar.AsyncState as Socket;
    
                Socket clientSocket = serverSocket.EndAccept(ar);
    
                clientSocket.Send(Encoding.UTF8.GetBytes("hellooo"));
    
                clientSocket.BeginReceive(buffer, 0, 1024, SocketFlags.None, ServerReceive, clientSocket);
    
                serverSocket.BeginAccept(ClientAccept, serverSocket);
    
            }
    
            static void ServerReceive(IAsyncResult ar)
            {
                Socket clientSOcket = ar.AsyncState as Socket;
    
                try
                {
                    int len = clientSOcket.EndReceive(ar);
                    if(len == 0)
                    {
                        clientSOcket.Close();
                        return;
                    }
                    Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, len));
    
                    clientSOcket.BeginReceive(buffer, 0, 1024, SocketFlags.None, ServerReceive, clientSOcket);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    clientSOcket.Close();
                }
    
    
                Console.ReadLine();
            }
        }

    客户端

    class Program
        {
            static void Main(string[] args)
            {
                byte[] buffer = new byte[1024];
    
                Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
                clientSocket.Connect(new IPEndPoint(IPAddress.Parse("10.6.0.38"), 88));
    
                int len = clientSocket.Receive(buffer, 0, 1024, SocketFlags.None);
    
                Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, len));
    
                while (true)
                {
                    string temp = Console.ReadLine();
    
                    if (temp == "c")
                    {
                        clientSocket.Close();
                        return;
                    }
    
                    byte[] tempByte = Encoding.UTF8.GetBytes(temp);
    
                    clientSocket.Send(tempByte);
                }
    
            }
        }
    本博客所有内容均为原创,转载请注明出处.
  • 相关阅读:
    第13章 TCP/IP和网络编程
    实验二测试
    实验四 Web服务器1socket编程
    thread同步测试
    团队作业(五):冲刺总结——第四天
    111
    递归和数学归纳法
    Nodejs中cluster模块的多进程共享数据问题
    JavaScript写类方式(一)——工厂方式
    JavaScript中的shift()和pop()函数
  • 原文地址:https://www.cnblogs.com/what-lee/p/8722934.html
Copyright © 2011-2022 走看看