예제 #1
0
 private void buttonAdd_Click_Goat(object sender, EventArgs e)
 {
     if (openFileDialog.ShowDialog() == DialogResult.OK)
     {
         button3.Enabled = resetButton.Enabled = false;
         foreach (string url in openFileDialog.FileNames)
         {
             progressBar.Value = 0;
             Goat goat = Goat.load(url);
             animals.Add(goat);
             progressBar.PerformStep();
             listBox1.Items.Add("id= " + goat.getId().ToString());
             progressBar.PerformStep();
             loadGoat(goat);
         }
         MessageBox.Show("load complete");
         button3.Enabled    = resetButton.Enabled = true;
         this.ActiveControl = this.enterDataControl;
         txtButton.Enabled  = wordButton.Enabled = excelButton.Enabled = true;
     }
 }
예제 #2
0
        private void exchangeInfo(Object conn)
        {
            Socket        connection = conn as Socket;
            DirectoryInfo folder     = new DirectoryInfo(this.fileDirectory);

            byte[] bytes = { Convert.ToByte(folder.GetFiles("*.iga").Count()) };
            connection.Send(bytes);
            try
            {
                foreach (FileInfo file in folder.GetFiles("*.iga"))
                {
                    bytes = Encoding.UTF8.GetBytes(file.Name);
                    byte[] length = BitConverter.GetBytes(bytes.Length);
                    connection.Send(length);
                    connection.Send(bytes);
                }
            } catch
            {
                richTextBox.AppendText(GetCurrentTime() + ": client: " + connection.RemoteEndPoint.ToString() + " close connection");
                clientDictionary.Remove(connection.RemoteEndPoint.ToString());
                listBox1.Items.Remove(connection.RemoteEndPoint.ToString());
            }

            while (true)
            {
                byte[] buffer = new byte[4];
                try
                {
                    connection.Receive(buffer);
                    int    index    = BitConverter.ToInt32(buffer, 0);
                    string filename = folder.GetFiles("*.iga")[index].FullName;
                    this.richTextBox.AppendText(GetCurrentTime() + ": request file: " + filename + "\n");
                    Goat goat = null;
                    try
                    {
                        goat = Goat.load(filename);
                    }
                    catch (Exception)
                    {
                        buffer = BitConverter.GetBytes(false);
                        connection.Send(buffer);
                        continue;
                    }
                    buffer = BitConverter.GetBytes(true);
                    connection.Send(buffer);
                    List <string> imgList     = new List <string>(goat.imgUrl);
                    string        datetimeStr = (DateTime.Now.Subtract(DateTime.Parse("2018-1-1"))).TotalMilliseconds.ToString().Split('.')[0];
                    for (int i = 0; i < goat.imgUrl.Count; i++)
                    {
                        goat.imgUrl[i] = @"cache/picture/" + datetimeStr + i + "." + goat.imgUrl[i].Split('.').Last();
                    }
                    buffer = serialize(goat);
                    byte[] animalLength = BitConverter.GetBytes(buffer.Length);
                    connection.Send(animalLength);
                    connection.Send(buffer);
                    buffer = BitConverter.GetBytes(imgList.Count);
                    connection.Send(buffer);

                    for (int i = 0; i < imgList.Count; i++)
                    {
                        byte[] length;
                        if (!File.Exists(imgList[i]))
                        {
                            richTextBox.AppendText(GetCurrentTime() + ": Warn: " + imgList[i] + " doesn't exist! \n");
                            length = BitConverter.GetBytes(0L);
                            connection.Send(length, 8, 0);
                            continue;
                        }
                        FileInfo fileInfo = new FileInfo(imgList[i]);
                        length = BitConverter.GetBytes(fileInfo.Length);
                        byte[] name       = Encoding.UTF8.GetBytes(goat.imgUrl[i]);
                        byte[] nameLength = BitConverter.GetBytes(name.Length);
                        connection.SendFile(imgList[i], length, nameLength, 0);
                        connection.Send(name);
                        richTextBox.AppendText(GetCurrentTime() + ": send image: " + imgList[i] + "\t...\n");
                        Thread.Sleep(100);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    break;
                }
            }
        }