帮帮我吧! !我需要在C#中进行文件传输 [英] Help me pease ! ! I need to file transfer in C#

查看:66
本文介绍了帮帮我吧! !我需要在C#中进行文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的程序。和socket或任何东西的本地文件传输帮助我。 :(



我尝试了什么:



我试过这个:



客户代码:

I need a simple program. And local file transfer with socket or anything pls help me. :(

What I have tried:

I tried this:

CLIENT code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.IO;

namespace FileSharingClient
{
    public partial class Form1 : Form
    {
        private static string shortFileName = "";
        private static string fileName = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "File Sharing Client";
            dlg.ShowDialog();
            txtFile.Text = dlg.FileName;
            fileName = dlg.FileName;
            shortFileName = dlg.SafeFileName;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string ipAddress = txtIPAddress.Text;
            int port = int.Parse(txtHost.Text);
            string fileName = txtFile.Text;
            Task.Factory.StartNew(() => SendFile(ipAddress,port,

             fileName,shortFileName));
            MessageBox.Show("File Sent");
        }

        public void SendFile(string remoteHostIP, int remoteHostPort, string  longFileName, string shortFileName)
        {
         try
         {
          if(!string.IsNullOrEmpty(remoteHostIP))
            {
             byte[] fileNameByte = Encoding.ASCII.GetBytes

             (shortFileName);
             byte[] fileData = File.ReadAllBytes(longFileName);
             byte[] clientData = new byte[4 + fileNameByte.Length

                + fileData.Length];
             byte[] fileNameLen = BitConverter.GetBytes(

              fileNameByte.Length);
             fileNameLen.CopyTo(clientData, 0);
             fileNameByte.CopyTo(clientData, 4);
             fileData.CopyTo(clientData, 4 + fileNameByte.Length);
             TcpClient clientSocket = new TcpClient(remoteHostIP,

              remoteHostPort);
             NetworkStream networkStream = clientSocket.GetStream();
             networkStream.Write(clientData, 0, clientData.GetLength

              (0));
             networkStream.Close();
           }
         }
         catch
         {

         }
        }
    }
}





服务器代码:



SERVER code:

<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.IO;
using System.Threading.Tasks;

namespace FileSharingServer
{
    public partial class Form1 : Form
    {

        public delegate void FileRecievedEventHandler(object source,string fileName);

        public event FileRecievedEventHandler NewFileRecieved;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.NewFileRecieved+=new FileRecievedEventHandler(Form1_NewFileRecieved);
        }

        private void Form1_NewFileRecieved(object sender, string
         fileName)
        {
            this.BeginInvoke(
            new Action(
            delegate()
            {
                MessageBox.Show("New File Recieved\n"+fileName);
                System.Diagnostics.Process.Start("explorer", @"c:\");
            }));
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            int port = int.Parse(txtHost.Text);
            Task.Factory.StartNew(() => HandleIncomingFile(port));
            MessageBox.Show("Listening on port"+port);
        }

        public void HandleIncomingFile(int port)
        {
          try
            {
                TcpListener tcpListener = new TcpListener(port);
                tcpListener.Start();
                while (true)
                {
                  Socket handlerSocket = tcpListener.AcceptSocket();
                    if (handlerSocket.Connected)
                    {
                     string fileName = string.Empty;
                     NetworkStream networkStream = new NetworkStream

                      (handlerSocket);
                     int thisRead = 0;
                     int blockSize = 1024;
                     Byte[] dataByte = new Byte[blockSize];
                     lock (this)
                     {
                      string folderPath = @"c:\";
                      handlerSocket.Receive(dataByte);
                      int fileNameLen = BitConverter.ToInt32(dataByte,

                       0);
                      fileName = Encoding.ASCII.GetString(dataByte, 4,

                       fileNameLen);
                      Stream fileStream = File.OpenWrite(folderPath +

                       fileName);
                      fileStream.Write(dataByte, 4+fileNameLen,(

                       1024-(4+fileNameLen)));
                      while (true)
                      {
                       thisRead = networkStream.Read(dataByte, 0,

                        blockSize);
                       fileStream.Write(dataByte, 0,thisRead);
                       if (thisRead == 0)
                        break;
                      }
                      fileStream.Close();

                    }
                    if (NewFileRecieved != null)
                     {
                       NewFileRecieved(this, fileName);
                     }
                    handlerSocket = null;
                    }
                }

            }
            catch
            {

            }
        }
    }
}





它不工作帮助我而不是我的代码,我找到了一些这个代码的网站。帮帮我



Its not working help me and Its not my code, I found some sites this code. help me

推荐答案

说它不起作用并不能提供你可能出现的问题的信息。



但是,两个代码块的一个明显问题是,如果出现问题,则不会通知用户并且不会记录问题。这是因为你有空的catch块。



要开始使用,请至少添加一个消息框或登录catch块以获取有关潜在问题的更多信息。如果发生异常,请调查整个异常堆栈(内部异常)以清楚地了解导致问题的原因。这样你就可以得到一些具体的东西。



ADDITION

--------

调查问题的一种简单方法可能就是注释掉try catch语句。这种方式在抛出异常时,它被Visual Studio捕获。当发生异常时,请调查它使用Visual Studio调试功能。



几篇文章开头:

- 从调试器开始 - Visual Studio | Microsoft Docs [ ^ ]

- 使用Visual Studio调试器管理异常 - Visual Studio | Microsoft Docs [ ^ ]
Saying just that it's not working does not give information about possible problems you have.

However, one obvious problem with both of the code blocks is that if something goes wrong, user is not informed and the problem is not logged. This is because you have empty catch blocks.

To get started with, add at least a message box or logging into the catch blocks to get more information about potential problems. If an exception occurs, investigate the whole exception stack (inner exceptions) to get a clear picture what causes a problem. This way you get something concrete to work with.

ADDITION
--------
One simple way for you to investigate the problem could be just to comment out the try catch statements. This way when an exception is thrown, it's catch by Visual Studio. When an exception occurs investigate it Using Visual Studio debugging capabilities.

Few articles to start with:
- Get started with the debugger - Visual Studio | Microsoft Docs[^]
- Manage exceptions with the Visual Studio debugger - Visual Studio | Microsoft Docs[^]


首先感谢您回答我:)我做了这个,但我没有理解任何事情。我是C#的初学者。我有服务器和客户端消息程序。我将在此使用文件传输。
Firstly thank you for answered to me :) I did this but I didnt understanding anything. I'm beginner in C#. I have server and Client to message program. I will use file transfer in this.


这篇关于帮帮我吧! !我需要在C#中进行文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆