如何多次发送文件 [英] How to send file more then once

查看:35
本文介绍了如何多次发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个 C# 项目:一个项目用于客户端,另一个用于服务器.第一步是运行服务器,然后选择一个目标文件夹,然后运行客户端项目,选择一些 text.txt 发送到服务器的目标文件夹.只有客户端可以向服务器发送文件

There are two C# projects: one project is for the client, the other one is for the server. First step is to run the server , then to choose a target folder, after that to run the client project, to choose some text.txt to send to the server's target folder. Only client can send files to the server

演示:

1.choosing file target                       2.client sends
   +------------+                                
   | tar folder |          <----------------       text.txt 
   +------------+

这很好用,但我希望它每隔几分钟自动发送一次 text.txt 所以我添加了 while(true) 循环和 Thread.Sleep(10000)但它总是给出例外.我的意思是,它不会让我再发送一次.

This works fine, but I want it to send the text.txt automatically every few minutes so I added while(true) loop and Thread.Sleep(10000) but it alaway gives Exceptions. I mean, It's not gives me to send it more then once.

在客户端类中,我试图放置一个 while 循环:

Here the in client class I'm trying to put a while loop :

 public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)//browse btn
        {
            string n;
            byte[] b1;
            TcpClient client;
            OpenFileDialog op;
            Stream s;
            StreamWriter sw;
            op = new OpenFileDialog();

            if (op.ShowDialog() == DialogResult.OK)
            {
                string t = textBox1.Text;
                t = op.FileName;
                FileInfo fi = new FileInfo(textBox1.Text = op.FileName);
                n = fi.Name + "." + fi.Length;

                client = new TcpClient("127.0.0.1", 8100);//"127.0.0.1", 5055

                sw = new StreamWriter(client.GetStream());
                s = client.GetStream();
                b1 = File.ReadAllBytes(op.FileName);

                //  s = client.GetStream();
                while (true)
                {

                    s.Write(b1, 0, b1.Length);
                    sw.WriteLine(n);
                     sw.Flush();
                    //sw.Close();
                    s.Close();//added by me
 Thread.Sleep(18000);

                                   }
                sw.Flush();
                client.Close();

                // label2.Text = "File Transferred....";
            }
        }

服务器类:

public Form1()
        {
            InitializeComponent();
        }

        string rd;
        byte[] b1;
        string v;
        int m=20;//number of byts
        TcpListener list;
        TcpClient client;
        int port = 8100;//5050
        int port1 = 8100;//5055
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");
        private void button1_Click(object sender, EventArgs e)//browse button
        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {

                    textBox1.Text = folderBrowserDialog1.SelectedPath;
                    list = new TcpListener(localAddr, port1);
                    list.Start();

                    Thread incoming_connection = new Thread(ic);
                    incoming_connection.Start();

            }
        }

        private void ic()
        {

            client = list.AcceptTcpClient();
            Stream s = client.GetStream();
            b1 = new byte[m];
            s.Read(b1,0, b1.Length);
            MessageBox.Show("pathh "+textBox1.Text);
            File.WriteAllBytes(textBox1.Text+"\\flag.txt", b1);// the left side us the name of the written file
            //list.Stop();
            //client.Close();
          //  label1.Text = "File Received......";
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            list = new TcpListener(localAddr, port);
           // TcpListener list = new TcpListener(port);
            list.Start();
            TcpClient client = list.AcceptTcpClient();
            MessageBox.Show("Client trying to connect");
            StreamReader sr = new StreamReader(client.GetStream());
            rd = sr.ReadLine();
            v = rd.Substring(rd.LastIndexOf('.') + 1);
            m = int.Parse(v);
           // list.Stop();
           // client.Close();
        }

相关

推荐答案

尝试更改为:

while (true)
{
    using (var client = new TcpClient("127.0.0.1", 8100))//"127.0.0.1", 5055
    using (var sw = new StreamWriter(client.GetStream()))
    using (var s = client.GetStream())
    {
        b1 = File.ReadAllBytes(op.FileName);
        //  s = client.GetStream();
        s.Write(b1, 0, b1.Length);
        sw.WriteLine(n);
        sw.Flush();
    }
    Thread.Sleep(18000);
}

使用 语句有助于处理对象.

using statement helps on disposing objects.

或者,如果您想保持连接打开,请尝试:

Alternativelly, if you want to keep the connection open, try:

using (var client = new TcpClient("127.0.0.1", 8100))//"127.0.0.1", 5055
while (true)
{
    using (var sw = new StreamWriter(client.GetStream()))
    using (var s = client.GetStream())
    {
        b1 = File.ReadAllBytes(op.FileName);
        //  s = client.GetStream();
        s.Write(b1, 0, b1.Length);
        sw.WriteLine(n);
        sw.Flush();
    }
    Thread.Sleep(18000);
}

这篇关于如何多次发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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