如何在.NET和android之间共享大文件

时间:2018-08-25 00:15:26

标签: java android .net tcp-ip file-sharing

我在C#.net中构建了一个简单的应用程序,该应用程序共享PC文件夹中的大文件。例如。使用TCP / IP协议从一个文件夹移动到另一个文件夹。 但是我想在PC上使用TCP / IP协议在Android设备上执行此操作。 我是Java的新手,但在.NET中有一定经验。

这是代码。

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

namespace LargeFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void bt_send_Click(object sender, EventArgs e)
        {
            if (dlg_open_file.ShowDialog() ==System.Windows.Forms.DialogResult.OK)
            {
                String selected_file = dlg_open_file.FileName;
                String file_name = Path.GetFileName(selected_file);
                FileStream fs = new FileStream(selected_file, FileMode.Open);
                TcpClient tc = new TcpClient("127.0.0.1", 6868);
                NetworkStream ns = tc.GetStream();
                byte[] data_tosend = CreateDataPacket(Encoding.UTF8.GetBytes("125"), Encoding.UTF8.GetBytes(file_name));
                ns.Write(data_tosend, 0, data_tosend.Length);
                ns.Flush();
                Boolean loop_break = false;
                while (true)
                {
                    if (ns.ReadByte() == 2)
                    {
                        byte[] cmd_buff = new byte[3];
                        ns.Read(cmd_buff, 0, cmd_buff.Length);
                        byte[] recv_data = ReadStream(ns);
                        switch (Convert.ToInt32(Encoding.UTF8.GetString(cmd_buff)))
                        {
                            case 126:
                                long recv_file_pointer = long.Parse(Encoding.UTF8.GetString(recv_data));
                                if (recv_file_pointer != fs.Length)
                                {
                                    fs.Seek(recv_file_pointer,SeekOrigin.Begin);
                                    int temp_buff_length = (int)(fs.Length - recv_file_pointer < 20000 ? fs.Length - recv_file_pointer: 20000);
                                    byte[] temp_buff = new byte[temp_buff_length];
                                    fs.Read(temp_buff, 0,temp_buff.Length);
                                    byte[] data_to_send = CreateDataPacket(Encoding.UTF8.GetBytes("127"), temp_buff);
                                    ns.Write(data_to_send, 0,data_to_send.Length);
                                    ns.Flush();
                                    pb_upload.Value = (int)Math.Ceiling((double)recv_file_pointer / (double)fs.Length * 100);
                                }
                                else
                                {
                                    byte[] data_to_send = CreateDataPacket(Encoding.UTF8.GetBytes("128"),Encoding.UTF8.GetBytes("Close"));
                                    ns.Write(data_to_send, 0,data_to_send.Length);
                                    ns.Flush();
                                    fs.Close();
                                    loop_break = true;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    if (loop_break == true)
                    {
                        ns.Close();
                        break;
                    }
                }
            }
        }
        public byte[] ReadStream(NetworkStream ns)
        {
            byte[] data_buff = null;

            int b = 0;
            String buff_length = "";
            while ((b = ns.ReadByte()) != 4)
            {
                buff_length += (char)b;
            }
            int data_length = Convert.ToInt32(buff_length);
            data_buff = new byte[data_length];
            int byte_read = 0;
            int byte_offset = 0;
            while (byte_offset < data_length)
            {
                byte_read = ns.Read(data_buff, byte_offset,data_length - byte_offset);
                byte_offset += byte_read;
            }

            return data_buff;
        }
        private byte[] CreateDataPacket(byte[] cmd, byte[] data)
        {
            byte[] initialize = new byte[1];
            initialize[0] = 2;
            byte[] separator = new byte[1];
            separator[0] = 4;
            byte[] datalength = Encoding.UTF8.GetBytes(Convert.ToString(data.Length));
            MemoryStream ms = new MemoryStream();
            ms.Write(initialize, 0, initialize.Length);
            ms.Write(cmd, 0, cmd.Length);
            ms.Write(datalength, 0, datalength.Length);
            ms.Write(separator, 0, separator.Length);
            ms.Write(data, 0, data.Length);
            return ms.ToArray();
        }


        private void bt_start_server_Click(object sender, EventArgs e)
        {
            TCPServer obj_server = new TCPServer();
            System.Threading.Thread obj_thread = new

            System.Threading.Thread(obj_server.StartServer);
            obj_thread.Start();
        }



        class TCPServer
        {
            TcpListener obj_server;
            public TCPServer()
            {
                obj_server = new TcpListener(IPAddress.Any, 6868);
            }

            public void StartServer()
            {
                obj_server.Start();
                while (true)
                {
                    TcpClient tc = obj_server.AcceptTcpClient();
                    SocketHandler obj_handler = new SocketHandler(tc);
                    System.Threading.Thread obj_thread = new System.Threading.Thread(obj_handler.ProcessSocketRequest);
                    obj_thread.Start();
                }
            }

            class SocketHandler
            {
                NetworkStream ns;
                public SocketHandler(TcpClient tc)
                {
                    ns = tc.GetStream();
                }

                public void ProcessSocketRequest()
                {
                    FileStream fs = null;
                    long current_file_pointer = 0;
                    Boolean loop_break = false;
                    while (true)
                    {
                        if (ns.ReadByte() == 2)
                        {
                            byte[] cmd_buff = new byte[3];
                            ns.Read(cmd_buff, 0, cmd_buff.Length);
                            byte[] recv_data = ReadStream();
                            switch (Convert.ToInt32(Encoding.UTF8.GetString(cmd_buff)))
                            {
                                case 125:
                                    {
                                        fs = new FileStream(@"C:\tl\" + Encoding.UTF8.GetString(recv_data), FileMode.CreateNew);
                                        byte[] data_to_send = CreateDataPacket(Encoding.UTF8.GetBytes("126"),


                                            Encoding.UTF8.GetBytes(Convert.ToString(current_file_pointer)));
                                        ns.Write(data_to_send, 0,data_to_send.Length);
                                        ns.Flush();
                                    }
                                    break;

                                case 127:
                                    {
                                        fs.Seek(current_file_pointer,SeekOrigin.Begin);
                                        fs.Write(recv_data, 0,recv_data.Length);
                                        current_file_pointer = fs.Position;
                                        byte[] data_to_send = CreateDataPacket(Encoding.UTF8.GetBytes("126"),


                                            Encoding.UTF8.GetBytes(Convert.ToString(current_file_pointer)));
                                        ns.Write(data_to_send, 0,data_to_send.Length);
                                        ns.Flush();
                                    }
                                    break;

                                case 128:
                                    {
                                        fs.Close();
                                        loop_break = true;
                                    }
                                    break;

                                default:
                                    break;
                            }
                        }
                        if (loop_break == true)
                        {
                            ns.Close();
                            break;
                        }
                    }
                }

                public byte[] ReadStream()
                {
                    byte[] data_buff = null;

                    int b = 0;
                    String buff_length = "";
                    while ((b = ns.ReadByte()) != 4)
                    {
                        buff_length += (char)b;
                    }
                    int data_length = Convert.ToInt32(buff_length);
                    data_buff = new byte[data_length];
                    int byte_read = 0;
                    int byte_offset = 0;
                    while (byte_offset < data_length)
                    {
                        byte_read = ns.Read(data_buff, byte_offset,data_length - byte_offset);
                        byte_offset += byte_read;
                    }

                    return data_buff;
                }

                private byte[] CreateDataPacket(byte[] cmd, byte[] data)
                {
                    byte[] initialize = new byte[1];
                    initialize[0] = 2;
                    byte[] separator = new byte[1];
                    separator[0] = 4;
                    byte[] datalength = Encoding.UTF8.GetBytes(Convert.ToString(data.Length));
                    MemoryStream ms = new MemoryStream();
                    ms.Write(initialize, 0, initialize.Length);
                    ms.Write(cmd, 0, cmd.Length);
                    ms.Write(datalength, 0, datalength.Length);
                    ms.Write(separator, 0, separator.Length);
                    ms.Write(data, 0, data.Length);
                    return ms.ToArray();
                }
            }

        }
    }
}

我认为可以使用“ TCPserver”类来接收数据。

所以有人可以帮助我或指导我如何在android中做到这一点。

我已成功将消息从Android发送到PC。我还从Java桌面应用程序向.net桌面应用程序发送了一些文本字符串,这很容易。 但是卡在这里。

我的主要任务是将视频文件从PC发送到设备并在设备上播放。

任何帮助或指导表示赞赏。 让我知道是否有人需要更多信息。 谢谢。

0 个答案:

没有答案