码迷,mamicode.com
首页 > Windows程序 > 详细

简易C# socket

时间:2018-06-21 17:21:16      阅读:207      评论:0      收藏:0      [点我收藏+]

标签: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

标签:mil   input   space   obj   ring   close   cep   ESS   gif   

原文地址:https://www.cnblogs.com/Joke-crazy/p/9209227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!