2011-12-06 20 views
1

我試圖從一個星期,從客戶端電腦截圖,併發送到服務器的Bij網絡。c#在插槽發送截圖

現在它有點工作了,但並不完全。我嘗試了幾個我在互聯網上找到的腳本,但沒有一個能夠像我想要的那樣工作,所以我改變了它們,但是對於socket的一些瞭解我無法正確理解。你們能幫我嗎?

截圖功能正常工作,但是當我發送流到我的服務器時,它只接收圖像的頂部(例如:原始屏幕截圖爲150kb,但我收到的文件只有40kb)嘗試其他幾個文件發送腳本,但最多我只收到6個字節。

當我嘗試使用While函數時,它沒有發送任何東西或1kb,所以我知道我做錯了什麼,但是什麼?

這裏是我的服務器代碼:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     try 
     { 
      IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656); 
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 
      sock.Bind(ipEnd); 
      sock.Listen(100); 
      Socket clientSock = sock.Accept(); 

      byte[] clientData = new byte[1024 * 5000]; 
      string receivedPath = "C:/"; 

      int receivedBytesLen = clientSock.Receive(clientData); 

      int fileNameLen = BitConverter.ToInt32(clientData, 0); 
      string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen); 

      konjo = "Client: " + clientSock.RemoteEndPoint + "connected & File: " + fileName + " started received."; 

      BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ; 
      bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen); 

      status = "File: " + fileName + "Received & Saved at path: " + receivedPath; 

      bWrite.Close(); 
      clientSock.Close(); 
     } 
     catch (Exception ex) 
     { 
      status = "Failed: " + ex.Message; 
     } 
    } 

    private void bgWorker_Complete(object sender, RunWorkerCompletedEventArgs e) 
    { 
     label1.Text = status; 

    } 

這裏是我的客戶端代碼:

 private void bgsender_work(object sender, DoWorkEventArgs e) 
    { 
     try 
     { 
      int screenWidth = Screen.GetBounds(new Point(0, 0)).Width; 
      int screenHeight = Screen.GetBounds(new Point(0, 0)).Height; 
      Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight); 
      Graphics gfx = Graphics.FromImage((Image)bmpScreenShot); 
      gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight)); 
      MemoryStream test = new MemoryStream(); 
      bmpScreenShot.Save(test, ImageFormat.Jpeg); 
      bmpScreenShot.Save("C:\\Clienttest.jpeg", ImageFormat.Jpeg); 

      IPAddress[] ipAddress = Dns.GetHostAddresses(Dns.GetHostName()); 
      IPEndPoint ipEnd = new IPEndPoint(ipAddress[2], 5656); 
      Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 


      string fileName = "screenshot.Jpeg"; 
      string filePath = "Your File Path"; 
      byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName); 
      byte[] Data = test.ToArray(); 
      byte[] clientData = new byte[4 + fileNameByte.Length + Data.Length]; 
      byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length); 

      fileNameLen.CopyTo(clientData, 0); 
      fileNameByte.CopyTo(clientData, 4); 
      Data.CopyTo(clientData, 4 + fileNameByte.Length); 

      clientSock.Connect("192.168.1.15", 5656); 
      clientSock.Send(clientData); 

      status = "File: " + fileName + " has been sent."; 
      clientSock.Close(); 
      Console.ReadLine(); 
     } 
     catch (Exception ex) 
     { 
      status = "It failed: " + ex.Message; 
     } 

    } 

    private void bgsender_complete(object sender, RunWorkerCompletedEventArgs e) 
    { 
     label1.Text = status; 
    } 

回答

1

我爲客戶端服務器應用程序編寫了一些示例代碼,這些代碼似乎滿足您對另一個問題的需求。

看看:https://stackoverflow.com/a/5575287/381588,因爲它的工作原理,可能很容易適應您的要求。

+0

thx這對我有幫助! – Ceesjan

0

的截圖功能工作正確的,但是當我流發送到我的 服務器只接收頂部的圖像(例如:原始的 截圖製作爲150kb,但我收到的文件只有40kb)嘗試 其他幾個文件發送腳本,但最多我只接收6 字節..

當我嘗試使用一個While函數它發送我什麼都沒有或1kb所以我知道我做錯了什麼,但什麼?

聽起來像你循環發送前6個字節後的下6個字節。你的代碼更復雜,然後它需要。我會重構它,使它更容易理解。

我要做的一件事是將文件的大小發送到客戶端,在圖像之前,以便知道要讀取多少個字節。

它聽起來像你沒有讀足夠的字節,也可能是你沒有發送足夠的字節,這是你自己想出來的。

+0

發送6個字節是我嘗試的另一個腳本,這個腳本工作的最好的我嘗試的一切。但我無法弄清楚如何創建一個好的循環或發送內存的字節大小到我的服務器..所以我需要幫助。 – Ceesjan

+0

@Ceesjan - 你設法發送文件名長度OK,所以你需要擴展你的頭文件以發送數據長度。 –

+0

@Ceesjan - 就像我說過的...我會發送文件的大小是多少字節,然後發送文件,否則我只會接受一個reall大緩衝區。 –