TCP_Socket

// 服务器
        public static void Server()
        {
            // 创建监听对象
            TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 30005);
            // 开始监听
            tcpListener.Start();
            try
            {
                // 开始等待链接
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                Console.WriteLine("有客户端" + tcpClient.Client.RemoteEndPoint + "加入聊天");
                Console.WriteLine("链接成功");
                // 4.获取数据流
                NetworkStream stream = tcpClient.GetStream();
                byte[] receiveData = new byte[1024];
                while (true)
                {
                    // 5.读取数据
                    int length = stream.Read(receiveData,0,receiveData.Length);
                    string msg = Encoding.UTF8.GetString(receiveData,0,length);
                    Console.WriteLine(tcpClient.Client.RemoteEndPoint + "说:" + msg);
                    // 6.服务器向客户端发送数据
                    byte[] senDatad = Encoding.UTF8.GetBytes("服务器收到数据了");
                    stream.Write(senDatad,0,senDatad.Length);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // throw;
            }
            finally
            {
                tcpListener.Stop();
            }
        }


        // 客户端  
        public static void Client()
        {
            // 1.创建客户端对象
            TcpClient client = new TcpClient();
            // 2.链接
            client.Connect(IPAddress.Parse("127.0.0.1"), 30005);
            try
            {
                // 3.获取数据流
                NetworkStream stream = client.GetStream();
                开启分线程 接收服务器的消息
                Thread thread = new Thread(ClientReceiveThread);
                thread.Start(stream);
                while (true)
                {
                    Console.WriteLine("链接成功 请说话:");
                    string input = Console.ReadLine();
                    if (input != string.Empty)
                    {
                        // 4.向服务器发送数据 
                        byte[] sendData = Encoding.UTF8.GetBytes(input);
                        stream.Write(sendData,0,sendData.Length);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // throw;
            }
            finally
            {
                client.Close();
            }
        }
        // 分线程
        public static void ClientReceiveThread(object obj)
        {
            NetworkStream stream = obj as NetworkStream;
            try
            {
                while (true)
                {
                    byte[] receiveData = new byte[1024];
                    // 5.读取数据
                    int length = stream.Read(receiveData, 0, receiveData.Length);
                    string msg = Encoding.UTF8.GetString(receiveData, 0, length);
                    Console.WriteLine(msg);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                // throw;
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值