欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# Socket通信

程序员文章站 2022-07-07 15:11:56
...

C# Socket通信

PS:也是在网上查找的各位大神的代码进行理解,如有侵犯,请通知删除

效果:
C# Socket通信

服务端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace ServerCtrl
{
    class Program
    {
        public void ServerStart()
        {
            Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5200);
            Server.Bind(EndPoint);
            Server.Listen(2);
            Console.WriteLine("服务器开启");
            while (true)
            {
                Socket AcceptServer = Server.Accept();
                Console.WriteLine("接收到客户端连接请求");

                Thread RevMsg = new Thread(ServerMsg);
                RevMsg.IsBackground = true;
                RevMsg.Start(AcceptServer);
            }

        }
        public void ServerMsg(object AcceptServer)
        {
            Socket ServerMessage = AcceptServer as Socket;
            byte[] ServerRecBuf = new byte[1024];


            while (true)
            {
                ServerMessage.Send(System.Text.Encoding.Default.GetBytes("服务端发送:我是服务端"));
                ServerMessage.Receive(ServerRecBuf);
                if (ServerRecBuf.Length > 1)
                {
                    Console.WriteLine("服务端接收:" + System.Text.Encoding.Default.GetString(ServerRecBuf));
                }
                Thread.Sleep(5000);
            }
        }

        static void Main(string[] args)
        {
            Program subject = new Program();
            subject.ServerStart();
        }
    }
}

客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace ClientCtrl
{
    class Program
    {
        public void ClientStart()
        {
            Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5200);
            byte[] ClientRecBuf = new byte[1024];
            Client.Connect(EndPoint);
            Console.WriteLine("连接服务端");
            while (true)
            {
                Client.Send(System.Text.Encoding.Default.GetBytes("客户端发送:我是客户端"));
                Client.Receive(ClientRecBuf);
                if (ClientRecBuf.Length > 1)
                {
                    Console.WriteLine("客户端接收:" + System.Text.Encoding.Default.GetString(ClientRecBuf));
                }
                Thread.Sleep(5000);
            }
            Client.Shutdown(SocketShutdown.Both);
            Client.Close();

        }
        static void Main(string[] args)
        {
            Program subject = new Program();
            subject.ClientStart();
        }
    }
}