[代碼]--c#-實現局域網聊天

服務器端:

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

namespace ConsoleApplication1
{
    class Program
    {
        private static byte[] result = new byte[1024];
        private static int myProt = 8885;//端口
        static Socket serverSocket;



        static void Main(string[] args)
        {
            //服務器IP地址
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ;
            serverSocket.Bind(new IPEndPoint(ip, myProt));//綁定IP地址:端口 
            serverSocket.Listen(10);//設定最多10個排隊鏈接請求  
            Console.WriteLine("啓動監聽{0}成功", serverSocket.LocalEndPoint.ToString());

            //經過clientsocket發送數據
            Thread myThread = new Thread(ListenClientConnect);
            myThread.Start();
            Console.ReadLine();

        }

        //監聽客戶端鏈接  
        private static void ListenClientConnect()
        {
            while (true)
            {
                Socket clientsocket = serverSocket.Accept();
                clientsocket.Send(Encoding.ASCII.GetBytes("server say hello"));
                Thread receiveThread = new Thread(ReceiveMessage);
                receiveThread.Start(clientsocket);
            }
        }

        //接收消息
        private static void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                try
                {
                    //經過clientsocket接收數據
                    int num = myClientSocket.Receive(result);
                    Console.WriteLine("李明:{1}", myClientSocket.RemoteEndPoint.ToString(), UTF8Encoding.UTF8.GetString(result, 0, num));

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }
        }
    }
}

客戶端:

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

namespace ConsoleApplication2
{
    class Program
    {
        private static byte[] result = new byte[1024];
        static void Main(string[] args)
        {
            //綁定服務器IP地址
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 8885));
                Console.WriteLine("鏈接服務器成功");

            }
            catch
            {
                Console.WriteLine("鏈接服務器失敗,請按回車鍵退出");
                return;
            }

            //經過clientSocket接受數據
            int num = clientSocket.Receive(result);
            Console.WriteLine("接收服務器消息:{0}", Encoding.ASCII.GetString(result, 0, num));


            //經過 clientSocket 發送數據    

            try
            {
                string userCommand = "";
                while (userCommand != "exit")
                {
                    Thread.Sleep(1000);    //等待1秒鐘    
                    Console.Write("韓梅梅:");
                    var sendMessage = Console.ReadLine();
                    byte[] byteData = UTF8Encoding.UTF8.GetBytes(sendMessage);
                    clientSocket.Send(byteData);
                }

            }
            catch
            {
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
            }
            Console.ReadLine();

        }
    }
}

  關注微信公衆號獲取更多福利服務器

相關文章
相關標籤/搜索