• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
~Joke_crazy
爱生活,爱拉芳!
   首页    新随笔       管理     

简易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

 

posted @ 2018-06-21 15:17  ~Joke_crazy  阅读(178)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3