zoukankan      html  css  js  c++  java
  • 简易C# socket

    服务器

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    
    namespace MyServer
    {
        class Socket_Server
        {
            public int port;
            public IPAddress ip;
    
            private static Socket s_socket;
            private static byte[] result = new byte[1024];
    
            public void Init(string address, int port)
            {
                this.port = port;
                ip = IPAddress.Parse(address);
            }
    
            public void Connection()
            {
                s_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                s_socket.Bind(new IPEndPoint(ip, port));
                s_socket.Listen(20);
    
                Thread st = new Thread(Listener);
                st.Start();
            }
    
            private void Listener()
            {
                while (true)
                {
                    Socket c_socket = s_socket.Accept();
                    c_socket.Send(Encoding.UTF32.GetBytes("连接服务器成功!"));
                    Thread ct = new Thread(Receive);
                    ct.Start(c_socket);
                }
            }
    
            private void Receive(object socket)
            {
                Socket c_socket = (Socket)socket;
                while (true)
                {
                    try
                    {
                        int num = c_socket.Receive(result);
                        string info = Encoding.UTF32.GetString(result,0, num);
                        Console.WriteLine(info);
                        c_socket.Send(Encoding.UTF32.GetBytes("消息回执"));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Close();
                        break;
                    }
                }
            }
    
            public void Close()
            {
                s_socket.Shutdown(SocketShutdown.Both);
                s_socket.Close();
            }
        }
    }
    View Code

    服务器-控制台

    using System;
    
    namespace MyServer
    {
        class Program
        {
            public static string inputValue;
    
            static void Main(string[] args)
            {
                Socket_Server server = new Socket_Server();
                server.Init("127.0.0.1", 88);
                server.Connection();
    
                while (inputValue != "Exit")
                {
                    inputValue = Console.ReadLine();
                    if (inputValue == "Close")
                    {
                        server.Close();
                    }
                }
            }
        }
    }
    View Code

    客户端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Client
    {
        class Socket_Client
        {
            public int port;
            public IPAddress ip;
    
            private static Socket c_socket;
            private static byte[] result = new byte[1024];
    
            public void Init(string address, int port)
            {
                this.port = port;
                ip = IPAddress.Parse(address);
            }
    
            public void Connection()
            {
                c_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    c_socket.Connect(new IPEndPoint(ip, port));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                ReceiveMessage();
            }
    
            public void ReceiveMessage()
            {
                int len = c_socket.Receive(result, 0, 1024, SocketFlags.None);
                string message = Encoding.UTF32.GetString(result, 0, len);
                Console.WriteLine(message);
            }
    
            public void SendMessage(string message)
            {
                byte[] buff = Encoding.UTF32.GetBytes(message);
                c_socket.Send(buff);
                ReceiveMessage();
            }
    
            public void Close()
            {
                c_socket.Close();
            }
        }
    }
    View Code

    客户端-控制台

    using System;
    
    namespace Client
    {
        class Program
        {
            public static string inputValue;
    
            static void Main(string[] args)
            {
                Socket_Client client = new Socket_Client();
                client.Init("127.0.0.1", 88);
                client.Connection();
    
                while (inputValue != "Exit")
                {
                    inputValue = Console.ReadLine();
                    client.SendMessage(inputValue);
                    if (inputValue == "Close")
                    {
                        client.Close();
                    }
                }
            }
        }
    }
    View Code
  • 相关阅读:
    经典的阿里前端笔试题
    Javascript之浏览器兼容EventUtil
    Javascript之对象的创建
    奇妙的CSS之CSS3新特性总结
    前端优化之无阻塞加载脚本
    正则表达式规则与常见的正则表达式
    全端工程师
    最全前端面试问题及答案总结--《转载》
    奇妙的CSS之布局与定位
    关于在django框架里取已登录用户名字的问题
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9209227.html
Copyright © 2011-2022 走看看