//接受文件 public static void File_Receive(Socket client_socket, Chat_Window chat_window, string recv_filename) { SaveFileDialog file_dlg = new SaveFileDialog(); file_dlg.InitialDirectory = chat_window.datapath; file_dlg.FileName = recv_filename; file_dlg.Filter = "(*." + recv_filename.Split('.')[recv_filename.Split('.').Length - 1] + ")|*." + recv_filename.Split('.')[recv_filename.Split('.').Length - 1]; if (file_dlg.ShowDialog() == DialogResult.OK) { string recv_filepath = file_dlg.FileName; FileStream source_stream = new FileStream(recv_filepath, FileMode.Create, FileAccess.Write); byte[] file_bytes = new byte[1024]; int packege_length = file_bytes.Length; int total_length = 0; string send_msg = "fileaccept." + chat_window.user_name[0]; Connect_Send(chat_window.socket_to_friend[chat_window.file_requestuser], send_msg); while (packege_length == 1024) { packege_length = client_socket.Receive(file_bytes, 0, 1024, SocketFlags.None); source_stream.Write(file_bytes, 0, packege_length);//写入文件 source_stream.Flush(); total_length = packege_length + total_length; Console.WriteLine("{0:G}", total_length); } source_stream.Close(); } MessageBox.Show(chat_window, "接受文件成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); string file_confim = "info." + chat_window.user_name[0] + " ." + DateTime.Now.ToString() + "\n" + "File receive successfully\n"; Connect_Send(client_socket, file_confim); Chat_Receive(client_socket, chat_window, true); }
private void Connect_Click(object sender, RoutedEventArgs e) { client = new TcpClient(); IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse(IP.Text), int.Parse(Port.Text)); Status.Text = "Waiting"; try { client.Connect(IP_End); if (client.Connected) { Status.Text = "Connected"; Chat_Window.AppendText("Connected" + "\n"); writer = new StreamWriter(client.GetStream()); reader = new StreamReader(client.GetStream()); writer.AutoFlush = true; myGetWorker.RunWorkerAsync(); mySendWorker.WorkerSupportsCancellation = true; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); Status.Text = "Error"; } }
private void Send_Click(object sender, RoutedEventArgs e) { if (Message.Text != "") { message = Message.Text; mySendWorker.RunWorkerAsync(); } Message.Text = ""; Chat_Window.ScrollToEnd(); }
private void backgroundWorkerSend_DoWork(object sender, DoWorkEventArgs e) { if (client.Connected) { message = your_nickname + ": " + message; writer.WriteLine(message); this.Message.Dispatcher.Invoke((delegate() { Chat_Window.AppendText(message + "\n"); })); } else { MessageBox.Show("Send failed"); } mySendWorker.CancelAsync(); }
private void backgroundWorkerGet_DoWork(object sender, DoWorkEventArgs e) { while (client.Connected) { try { receive = reader.ReadLine(); // receive = your_nickname + ": " + receive; this.Message.Dispatcher.Invoke((delegate() { Chat_Window.AppendText(receive + "\n"); })); receive = ""; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } }
//聊天消息接受 public static void Chat_Receive(Socket client_socket, Chat_Window chat_window, bool chat_state) { ChatObject ss = new ChatObject(); ss.client_socket = client_socket; ss.chat_window = chat_window; ss.chat_state = chat_state; try { //开始接收消息 if (chat_state) { client_socket.BeginReceive(recv_bytes, 0, recv_bytes.Length, SocketFlags.None, new AsyncCallback(Chat_Receive_Callback), ss); } } catch (Exception ex) { Console.WriteLine("异常信息:", ex.Message); } }
public static void Voice_Receive(Socket work_socket, WaveOut recv_stream, BufferedWaveProvider provider, Chat_Window chat_Window) { try { VoiceObject v_obj = new VoiceObject(); v_obj.work_socket = work_socket; v_obj.recieved_stream = recv_stream; v_obj.wave_provider = provider; v_obj.chat_Window = chat_Window; work_socket.BeginReceive(voice_buffer, 0, 5242880, SocketFlags.None, Voice_Receive_Callback, v_obj); } catch (Exception ex) { Console.WriteLine("异常信息:", ex.Message); } }