美文网首页
NetworkStream简单例子,客户端服务器

NetworkStream简单例子,客户端服务器

作者: 价值投机168 | 来源:发表于2019-12-11 09:26 被阅读0次

客户端:

  class Program
{
    /// <summary>
    /// 客户端
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        SendImageToServer(@"E:\temp\ccc.jpg");
    }

    static void SendImageToServer(string imgURl)
    {
        if (!File.Exists(imgURl)) return;
        //创建一个文件流打开图片
        FileStream fs = File.Open(imgURl, FileMode.Open);
        //声明一个byte数组接受图片byte信息
        byte[] fileBytes = new byte[fs.Length];
        using (fs)
        {
            //将图片byte信息读入byte数组中
            fs.Read(fileBytes, 0, fileBytes.Length);
            fs.Close();
        }
        //找到服务器的IP地址
        IPAddress address = IPAddress.Parse("127.0.0.1");
        //创建TcpClient对象实现与服务器的连接
        TcpClient client = new TcpClient();
        //连接服务器
        client.Connect(address, 80);
        using (client)
        {
            //连接完服务器后便在客户端和服务端之间产生一个流的通道
            NetworkStream ns = client.GetStream();
            using (ns)
            {
                //通过此通道将图片数据写入网络流,传向服务器端接收
                ns.Write(fileBytes, 0, fileBytes.Length);
                //ns.Write(fileBytes, 0, fileBytes.Length);
            }
        }
    }
}

服务器端:有个bug,只有关闭程序,才能看见文件成功了。只是演示方法,需要使用时自己优化

class Program
{
    //全局TcpClient
    static TcpClient client;
    //文件流建立到磁盘上的读写流
    static FileStream fs = new FileStream("E:\\abc.jpg", FileMode.Create);
    //buffer
    static int bufferlength = 200;
    static byte[] buffer = new byte[bufferlength];
    //网络流
    static NetworkStream ns;

    static void Main(string[] args)
    {
        ConnectAndListen();
    }

    static void ConnectAndListen()
    {
        //服务端监听任何IP 但是端口号是80的连接
        TcpListener listener = new TcpListener(IPAddress.Any, 80);
        //监听对象开始监听
        listener.Start();
        while (true)
        {
            Console.WriteLine("等待连接");
            //线程会挂在这里,直到客户端发来连接请求
            client = listener.AcceptTcpClient();
            Console.WriteLine("已经连接");
            //得到从客户端传来的网络流
            ns = client.GetStream();
            //如果网络流中有数据
            if (ns.DataAvailable)
            {
                //同步读取网络流中的byte信息
                // do
                //  {
                //  ns.Read(buffer, 0, bufferlength);
                //} while (readLength > 0);

                //异步读取网络流中的byte信息
                ns.BeginRead(buffer, 0, bufferlength, ReadAsyncCallBack, null);
            }
        }
    }

    /// <summary>
    /// 异步读取
    /// </summary>
    /// <param name="result"></param>
    static void ReadAsyncCallBack(IAsyncResult result)
    {
        int readCount;
        //获得每次异步读取数量
        readCount = client.GetStream().EndRead(result);
        //如果全部读完退出,垃圾回收
        if (readCount < 1)
        {
            client.Close();
            ns.Dispose();
            return;
        }
        //将网络流中的图片数据片段顺序写入本地
        fs.Write(buffer, 0, buffer.Length);
        //再次异步读取
        ns.BeginRead(buffer, 0, 200, ReadAsyncCallBack, null);
    }
}

相关文章

网友评论

      本文标题:NetworkStream简单例子,客户端服务器

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