通过TCP / IP将.text文件从一台计算机简单发送到另一台计算机

问题描述 投票:0回答:1

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

演示:

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

我的问题:两个项目都没有编译错误或语法错误,唯一的问题是服务器没有收到.txt文件。

Client:

首先,我为客户设计了一个表单,例如:

enter image description here

并从ToolBox-> Dialogs-> OpenFileDialog控件中放置一个OpenFileDialog。

完整代码:

namespace SFileTransfer
{
    public partial class Form1 : Form
    {
        string n;
        byte[] b1;
        OpenFileDialog op;

        private void button1_Click(object sender, EventArgs e) //browse btn
        {
            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;
                TcpClient client = new TcpClient("127.0.0.1", 8100);//"127.0.0.1", 5055
                StreamWriter sw = new StreamWriter(client.GetStream());
                sw.WriteLine(n);
                sw.Flush();
               // label2.Text = "File Transferred....";
            }
        }
        private void button2_Click(object sender, EventArgs e) //send btn
        {
            TcpClient client = new TcpClient("127.0.0.1", 8100);//5050
            Stream s = client.GetStream();
            b1 = File.ReadAllBytes(op.FileName);
            s.Write(b1, 0, b1.Length);
            client.Close();
           // label2.Text = "File Transferred....";
        }
    }
}

Server:

为服务器创建并设计了一个Form,如:

enter image description here

然后从ToolBox-> Dialogs-> folderBrowserDialog中放置一个folderBrowserDialog。

完整代码:

namespace SFileTransferServer
{
    public partial class Form1 : Form
    {
        string rd;
        byte[] b1;
        string v;
        int m=1;
        TcpListener list;
        TcpClient client;
        Int32 port = 8100;//5050
        Int32 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();

                client = list.AcceptTcpClient();
                MessageBox.Show("ggggggg" + m.ToString());
                Stream s = client.GetStream();
                b1 = new byte[m];

                s.Read(b1, 0, b1.Length);
                MessageBox.Show(textBox1.Text);
                File.WriteAllBytes(textBox1.Text+ "\\" + rd.Substring(0, rd.LastIndexOf('.')), b1);
                list.Stop();
                client.Close();
                label1.Text = "File Received......";
            }
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            TcpListener list = new TcpListener(localAddr, 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();
        }
    }
}

Based on this page

c# tcp-ip
1个回答
-1
投票

我假设你是一个新的编码器,因为我看到很多低效的代码。

在客户端应用程序上:不要重新声明TcpClient,而是在全局范围内声明它并重新使用它。

然后在服务器端:始终使用正确的数据类型IF AT ALL

Int16 port = 8100;
//same as
int port = 8100;

接下来的问题是:首先,您只是从接收到的数据中读取一个字节

int m=1;
byte[] b1 = new byte[m];
s.Read(b1, 0, b1.Length);
//Then if you know the length of the byte, why do the computation of b1.Length, just use
s.Read(b1, 0, 1);

我现在看到你也在Load事件上打开连接。但是该变量不在全局范围内,因此您实际上是在Load事件中创建变量,然后在Load事件完成之后,将其发送到垃圾回收集,然后在按钮单击事件中声明具有相同名称的变量。

因此,尝试在全局范围内声明TcpListener对象,然后在load事件中分配它并开始侦听。

现在,AcceptTcpClient()方法是一个阻塞方法,因此它将阻止你的thead,直到它收到连接尝试,此时它将返回客户端对象。因此,请尝试使用此代码:https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.accepttcpclient(v=vs.110).aspx

using System.Threading;
TcpClient client;
TcpListener server = new TcpListener("127.0.0.1",8100)
Thread incoming_connection = new Thread(ic);
incoming_connection.Start();

private void ic() {
client = server.AcceptTcpClient();
Stream s = client.GetStream();
//And then all the other code
}

或者您可以使用MSDN建议的Pending方法(在参考页面上)。

编辑:使用这样的线程是“不安全”,所以如果你不理解线程,请先阅读它,这篇文章不涉及如何使用多线程。

© www.soinside.com 2019 - 2024. All rights reserved.