欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
C# TcpListener和TcpClient的應用

C# TcpListener和TcpClient的應用

(2013-01-14 10:33:06)
標簽:

雜談

//服務(wù)器端程序
using System.IO;
using System.Net.Sockets;
using System.Net;
namespace PictureServer
{
    class Program
    {
       static void Main(string[] args)
       {
//在本機創(chuàng )建一個(gè)TcpListener,端口是8888,
          TcpListener listener = new TcpListener(IPAddress.Any, 8888);
//開(kāi)始監聽(tīng),
          listener.Start();
//循環(huán),等待客戶(hù)端連接
           while(true)
           {

              const int bufferSize =256;
//接受客戶(hù)端的連接,利用client保存連接的客戶(hù)端
              TcpClient client =listener.AcceptTcpClient();
//獲取客戶(hù)端的流stream
              NetworkStream clientStream =client.GetStream();
              byte[] buffer = newbyte[bufferSize];
              int readBytes = 0;
//將客戶(hù)端流讀入到buffer中
              readBytes =clientStream.Read(buffer, 0, bufferSize);
//將從客戶(hù)端流讀取的數據保存到字符串request中
              string request =Encoding.ASCII.GetString(buffer).Substring(0, readBytes);
//如果客戶(hù)端的命令以L(fǎng)IST開(kāi)頭,
              if(request.StartsWith("LIST"))
              {
                 // LIST request - return list
//利用類(lèi)PictureHelper的函數GetFileListBytes,獲取圖片文件列表
                 byte[] responseBuffer =PictureHelper.GetFileListBytes();
//將服務(wù)器獲取的圖片文件列表寫(xiě)入到clientStream中
                 clientStream.Write(responseBuffer, 0,responseBuffer.Length);
              }
//如果客戶(hù)端的請求命令以FILE開(kāi)頭,即獲取單個(gè)圖片文件
              else if(request.StartsWith("FILE"))
              {
              // FILE request - returnfile
                 // get the filename
//獲取請求的文件名字
                 string[] requestMessage =request.Split(':');
                 string filename = requestMessage[1];
//利用File.ReadAllBytes函數將文件里面的文件filename讀入到字節數組data中,             
    byte[] data =File.ReadAllBytes(Path.Combine(@"C:Documents andSettingsAdministratorMy DocumentsMy Pictures", filename));
                 // Send the picture to the client.
//將data中的文件內容寫(xiě)入到客戶(hù)端clientStream中,傳回客戶(hù)端
                 clientStream.Write(data, 0, data.Length);
              }
//關(guān)閉客戶(hù)端流
              clientStream.Close();
           }
       }
//靜態(tài)類(lèi)PictureHelper,
       public static class PictureHelper
       {
//提供文件夾中的文件列表
           publicstatic string[] GetFileList()
           {
              string[] files =Directory.GetFiles(@"C:Documents and SettingsAdministratorMyDocumentsMy Pictures");
//去掉文件夾路徑,只保留文件名
              // Remove the directory pathfrom the filename.
              for (int i = 0; i <files.Length; i++)
              {
                 files[i] = Path.GetFileName(files[i]);
              }
              return files;
           }
//將文件filename的內容讀到字節數組中
           publicstatic byte[] GetPictureBytes(string filename)
           {
              FileInfo fileInfo = newFileInfo(filename);
              byte[] buffer = newbyte[fileInfo.Length];
              using (FileStream stream =fileInfo.OpenRead())
              {
                 stream.Read(buffer, 0, buffer.Length);
              }
              return buffer;
           }

           publicstatic byte[] GetFileListBytes()
           {
              // LIST request - returnlist
              string[] files =PictureHelper.GetFileList();
              StringBuilder responseMessage= new StringBuilder();
              foreach (string s infiles)
              {
                 responseMessage.Append(s);
                 responseMessage.Append(":");
              }
              byte[] responseBuffer =Encoding.ASCII.GetBytes(
             responseMessage.ToString());
              return responseBuffer;
           }
       }
    }
}
 
//////客戶(hù)端程序
using System.Net;
using System.Net.Sockets;
using System.IO;
 
       private void buttonListPictures_Click(objectsender, EventArgs e)
       {
           const intbufferSize = 4096;
           // Connectto the server.
//生成一個(gè)TcpClinet
           TcpClientclient = new TcpClient();
//生成服務(wù)器的IPHostEntry,因為我的服務(wù)器和客戶(hù)端在同一臺計算機上,所以此處用localhost,實(shí)際應用中,此處的localhost應用用服務(wù)器的ip地址代替,
          IPHostEntry host = Dns.GetHostEntry("localhost");
// 連接到服務(wù)器的8888端口
 client.Connect(host.AddressList[0],8888);
           // Send arequest to the server.
//創(chuàng )建一個(gè)NetworkStream,  
        NetworkStream clientStream =client.GetStream();
//request是"LIST"         
          stringrequest = "LIST";
//將request放入到字節數組requestBuffer中
           byte[]requestBuffer = Encoding.ASCII.GetBytes(request);
//將請求寫(xiě)入到NetworkStream中,發(fā)送到服務(wù)器端
          clientStream.Write(requestBuffer, 0, requestBuffer.Length);
//獲取服務(wù)器端的回應
           // Readthe response from the server.
           byte[]responseBuffer = new byte[bufferSize];
//生成一個(gè)內存流memstream
          MemoryStream memStream = new MemoryStream();
           intbytesRead = 0;
           do
           {
//將網(wǎng)絡(luò )流中的數據按256字節一組的順序寫(xiě)入到memStream中
           bytesRead= clientStream.Read(responseBuffer, 0, bufferSize);
          memStream.Write(responseBuffer, 0, bytesRead);
           } while(bytesRead > 0);
          clientStream.Close();
          client.Close();
//從內存流讀取數據
           byte[]buffer = memStream.GetBuffer();
           stringresponse = Encoding.ASCII.GetString(buffer);
//將服務(wù)器返回的文件列表分解開(kāi),文件名保存到字符串數組fileName中         
           string[]fileNames = response.Split(':');
//將字符串數組填到ListBox中
          this.listFiles.DataSource = fileNames;
       }
 
//獲取單個(gè)圖片文件,并顯示之    
       private void buttonGetPicture_Click(objectsender, EventArgs e)
       {
           const intbufferSize = 4096;
           TcpClientclient = new TcpClient();
          IPHostEntry host = Dns.GetHostEntry("localhost");
          client.Connect(host.AddressList[0],8888);
          NetworkStream clientStream = client.GetStream();
           try
           {
//生成請求,以FIlE:開(kāi)頭,后接在listbox中選擇的文件名
              string request = "FILE:" +this.listFiles.SelectedItem.ToString();
              byte[] requestBuffer =Encoding.ASCII.GetBytes(request);
//將請求寫(xiě)入到clientStream中,發(fā)送到服務(wù)器端
             clientStream.Write(requestBuffer, 0, requestBuffer.Length);
              byte[] responseBuffer = newbyte[bufferSize];
         
          MemoryStream memStream = new MemoryStream();
           intbytesRead = 0;
           do
           {
//獲取服務(wù)器端的回應,
              bytesRead =clientStream.Read(responseBuffer, 0, bufferSize);
//服務(wù)器的回應寫(xiě)入到memStream
             memStream.Write(responseBuffer, 0, bytesRead);
           } while(bytesRead > 0);
          clientStream.Close();
          client.Close();
 //從內存流中讀取圖片文件數據,顯示在picturebox控件上    
          pictureBox.Image = Image.FromStream(memStream);
       }
       catch (Exception exce)
       {
          MessageBox.Show(exce.Message);
       }
       }
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C# TcpClient網(wǎng)絡(luò )編程傳輸文件(帶文件名)
C# 實(shí)現基本的套接字TCP通信
[C# 網(wǎng)絡(luò )編程系列]自定義Web服務(wù)器
C# TCP通訊示例
C# Tutorial
C#線(xiàn)程系列講座(3):線(xiàn)程池和文件下載服務(wù)器
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久