简体   繁体   English

文件传输到 c#

[英]File Transfer to c#

I am trying to send a file from python client to a c# server and present it on screen by saving it first and then showing it on my MainWindow.我正在尝试将文件从 python 客户端发送到 c# 服务器,并通过先保存然后在我的 MainWindow 上显示来将其呈现在屏幕上。 I came across a couple of problems I can't figure out why happen (I'm new to C#) I followed this guide: http://snippetbank.blogspot.com/2014/04/csharp-client-server-file-transfer-example-1.html The problems were:我遇到了几个我不知道为什么会发生的问题(我是 C# 新手)我遵循了本指南: http://snippetbank.blogspot.com/2014/04/csharp-client-server-file- transfer-example-1.html问题是:

1. The file was not being saved to my folder. 1. 文件没有保存到我的文件夹中。

2. when I used message box to try and detect if it passes all the info it looks like it gets stuck in the middle. 2. 当我使用消息框尝试检测它是否传递了所有信息时,它看起来像是卡在了中间。

I've been stuck on this for quite some time now but can't figure out what I'm missing我已经坚持了很长一段时间,但无法弄清楚我错过了什么

Python code: 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# CODE: 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;
    }

In your C# code looks like a while cycle is missing, unless you call the function ReceiveFile() iterating somewhere else.在您的 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. 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.

You don't know when the client will connect and send the data so you can't just call the ReceiveFile() function once.您不知道客户端何时连接并发送数据,因此您不能只调用一次 ReceiveFile() function。

In the example there is a perpetual while (true) cycle that makes that reader.Readline() work:在示例中,有一个永久的 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();  
            ...
            }

Do you have an equivalent in your code?您的代码中有等价物吗? Also, is not pratical to use MessageBox for debugging purposes.此外,将 MessageBox 用于调试目的也不实用。

Try:尝试:

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

Note you need System.Diagnostics to use that.请注意,您需要 System.Diagnostics 才能使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM