zoukankan      html  css  js  c++  java
  • .net网络编程(3)Socket基础

    参考:

    与Socket的第一次“约会”

    与Socket的“再次见面”

    揭开Socket编程的面纱

    “麻烦”的数据缓冲区

    《.NET 4.0网络开发入门之旅》7——填平缓冲区陷阱

    一.服务器端

    class Threadtcpserver
    {
        private Socket server;
        public Threadtcpserver()
        {
            IPAddress local = IPAddress.Parse("127.0.0.1");
            IPEndPoint iep = new IPEndPoint(local, 13000);
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
            // 将套接字与本地终结点绑定    
            server.Bind(iep);
    
            //在本地13000端口号上进行监听    
            server.Listen(20);
            Console.WriteLine("等待客户机进行连接......");
            while (true)
            {
                // 得到包含客户端信息的套接字
                Socket client = server.Accept();
    
                //创建消息服务线程对象
                ClientThread newclient = new ClientThread(client);
                newclient.ClientService();
            }
        }
        static void Main(string[] args)
        {
            Threadtcpserver instance = new Threadtcpserver();
        }
    }
    class ClientThread
    {
        //connections变量表示连接数
        public static int connections = 0;
        public Socket service;
        int i;
        public ClientThread(Socket clientsocket)
        {
            //service对象接管对消息的控制
            this.service = clientsocket;
        }
        public void ClientService()
        {
            String data = null;
            byte[] bytes = new byte[1024];
            if (service != null)
            {
                connections++;
            }
            Console.WriteLine("新客户连接建立:{0} 个连接数", connections);
            while ((i = service.Receive(bytes)) != 0)
            {
                data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
                Console.WriteLine("收到的数据: {0}", data);
    
                // 处理客户端发来的消息,这里是转化为大写字母
                data = data.ToUpper();
                byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
    
                // 发送一条应答消息
                service.Send(msg);
                Console.WriteLine("发送的数据: {0}", data);
            }
            //关闭套接字
            service.Close();
            connections--;
            Console.WriteLine("客户关闭连接:{0} 个连接数", connections);
        }
    }
    

    二.客户端

    static void Main(string[] args)
    {
        Socket client;
        byte[] buf = new byte[1024];
        string input;
        IPAddress local = IPAddress.Parse("127.0.0.1");
        IPEndPoint iep = new IPEndPoint(local, 13000);
        try
        {
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(iep);
        }
        catch (SocketException)
        {
            Console.WriteLine("无法连接到服务器!");
            return;
        }
        while (true)
        {
            //在控制台上输入一条消息
            input = Console.ReadLine();
            //输入exit,可以断开与服务器的连接
            if (input == "exit")
            {
                break;
            }
            client.Send(Encoding.ASCII.GetBytes(input));
            //得到实际收到的字节总数
            int rec = client.Receive(buf);
            Console.WriteLine(Encoding.ASCII.GetString(buf, 0, rec));
        }
        Console.WriteLine("断开与服务器的连接......");
        client.Close();
    }
    

    测试结果:

    image

    换成UTF-8编码,就可以接受和发送中文了

    image

  • 相关阅读:
    python appium环境搭建
    github 删除某个文件
    python 导入的模块使用了相对路径,导致找不到文件错误
    python asyncio协程
    python 获取调用函数的名字和行号
    monkey测试命令
    python 属性查询顺序,数据描述符
    JS各循环的差别
    AngularJS复习小结
    那些不正经的前端笔试题
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1956109.html
Copyright © 2011-2022 走看看