簡易C# socket

來源:互聯網
上載者:User

標籤:mil   input   space   obj   ring   close   cep   ESS   gif   

伺服器

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

 

簡易C# socket

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.