Пример #1
0
        private string send_data_rev_data()
        {
            //string re = null;
            ////old file -> hashlist
            //List<string> md5_list = lib.Get_file_block_md5("F:\\Group_Project\\Meerkats\\desktop\\Meerkats_Win\\bin\\Debug\\sync_disk\\4",out re);

            ////new -> search
            //byte[] filedata= null;
            //differ_info_json_list diff_json = new differ_info_json_list();
            //lib.Search_block_index("F:\\Group_Project\\Meerkats\\desktop\\Meerkats_Win\\bin\\Debug\\sync_disk\\3", md5_list, diff_json, out filedata);

            ////old ->modifer
            //lib.Differ_modifer_file("F:\\Group_Project\\Meerkats\\desktop\\Meerkats_Win\\bin\\Debug\\sync_disk\\4", diff_json, filedata);

            SocketTCPClient t1 = new SocketTCPClient();

            //t1.KillEmptyDirectory(PATH);
            //t1.KillEmptyDirectory("F:\\Group_Project\\Meerkats\\desktop\\Meerkats_Win\bin\\Debug\\sync_disk\\dir2\\1\\2\\");
            t1.CreateInstance();

            //
            t1.SendMessage(Get_local_File_info(PATH));

            byte[] result = t1.ReceiveMessage();

            // get file cmd_flag < 6 operation >
            string avg_speed = t1.Check_cmd_flag(result);

            t1.DisconnectServer();
            return(avg_speed);
        }
        private void buttonConnectTcp_Click(object sender, EventArgs e)
        {
            if (this.clientTcp != null)
            {
                if (this.clientTcp.Connected)
                {
                    CloseTcpClient(ref this.clientTcp);
                    return;
                }

                this.clientTcp.Dispose();
                this.clientTcp = null;
            }
            if (this.m_curTcpServer == null)
            {
                MessageBox.Show("Please select right host.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            IPAddress tcpIP = IPAddress.Parse(this.m_curTcpServer.ServerIP);

            this.clientTcp = new SocketTCPClient(new IPEndPoint(tcpIP, this.m_curTcpServer.ServerPort));
            try
            {
                textBoxTCPTips.AppendText("new client connect...\r\n");
                clientTcp.ConnectServer(ReceiveDataCallback);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connect Filed!");
                this.clientTcp.Dispose();
                this.clientTcp = null;
                return;
            }
        }
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         string[] str = { "测试字符串a", "abc", "def" };
         SocketTCPClient.SendMessage(SocketTCPClient.BuildDataPackage(1, 2, 3, 4, 5, str));
         //string[] str2 = { "我是与1同时发送的测试字符串2,请注意我是否与其他信息粘包", "test2", "test22" };
         //SocketTCPClient.SendMessage(SocketTCPClient.BuildDataPackage(1, 6, 7, 8, 9, str2));
     }
 }
        public SocketTcpClientCommunication()
        {
            _macDic = new Dictionary <string, long>();

            _socketClient                 = new SocketTCPClient();
            _socketClient.OnAccept       += OnAccept;
            _socketClient.OnConnectClose += OnConnectClose;
            _socketClient.OnDataArrive   += OnDataArrive;
            _socketClient.OnError        += OnError;
        }
 private void CloseTcpClient(ref SocketTCPClient client)
 {
     if (client != null)
     {
         // 断开连接
         client.DisconnectServer((error) =>
         {
             if (error.ErrorCode != ClientErrorCode.None)
             {
                 this.textBoxTCPTips.AppendText($"{error.ErrorStr}\r\n");
             }
         });
     }
     client = null;
     ConnectStatus(false);
 }
        private void TCP_IPMenuForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (clientTcp != null)
            {
                clientTcp.DisconnectServer((error) =>
                {
                    if (error.ErrorCode != ClientErrorCode.None)
                    {
                        textBoxTCPTips.AppendText($"{error.ErrorStr}\r\n");
                    }
                });
            }
            clientTcp = null;
            clientUdp = null;

            FormManager.Instance.BackClose();
        }
Пример #7
0
        public byte[] Get_local_File_info(string PATH)
        {
            DirectoryInfo dir = new DirectoryInfo(PATH);

            FileSystemInfo[] fsinfos = dir.GetFileSystemInfos();
            SocketTCPClient  t1      = new SocketTCPClient();

            List <file_info_json_pull> file_json = new List <file_info_json_pull>();

            //  traverse files and dirs
            int level = 0;

            listDirectory(PATH, PATH, level, file_json);

            /**
             * json file_info demo
             *
             *  [
             *      {
             *          "Name":"1.txt",
             *          "Typ":1,
             *          "Digest":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
             *      },
             *      {
             *          "Name":"2.txt",
             *          "Typ":1,
             *          "Digest":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
             *      }
             *  ]
             *
             **/

            // Json serialize
            string MsgBody_str = JsonConvert.SerializeObject(file_json);

            byte[] MsgBody = System.Text.Encoding.Default.GetBytes(MsgBody_str);
            // encapsulate packets
            byte[] MessageBodyByte = new byte[MsgBody.Length + 30];
            MessageBodyByte = t1.BuildDataPackage_For_Pull(MsgBody, 0x2, Device_id);

            return(MessageBodyByte);
        }
 void Start()
 {
     SocketTCPServer.init(Ip, Port);           //开启并初始化服务器
     SocketTCPClient.CreateInstance(Ip, Port); //客户端连接服务器
 }
 void OnApplicationQuit()
 {
     SocketTCPClient.Close();
     SocketTCPServer.Close();
 }
Пример #10
0
        private string send_data_rev_data()
        {
            SocketTCPClient t1 = new SocketTCPClient();

            string testdata = null;

            /**
             * json file_info demo
             *
             *  [
             *      {
             *          "Name":"1.txt",
             *          "Typ":1,
             *          "Digest":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
             *      },
             *      {
             *          "Name":"2.txt",
             *          "Typ":1,
             *          "Digest":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
             *      }
             *  ]
             *
             **/
            // for text md5 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]


            // convert to json list
            // Typ = 0x1 => file
            // Typ = 0x2 => Directory

            byte[] md11 = t1.HexStrTobyte(t1.GetMD5HashFromFile("F:\\fortest\\1.txt"));
            byte[] md22 = t1.HexStrTobyte(t1.GetMD5HashFromFile("F:\\fortest\\2.txt"));

            List <byte> file_1_md5 = new List <byte>();

            for (int i = 0; i < 16; i++)
            {
                file_1_md5.Add(md11[i]);
            }

            List <byte> file_2_md5 = new List <byte>();

            for (int i = 0; i < 16; i++)
            {
                file_1_md5.Add(md22[i]);
            }


            List <file_info_json> testdemo = new List <file_info_json>()
            {
                new file_info_json()
                {
                    Name   = "1.txt",
                    Typ    = 0x1,
                    Digest = file_1_md5
                },

                new file_info_json()
                {
                    Name   = "2.txt",
                    Typ    = 0x1,
                    Digest = file_2_md5
                }
            };

            // Json serialize
            testdata = JsonConvert.SerializeObject(testdemo);

            t1.CreateInstance();

            //byte[] test = t1.HexStrTobyte(testdata);

            byte[] test            = System.Text.Encoding.Default.GetBytes(testdata);
            byte[] MessageBodyByte = new byte[test.Length + 30];

            MessageBodyByte = t1.BuildDataPackage_For_Pull(test, 0x2, Device_id);


            t1.SendMessage(MessageBodyByte);
            byte[] result = t1.ReceiveMessage();

            List <string> file_name = t1.Check_If_Upload(result);

            string status_file = null;

            if (file_name.Count != 0)
            {
                status_file = t1.Upload_File(file_name);
            }

            // string result_str = System.Text.Encoding.Default.GetString(result);
            // fortest.Text = result_str;


            // test for download
            byte[] test_for_download            = null;
            byte[] MessageBodyByte_for_download = new byte[30];
            MessageBodyByte_for_download = t1.BuildDataPackage_For_Pull(test_for_download, 0x21, Device_id);
            t1.SendMessage(MessageBodyByte_for_download);

            string str_status_1 = t1.ReceiveMessage_For_download();
            string str_status_2 = t1.ReceiveMessage_For_download();

            return(status_file + " + " + str_status_1 + " + " + str_status_2);
        }