//傳送檔案 private void btnSendFile_Click(object sender, EventArgs e) { string SelectFriend = GetSelectClient(); if (SelectFriend.Length == 0) { return; } string fileName = txtFilePath.Text; if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("請選擇檔案"); return; } //將檔案打包成mod MessageMod mod = new MessageMod(); using (FileStream fs = new FileStream(fileName, FileMode.Open)) { byte [] byts = new byte [1024 * 1024 * 2]; fs.Read(byts, 0, (int)fs.Length); mod.ContentBytes = byts; } mod.MsgType = (int)Common.PubClass.MsgType.Client2ClientFile; mod.FromUser = localName; mod.ToUser = SelectFriend; mod.Content = fileName; socketClient.Send(mod.ToBytes()); //傳送出去 }
// 傳送訊息 private void btnSendMsg_Click(object sender, EventArgs e) { string SelectFriend = GetSelectClient(); if (SelectFriend.Length == 0) { return; } if (txtMessage.Text.Length == 0) { return; } if (receiveFlag) { MessageMod mod = new MessageMod(); mod.MsgType = (int)Common.PubClass.MsgType.Client2Client; mod.FromUser = localName; mod.ToUser = SelectFriend; mod.Content = this.txtMessage.Text; socketClient.Send(mod.ToBytes()); txtReceived.AppendTxt(string.Format("{0}:{1}", mod.FromUser, mod.Content)); this.txtMessage.Text = ""; } else { MessageBox.Show("伺服器停止回應"); } }
private void SendFrientToAll() { if (dictClients.Count < 1) { return; } string clients = ""; foreach (KeyValuePair <string, Socket> item in dictClients) { clients += "^" + item.Key; //^IP:port //Debug.Print(clients); } clients = clients.Substring(1); //處理訊息,建立每個訊息的詳細資訊 MessageMod mod = new MessageMod(); mod.MsgType = (int)Common.PubClass.MsgType.RadioClients; mod.FromUser = socketServer.LocalEndPoint.ToString(); mod.ToUser = "******"; mod.Content = clients; mod.ContentBytes = new byte[1]; foreach (var item in dictClients) { item.Value.Send(mod.ToBytes()); //Value = socket } }
private void Server2ClientMsg(string toUser, string msg, bool isAll, Common.PubClass.MsgType type = Common.PubClass.MsgType.Server2ClientMsg) { //處理訊息,建立每個訊息的詳細資訊 MessageMod mod = new MessageMod(); mod.MsgType = (int)type; mod.FromUser = socketServer.LocalEndPoint.ToString(); mod.ToUser = toUser; mod.Content = msg; mod.ContentBytes = new byte[1]; byte[] bytes = mod.ToBytes(); txtServerState.AppendTxt("【" + txtSerName.Text + "】" + " 對 " + "【" + mod.ToUser + "】" + " 說:" + msg); foreach (KeyValuePair <string, Socket> item in dictClients) { if (isAll) //全體 { item.Value.Send(bytes); } else if (item.Key == toUser) { item.Value.Send(bytes); } } }
private void SendFile(MessageMod mod, bool isAll) { byte[] byts = mod.ToBytes(); foreach (KeyValuePair <string, Socket> item in dictClients) { if (isAll) { item.Value.Send(byts); } else if (item.Key == mod.ToUser) { item.Value.Send(byts); } } }
//叮咚 private void btnShake_Click(object sender, EventArgs e) { string SelectFriend = GetSelectClient(); if (SelectFriend.Length == 0) { return; } MessageMod mod = new MessageMod(); mod.MsgType = (int)Common.PubClass.MsgType.ShineScreen; mod.FromUser = localName; mod.ToUser = SelectFriend; mod.Content = "叮咚!有人在家嗎~~"; socketClient.Send(mod.ToBytes()); }
//叮咚 private void btnShake_Click(object sender, EventArgs e) { MessageMod mod = new MessageMod(); mod.MsgType = (int)Common.PubClass.MsgType.ShineScreen; mod.FromUser = localName; mod.FromUser_Name = userName; mod.ToUser = ""; mod.Content = "叮咚!有人在家嗎~~"; socketClient.Send(mod.ToBytes()); this.txtReceived.SelectionFont = new Font("微軟正黑體", 14, FontStyle.Bold); this.txtReceived.SelectionColor = Color.Red; this.txtReceived.AppendTxt("叮咚!有人在家嗎~~"); this.txtReceived.SelectionFont = new Font("微軟正黑體", 12, FontStyle.Regular); this.txtReceived.SelectionColor = Color.Black; }