• 简易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
  • 相关阅读:
    2020牛客暑期多校训练营(第二场)Interval 网络流平面图转化成最短路
    [P4001 [ICPC-Beijing 2006]狼抓兔子]
    [2020牛客暑期多校训练营(第二场)Greater and Greater]
    [2020牛客暑期多校训练营(第二场)All with Pairs]
    2020牛客暑期多校训练营(第二场)[ Boundary]
    数据结构的特性
    centos7 pgsql启动异常问题
    go实现服务授权
    go 使用线程池做请求限流
    go实现爬虫
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9209227.html
Copyright © 2020-2023  润新知