簡體   English   中英

文件傳輸到 c#

[英]File Transfer to c#

我正在嘗試將文件從 python 客戶端發送到 c# 服務器,並通過先保存然后在我的 MainWindow 上顯示來將其呈現在屏幕上。 我遇到了幾個我不知道為什么會發生的問題(我是 C# 新手)我遵循了本指南: http://snippetbank.blogspot.com/2014/04/csharp-client-server-file- transfer-example-1.html問題是:

1. 文件沒有保存到我的文件夾中。

2. 當我使用消息框嘗試檢測它是否傳遞了所有信息時,它看起來像是卡在了中間。

我已經堅持了很長一段時間,但無法弄清楚我錯過了什么

Python代碼:

def send_file(conn, name):
try:
    full_path = "Output/"+name
    file_to_send = open(full_path, "rb")
    size = os.path.getsize(full_path)
    file_name = name + "\n"
    size_to_send = str(size) + "\n"
    conn.send(size_to_send.encode())
    conn.send(file_name.encode())

    while size > 0:
        data = file_to_send.read(1024)
        conn.send(data)
        size -= len(data)

    file_to_send.close()
    return conn

except IOError:
    print("FILE IS ALREADY OPEN")

C# 代碼:

 public static string ReceiveFile(StreamReader reader, TcpClient tcpClient) 
    {
        string folder = @"C:\Users\Roy\Desktop\GUI243\GUI243\";
        // The first message from the client is the file size    
        string cmdFileSize = reader.ReadLine();
        MessageBox.Show(cmdFileSize);
        // The first message from the client is the filename    
        string cmdFileName = reader.ReadLine();
        MessageBox.Show(cmdFileName);
        string full_path = folder + cmdFileName;

        int length = Convert.ToInt32(cmdFileSize);
        byte[] buffer = new byte[length];
        int received = 0;
        int read = 0;
        int size = 1024;
        int remaining = 0;
        // Read bytes from the client using the length sent from the client    
        while (received < length)
        {
            remaining = length - received;
            if (remaining < size)
            {
                size = remaining;
            }

            read = tcpClient.GetStream().Read(buffer, received, size);

            if (read == 0)
            {
                break;
            }

            received += read;
        }
        // Save the file using the filename sent by the client    
        using (FileStream fStream = new FileStream(Path.GetFileName(cmdFileName), FileMode.Create))
        {
            fStream.Write(buffer, 0, buffer.Length);
            fStream.Flush();
            fStream.Close();
        }
        return full_path;
    }

在您的 C# 代碼中,似乎缺少一個 while 循環,除非您調用 function ReceiveFile() 在其他地方迭代。

The class StreamReader needs to constantly check the tcp client socket to see if new data has been received because this is how a TCP stream works.

您不知道客戶端何時連接並發送數據,因此您不能只調用一次 ReceiveFile() function。

在示例中,有一個永久的 while (true) 循環使 reader.Readline() 工作:

  while (true)  
        {  
            // Accept a TcpClient    
            TcpClient tcpClient = tcpListener.AcceptTcpClient();  

            Console.WriteLine("Connected to client");  

            StreamReader reader = new StreamReader(tcpClient.GetStream());  

            // The first message from the client is the file size    
            string cmdFileSize = reader.ReadLine();  
            ...
            }

您的代碼中有等價物嗎? 此外,將 MessageBox 用於調試目的也不實用。

嘗試:

Debug.WriteLine ("Expected size is: " + cmdFileSize);

請注意,您需要 System.Diagnostics 才能使用它。

暫無
暫無

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

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