• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我希望你是我獨家記憶

            一段永遠封存的記憶,隨風而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

            TCP客戶端

            Posted on 2009-07-16 13:14 Hero 閱讀(492) 評論(0)  編輯 收藏 引用 所屬分類: C#積累
             1using System;
             2using System.Collections.Generic;
             3using System.Text;
             4using System.IO;
             5
             6namespace Server
             7{
             8    class PictureHelper
             9    {
            10        public static string[] GetFileList()
            11        {
            12            //注意得到的是完整的路徑名稱
            13            string[] files = Directory.GetFiles( Properties.Settings.Default.PictureDirectory );
            14
            15            //remove the directory path from the filename
            16            for ( int i = 0; i < files.Length; i++ )
            17            {
            18                files[i] = Path.GetFileName( files[i] );
            19            }

            20
            21            return files;
            22        }

            23
            24        //返回圖片文件的字節數組
            25        public static byte[] GetPictureBytes( string filename )
            26        {
            27            //通過FileInfo來獲得文件的長度
            28            FileInfo fileInfo = new FileInfo( filename );
            29            byte[] buffer = new byte[fileInfo.Length];
            30            using ( FileStream fileStream = fileInfo.OpenRead() )
            31            {
            32                fileStream.Read( buffer, 0, buffer.Length );
            33            }

            34
            35            return buffer;
            36        }

            37
            38        //在字節數組中返回文件列表
            39        public static byte[] GetFileListBytes()
            40        {
            41            string[] files = PictureHelper.GetFileList();
            42
            43            StringBuilder responseMessage = new StringBuilder();
            44
            45            foreach ( string str in files )
            46            {
            47                responseMessage.Append( str );
            48                responseMessage.Append( ":" );
            49            }

            50
            51            byte[] responseBuffer = Encoding.ASCII.GetBytes( responseMessage.ToString() );
            52
            53            return responseBuffer;
            54        }

            55    }

            56}

            57

             1using System;
             2using System.Collections.Generic;
             3using System.Windows.Forms;
             4using System.Net;
             5using System.Net.Sockets;
             6using System.IO;
             7
             8namespace Server
             9{
            10    static class Program
            11    {
            12        /// <summary>
            13        /// 應用程序的主入口點。
            14        /// </summary>

            15        [STAThread]
            16        static void Main()
            17        {
            18            //Application.EnableVisualStyles();
            19            //Application.SetCompatibleTextRenderingDefault( false );
            20            //Application.Run( new Form1() );
            21
            22            //新建一個監聽器
            23            TcpListener listener = new TcpListener(IPAddress.Any, Properties.Settings.Default.Port);
            24
            25            listener.Start();//開始監聽
            26
            27            while ( true )
            28            {
            29                const int bufferSize = 256;
            30
            31                //創建一個客戶端
            32                TcpClient client = listener.AcceptTcpClient();
            33                //從客戶端獲得網絡流
            34                NetworkStream stream = client.GetStream();
            35
            36                //從客戶端網絡流中讀取查詢數據
            37                byte[] buffer = new byte[bufferSize];
            38                //記錄實際獲得的字節數
            39                int readBytes = 0;
            40                readBytes = stream.Read( buffer, 0, bufferSize );
            41                //獲得查詢字符串
            42                string request = (System.Text.Encoding.ASCII.GetString( buffer )).Substring( 0, readBytes );
            43
            44                if ( request.StartsWith( "LIST" ) )
            45                {//如果請求的是列表
            46
            47                    byte[] responseBuffer = PictureHelper.GetFileListBytes();
            48
            49                    stream.Write( responseBuffer, 0, responseBuffer.Length );
            50                }

            51                else if ( request.StartsWith( "FILE" ) )
            52                {//如果請求的是文件
            53
            54                    //獲得要下載的文件名字
            55                    string[] requestMessage = request.Split( ':' ) ;
            56                    string filename = requestMessage[1];
            57
            58                    //string curpath = (( Properties.Settings.Default.PictureDirectory ).ToString() + @"\" + filename );
            59                    //curpath = @"D:\Picture\" + filename ;
            60                    //byte[] data = File.ReadAllBytes( curpath );
            61
            62                    string curpath = Path.Combine( Properties.Settings.Default.PictureDirectory, filename );
            63                    MessageBox.Show( curpath.ToString() );
            64                    //從絕對路徑中獲取文件的全部字節流
            65                    byte[] data = File.ReadAllBytes( curpath );
            66                    
            67                    //將圖片發送給客戶端
            68                    stream.Write( data, 0, data.Length );
            69                }

            70
            71                //不要忘記關閉流
            72                stream.Close();
            73                client.Close();
            74            }

            75        }

            76    }

            77}
            久久精品国产久精国产思思| 亚洲精品美女久久777777| 曰曰摸天天摸人人看久久久| 99久久国产综合精品五月天喷水| 丁香五月综合久久激情| 三级三级久久三级久久| 国产精品久久久久AV福利动漫| 国产精品成人99久久久久91gav | 久久久无码人妻精品无码| 2021久久国自产拍精品| 久久青青草视频| 精品国产91久久久久久久a | 国产精品视频久久| 久久丝袜精品中文字幕| 久久精品aⅴ无码中文字字幕重口| 精品久久久无码中文字幕天天| 色欲综合久久躁天天躁蜜桃| 日韩电影久久久被窝网| 色偷偷888欧美精品久久久| 亚洲AV无码一区东京热久久| 久久久久一级精品亚洲国产成人综合AV区| 亚洲综合精品香蕉久久网| 中文字幕无码久久精品青草| 国产福利电影一区二区三区,免费久久久久久久精 | 欧洲人妻丰满av无码久久不卡| 久久黄视频| 国内精品伊人久久久久影院对白| 99久久99久久久精品齐齐 | 色欲久久久天天天综合网| 深夜久久AAAAA级毛片免费看 | 欧美黑人又粗又大久久久| 亚洲人成无码久久电影网站| 精品久久久久久无码国产| 99久久国产热无码精品免费久久久久| 麻豆亚洲AV永久无码精品久久| 久久久久青草线蕉综合超碰| 色婷婷综合久久久久中文字幕| 香蕉99久久国产综合精品宅男自| 欧美性大战久久久久久| 亚洲午夜无码AV毛片久久| 久久综合久久美利坚合众国|