public void SendTo() { FileStream fs = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read); BinaryReader read = new BinaryReader(fs); FileInfo fi = new FileInfo(textBox4.Text); int count = (int)(fi.Length / (1024 * 3)) + 1; NameSize ns = new NameSize(); ns.name = fi.Name; ns.Type = (int)PacketType.이름과사이즈; Packet.Serialize(ns).CopyTo(this.sendBuffer, 0); this.Client_Send(); progressBar1.Maximum = (int)fi.Length; for (int i = 0; i < count; i++) { DataSend ds = new DataSend(); ds.data = read.ReadBytes(1024 * 3); ds.Type = (int)PacketType.일; progressBar1.Value += progressBar1.Maximum / count; Packet.Serialize(ds).CopyTo(this.sendBuffer, 0); this.Client_Send(); } progressBar1.Value = 0; }
public void Receive() { while (true) { m_NetStream.Read(readBuffer, 0, readBuffer.Length); Packet packet = (Packet)Packet.Deserialize(this.readBuffer); switch ((int)packet.Type) { case (int)PacketType.이름과사이즈: { ListViewItem item; this.m_NameSize = (NameSize)Packet.Deserialize(this.readBuffer); this.Invoke(new MethodInvoker(delegate() { item = listView1.Items.Add(m_NameSize.name); item.SubItems.Add(m_NameSize.size.ToString()); })); break; } case (int)PacketType.일이름: { this.m_DataName = (DataName)Packet.Deserialize(this.readBuffer); break; } case (int)PacketType.일: { this.m_DataSend = (DataSend)Packet.Deserialize(this.readBuffer); FileStream fs = new FileStream(textBox3.Text + "\\" + m_DataName.name, FileMode.Append, FileAccess.Write); fs.Write(m_DataSend.data, 0, m_DataSend.data.Length); this.Invoke(new MethodInvoker(delegate() { progressBar1.Value += (int)(progressBar1.Maximum / ((progressBar1.Maximum / (1024 * 3)) + 1)); })); fs.Close(); if (m_DataSend.data.Length < 1024 * 3) { this.Invoke(new MethodInvoker(delegate() { progressBar1.Value = 0; })); } break; } } } }
public void RUN() { try { this.m_Listener = new TcpListener(Convert.ToInt32(textBox2.Text)); this.m_Listener.Start(); } catch { MessageBox.Show("포트번호를 입력하세요"); } if (!this.m_blsClientOn) { this.Invoke(new MethodInvoker(delegate() { this.textBox4.AppendText("Server : Start \r\n"); this.textBox4.AppendText("Storage Path : " + textBox3.Text); this.textBox4.AppendText("\r\nwaiting for client access..."); })); } Socket Client = this.m_Listener.AcceptSocket(); if (Client.Connected) { this.m_blsClientOn = true; this.m_NetStream = new NetworkStream(Client); this.Invoke(new MethodInvoker(delegate() { this.textBox4.AppendText("\r\nClient is connected"); })); m_DataSendT = new Thread(new ThreadStart(Receive)); m_DataSendT.Start(); NameSize ns = new NameSize(); DirectoryInfo di; di = new DirectoryInfo(textBox3.Text); FileInfo[] fiArray; fiArray = di.GetFiles(); foreach (FileInfo fis in fiArray) { ns.name = fis.Name; ns.size = fis.Length; Packet.Serialize(ns).CopyTo(this.sendBuffer, 0); this.Send(); } } else if (!Client.Connected) { this.m_Listener.Stop(); this.m_Thread.Abort(); this.m_NetStream.Close(); } }