美文网首页
0001-TcpListener/TcpClient同步编程

0001-TcpListener/TcpClient同步编程

作者: HeavenOrSky | 来源:发表于2019-04-18 10:13 被阅读0次

    在使用TcpListener/TcpClient同步编程之前,要引入两个命名空间

    using System.Net;
    using System.Net.Sockets;
    

    服务器代码示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    namespace Net
    {
        class Program
        {
            static void Main(string[] args)
            {
                //服务端对端口进行侦听:
                TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001));
                listener.Start();
                //检测来自客户端的连接请求
                Console.WriteLine("等待连接请求...........");
                TcpClient remoteClient = listener.AcceptTcpClient();
                Console.WriteLine("连接到客户端!!!");
                //建立和连接的客户端的数据流(传输数据)
                NetworkStream streamFromClient = remoteClient.GetStream();
                //创建缓存区
                byte[] buffer = new byte[8096];
    
                //每次读取缓存中的大小
                int byteRead;
                try
                {
                    //为了保证数据的完整性以及安全性锁定数据流
                    //读取客户端发送来的数据
                    lock (streamFromClient)
                    {
                        byteRead = streamFromClient.Read(buffer, 0, 8096);
                        Console.WriteLine(DateTime.Now +":收到来自客户端的消息:"+Encoding.Unicode.GetString(buffer, 0, byteRead));
                    }
    
                    //向连接的客户端发送数据
                    lock (streamFromClient)
    
                    {
                        //buffer为发送的字符数组
                        streamFromClient.Write(buffer,0, buffer.Length);                  
    
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
    
                streamFromClient.Dispose();
                remoteClient.Close();
    
                Console.ReadLine();
            }
        }
    }
    
    

    客户端代码示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    namespace Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect("127.0.0.1", 10001);
                if (tcpClient.Connected)
                {
                    NetworkStream streamFromServer = tcpClient.GetStream();
                    byte[] buffer = Encoding.Unicode.GetBytes("hello world Unity");
                    int byteRead;
                    try
                    {
                        lock (streamFromServer)
                        {
                            //发送到服务器
                            streamFromServer.Write(buffer, 0, buffer.Length);
                            buffer = new byte[8096];
                            
                        }
                        lock (streamFromServer)
                        {
                            byteRead = streamFromServer.Read(buffer, 0, 8096);
                            Console.WriteLine(DateTime.Now + "收到来自服务器的消息:" + Encoding.Unicode.GetString(buffer, 0, byteRead));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                Console.ReadLine();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:0001-TcpListener/TcpClient同步编程

          本文链接:https://www.haomeiwen.com/subject/wgczwqtx.html