Socket和SuperSocket入门

目录

一、Socket

1、客户端

    /// <summary>
    /// 发起socket请求
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("启动一个Socket客户端链接");

                int port = 2021;
                string host = "127.0.0.1";//服务器端ip地址

                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);

                Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(ipe);

                while (true)
                {
                    Console.WriteLine("请输入发送到服务器的信息:");
                    string sendStr = Console.ReadLine();
                    if (sendStr == "exit")
                        break;

                    byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
                    clientSocket.Send(sendBytes);

                    //receive message
                    string recStr = "";
                    byte[] recBytes = new byte[4096];
                    int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
                    recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
                    Console.WriteLine($"服务器返回:{recStr}");
                }
                clientSocket.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
View Code

2、服务端

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("已经开启Socket服务端链接");
                int port = 2021;
                string host = "127.0.0.1";

                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);

                Socket sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sSocket.Bind(ipe);
                sSocket.Listen(0);
                Console.WriteLine("监听已经打开,请等待");

                //收到消息  接受一个socket链接
                Socket serverSocket = sSocket.Accept();
                Console.WriteLine("连接已经建立。。。");
                while (true)
                {
                    string recStr = "";
                    byte[] recByte = new byte[4096];
                    int bytes = serverSocket.Receive(recByte, recByte.Length, 0);
                    recStr += Encoding.ASCII.GetString(recByte, 0, bytes);
                    Console.WriteLine("服务器端获得信息:{0}", recStr);

                    if (recStr.Equals("stop"))
                    {
                        serverSocket.Close();//关闭该socket对象
                        Console.WriteLine("关闭链接。。。。");
                        break;
                    }

                    //回发消息
                    Console.WriteLine("请输入回发消息。。。。");
                    string sendStr = Console.ReadLine(); //"send to client :hello world";
                    byte[] sendByte = Encoding.ASCII.GetBytes(sendStr);
                    serverSocket.Send(sendByte, sendByte.Length, 0);

                }
                sSocket.Close();//关闭server监听
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
View Code

二、SuperSocket

作者:chenze
出处:https://www.cnblogs.com/chenze-Index/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
原文地址:https://www.cnblogs.com/chenze-Index/p/14736117.html