您的当前位置:首页正文

C#简单Socket

2024-11-09 来源:个人技术集锦


服务端:


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

namespace SocketServer
{
    class Program
    {
        private static Socket serverSocket;
        static void Main(string[] args)
        {
            IPAddress ip =IPAddress.Parse("127.0.0.1");
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new  IPEndPoint(ip, 8808));
            serverSocket.Listen(0);
            Console.WriteLine("服务器{0}开启成功", serverSocket.LocalEndPoint.ToString());
            Thread thread = new Thread(ClientSocket);
            thread.Start();
            Console.ReadKey();    
        }

        private static void ClientSocket()
        {
                while (true)
                {
                    Socket clientSocket = serverSocket.Accept();
                    Console.WriteLine("客户端{0}连接成功", clientSocket.LocalEndPoint.ToString());
                    string msg = "hello";
                    byte[] data = Encoding.UTF8.GetBytes(msg);
                    clientSocket.Send(data);
                    Thread receiveThread = new Thread(ReceiveMessage);
                    receiveThread.Start(clientSocket);
                }
        }

        private static void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                try
                {
                    byte[] dataBUffer = new byte[1024];
                    int count = myClientSocket.Receive(dataBUffer);
                    string msgReceive = Encoding.UTF8.GetString(dataBUffer, 0, count);
                    Console.WriteLine("接收客户端消息:" + msgReceive);
                }
                catch (Exception  e)
                {
                    Console.WriteLine(e.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    return;
                } 
            }           
        }
    }
}

客户端:

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

namespace ClientServer
{
    class Program
    {
        private static Socket clientSocket;
        static void Main(string[] args)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 8808));
               
            }
            catch (Exception e)
            {
                Console.WriteLine("客户端连接异常:"+e.Message);
                throw;
            }

            //接收消息
            byte[] data = new byte[1024];
            int count = clientSocket.Receive(data);
            string msg = Encoding.UTF8.GetString(data,0,count);
            Console.WriteLine("从服务器接收消息:"+msg);

            //发送消息
            while (true)
            {
                try
                {
                    string str = Console.ReadLine();
                    clientSocket.Send(Encoding.UTF8.GetBytes(str));
                }
                catch
                {
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                    break;
                }
               
            }
            
            Console.ReadKey();
            clientSocket.Close();
        }
    }
   
}
记录一次自己的学习结果

Top