Exemplo n.º 1
0
 private void sendDatagram(Datagram data, IPAddress dest)
 {
     client.Send(data.Data, data.Data.Length, new IPEndPoint(dest, 21621));
     Invoke(MessageHandler, data, new IPEndPoint(localaddress, 21621));
 }
Exemplo n.º 2
0
        private void handleMessage(Datagram data, IPEndPoint source)
        {
            int pos = 1;

            try
            {
                switch (data.Data[0])
                {
                case 0:
                    string sender  = data.ReadString(ref pos);
                    string message = data.ReadString(ref pos);
                    userip(sender, source.Address.ToString());
                    textBox2.AppendText("\r\n" + listView1.Items[ipdict[source.Address.ToString()]].SubItems[2].Text + ":\"" + sender + "\": " + message);
                    if (WindowState == FormWindowState.Minimized)
                    {
                        notifyIcon1.ShowBalloonTip(100, sender, message, ToolTipIcon.None);
                    }
                    break;

                case 1:
                    userip(data.ReadString(ref pos), source.Address.ToString());
                    break;

                case 2:
                    string name     = data.ReadString(ref pos);
                    long   size     = data.Readlong(ref pos);
                    string sharerid = ipdict[source.Address.ToString()].ToString();
                    if (sharedfiles.ContainsKey(sharerid + ":" + name))
                    {
                        sharedfiles[sharerid + ":" + name] = size;
                    }
                    else
                    {
                        ListViewItem item = new ListViewItem(new string[] { name, sharerid, getFileSize(size), source.Address.ToString() });
                        listView2.Items.Insert(sharedfiles.Count, item);
                        sharedfiles.Add(sharerid + ":" + name, size);
                    }
                    break;

                case 3:
                    string filename = data.ReadString(ref pos);
                    int    filepart = data.ReadInt(ref pos);
                    data.ID = 4;
                    data.AppendBytes(pubfiledata[filename][filepart]);
                    Invoke(MessageUnicastSender, data, source.Address);
                    break;

                case 4:
                    if (rcvfile == data.ReadString(ref pos))
                    {
                        if (rcvnextpart == data.ReadInt(ref pos))
                        {
                            Rerequester.Change(int.MaxValue, int.MaxValue);
                            if (rcvnextpart < rcvpart)
                            {
                                Array.Copy(data.ReadBytes(ref pos, 32 * 1024), 0, rcvdata, rcvnextpart * 32 * 1024, 32 * 1024);
                                rcvnextpart++;
                                DownloadProgress();
                                Rerequester.Change(100, int.MaxValue);
                                requestnextpart();
                            }
                            else
                            {
                                Array.Copy(data.ReadBytes(ref pos, rcvdata.Length % (32 * 1024)), 0, rcvdata, rcvnextpart * 32 * 1024, rcvdata.Length % (32 * 1024));

                                FileStream outfile = File.OpenWrite(saveFileDialog1.FileName);
                                button2.Text = "Schreibe Daten";
                                outfile.Write(rcvdata, 0, rcvdata.Length);
                                outfile.Close();
                                MessageBox.Show("Dateidownload Fertig!\r\n" + rcvfile, "Download Abgeschlossen", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                button2.Text    = "Download";
                                button2.Enabled = true;
                            }
                        }
                    }
                    break;
                }
            }
            catch { }
        }
Exemplo n.º 3
0
 private void sendDatagram(Datagram data)
 {
     sendDatagram(data, IPAddress.Broadcast);
 }