簡體   English   中英

C# TCP客戶端發送文件

[英]C# TCP Client Sending files

我有一個關於使用 TCP 發送文件的小問題......

我正在嘗試將文件從客戶端發送到服務器。 正如您從客戶端的sendFile()函數中看到的那樣,我添加了一個帶有數字 9 的 prebuff,這有助於我在服務器端過濾傳入的數據。 (在這種情況下,如果流以 9 開頭,那么它將運行一個函數來讀取和寫入文件)

客戶端:

private void sendFile()
        {
            string string1 = String.Format("9");
            byte[] preBuf = Encoding.ASCII.GetBytes(string1);

            // Create the postBuffer data.
            string string2 = String.Format("");
            byte[] postBuf = Encoding.ASCII.GetBytes(string2);
            string filePath = @"\Users\nico_\Documents\transfer_ex.txt";

            newclient.Client.SendFile(filePath, preBuf, postBuf, TransmitFileOptions.UseDefaultWorkerThread);

服務器端(我過濾傳入數據的地方):

private void handleClient(object client, int i)
        {
            try
            {
                TcpClient streamData = (TcpClient)client;
                
                byte[] data = new byte[1024];
                byte[] sendData = new byte[1024];
                int byteRead;
                string strdata;
                ASCIIEncoding encode = new ASCIIEncoding();
                Thread.Sleep(2000);
                NetworkStream networkstream = streamData.GetStream();
                StreamReader streamReader = new StreamReader(networkstream);
                //Send Command 1
                sendData = encode.GetBytes("1");
                networkstream.Write(sendData, 0, sendData.Length);
                networkstream.Flush();
                //Listen...
                while (true)
                {

                    if (networkstream.DataAvailable == true)
                    {
                        byteRead = 1;
                        byteRead = networkstream.Read(data, 0, 1024);
                        //Debug.WriteLine(byteRead);

                        strdata = Encoding.ASCII.GetString(data, 0, byteRead);
                        //Debug.WriteLine(strdata);


                        //Get user info
                        if (strdata.StartsWith("1"))
                        {
                            updateLabel(labelMachinename, strdata, 0);
                            updateLabel(labelSampleOutput, strdata, 1);
                        }
                        if (strdata.StartsWith("2"))
                        {
                            updateText(txtCmdConsole, strdata);
                        }
                        if (strdata.StartsWith("9"))
                        {
                            
                            //Debug.WriteLine(strdata);

                            ReceiveFile(data, byteRead);
                        }
                        //receiveFile();
                    }
                }
            }
            catch (Exception err)
            {
                {
                    Cleanup();

                }
            }

        }

我處理接收到的文件的服務器端:

private void ReceiveFile(byte[] data, int byteRead)
    {
        string filePath = @"\Users\nico_\test";
        System.IO.Stream FileStream = System.IO.File.OpenWrite(filePath);
        Debug.WriteLine(byteRead);
        FileStream.Write(data, 0, byteRead);
        FileStream.Close();

    }

我面臨兩個挑戰..

  1. 從服務器端處理字節時如何刪除 9?
  2. 為什么ReceiveFile函數不起作用?

如果您有其他發送此數據的方法,請告訴我。

在此先感謝您的幫助。

在回答你的第二個問題時,雖然你沒有描述該方法是如何不起作用的,但我懷疑這是因為你在while(true)循環的第一次迭代中調用了break

請注意,您有Debug.WriteLine(byteRead); 作為if語句后的單行。 這意味着它是唯一在條件為true時運行的東西。 這也意味着下面的break語句將立即運行,而不考慮if條件的值:

if (byteRead == 0)
    Debug.WriteLine(byteRead);  // This is all that's dependent on the if condition
{
    break;   // This executes right away; it's not part of the if statement.
}

要解決它,請將if條件為真時應運行的所有代碼放在語句下的塊中:

if (byteRead == 0)
{
    Debug.WriteLine(byteRead);
    break;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM