zoukankan      html  css  js  c++  java
  • [代码]--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();
    
            }
        }
    }

      关注微信公众号获取更多福利

  • 相关阅读:
    BNUOJ 12756 Social Holidaying(二分匹配)
    HDU 1114 Piggy-Bank(完全背包)
    HDU 2844 Coins (多重背包)
    HDU 2602 Bone Collector(01背包)
    HDU 1171 Big Event in HDU(01背包)
    HDU 2571 命运 (入门dp)
    HDU 1069 Monkey and Banana(最长递减子序列)
    HDU 1160 FatMouse's Speed (最长上升子序列)
    HDU 2594 KMP
    POJ 3783 Balls --扔鸡蛋问题 经典DP
  • 原文地址:https://www.cnblogs.com/girliswater/p/9699831.html
Copyright © 2011-2022 走看看