public void Insert(ShortMessage msg) { reader.Close(); String contactPhoneNumber;//对方联系人 //sql = @"insert into [ShortMessage] (Time) values('"+msg.GetTime().ToString()+"')"; if (msg.GetContent().Length < 70)//如果长度小于70字 { sql = @"insert into ShortMessage(Owner, SendNumber, ReceiveNumber, Time, Content) values('" + Owner + "' , '" + msg.GetSendNumber() + "','" + msg.GetReceiveNumber() + "','" + msg.GetTime().ToString("yyyy/MM/dd HH:mm:ss") + "','" + msg.GetContent() + "')"; cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); } else { int length = 70; int temp; for (int j = 0; j *length < msg.GetContent().Length; j++) { temp = j * length; if (msg.GetContent().Length - j * length < length) { sql = @"insert into ShortMessage(Owner, SendNumber, ReceiveNumber, Time, Content) values('" + Owner + "' , '" + msg.GetSendNumber() + "','" + msg.GetReceiveNumber() + "','" + msg.GetTime().ToString("yyyy/MM/dd HH:mm:ss") + "','" + " (" + (j + 1) + ") " + msg.GetContent().Substring(j * length, msg.GetContent().Length - j * length) + "')"; cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); } else { sql = @"insert into ShortMessage(Owner, SendNumber, ReceiveNumber, Time, Content) values('" + Owner + "' , '" + msg.GetSendNumber() + "','" + msg.GetReceiveNumber() + "','" + msg.GetTime().ToString("yyyy/MM/dd HH:mm:ss") + "','" + " (" + (j + 1) + ") " + msg.GetContent().Substring(j * length, 70) + "')"; cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); } } } if (msg.GetReceiveNumber() != Owner)//如果收件人不是我 { contactPhoneNumber = msg.GetReceiveNumber(); } else { contactPhoneNumber = msg.GetSendNumber(); } AddContact(contactPhoneNumber); }
private void showMessage() { this.Messagebox.Items.Clear(); messageList = mainForm.openForm.client.MyDBController.GetMessageListByPhoneNumber(this.PhoneNumber); if (messageList.Count == 0) { } for (int i = 0; i < messageList.Count; i++) { ShortMessage msg = (ShortMessage)messageList[i]; //if(msg.GetContent().Length<999)//如果长度小于70字 //{ Messagebox.Items.Add("发送者:" + msg.GetSendNumber() + " 接收者:" + msg.GetReceiveNumber() + " 时间:" + msg.GetTime() + " 内容:" + msg.GetContent()); //} //else //{ ///MessageBox.Show("信息过长,无法显示!"); //} //else //{ // int length = 70; // int temp; // for (int j = 0; j*length < msg.GetContent().Length; j++) // { // temp = j * length; // if (msg.GetContent().Length - j * length <length) // Messagebox.Items.Add("(" + (j + 1) + ")" + "发送者:" + msg.GetSendNumber() + " 接收者:" + msg.GetReceiveNumber() + " 时间:" + msg.GetTime() + " 内容:" + msg.GetContent().Substring(j * length, msg.GetContent().Length - j*length)); // else // Messagebox.Items.Add("(" + (j+1) + ")" + "发送者:" + msg.GetSendNumber() + " 接收者:" + msg.GetReceiveNumber() + " 时间:" + msg.GetTime() + " 内容:" + msg.GetContent().Substring(j*length, 70)); // } //} } //SendMessage(Messagebox.Handle, SB_BOTTOM, SB_LINEDOWN, 0); //Messagebox.SelectedIndex = Messagebox.Items.Count - 1; //Messagebox.ClearSelected(); }
private Byte[] ConvertShortMessageToByte(ShortMessage shortMessage) { String Message; Message = (int)shortMessage.GetId() + " " + shortMessage.GetSendNumber() + " " + shortMessage.GetReceiveNumber() + " " + shortMessage.GetTime().ToString("yyyy/MM/dd HH:mm:ss") + " " + shortMessage.GetContent(); Byte[] data = new byte[100000]; data = System.Text.Encoding.UTF8.GetBytes(Message + '\0'); return(data); }
public void Receive() { //System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, // new DeleFunc(Func)); //接收服务器的信息 while (ShutDown == 0) { Byte[] Data = new byte[100000]; int Len; try { Len = stream.Read(Data, 0, Data.Length); } catch (Exception e) { messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("服务器崩溃"); }); System.Threading.Thread.Sleep(3000); Environment.Exit(0); //messageBox.Show("服务器崩溃"); break; } String ResponseData = System.Text.Encoding.UTF8.GetString(Data, 0, Len); string[] cmd = ResponseData.Split(' '); if (cmd[0] == "Info")//如果读到服务器回执 id(string) (SUCCESS FAIL NOREGISTER NOTONLINE DELAY) { if (cmd[2] == "SUCCESS") { messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("你发送给 " + cmd[1] + " 的短信已经安全送达"); }); } if (cmd[2] == "FAIL") { messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("发送失败"); }); //messageBox.Show("发送失败"); } if (cmd[2] == "NOREGISTER") { //openForm.mainForm.dialogForm.StatusBar.Text = "发送号码为空号"; messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("发送号码为空号"); }); //messageBox.Show("发送号码为空号"); } if (cmd[2] == "NOTONLINE") { messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("向 " + cmd[1] + " 发送的短信因对方已经关机尚未送达"); }); //messageBox.Show("对方已关机"); } if (cmd[2] == "DELAY") { messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("发送给 " + cmd[1] + " 的短信经过延迟已经安全送达"); }); //messageBox.Show("已设置定时发送"); } //if (cmd[2] == "ILLEGAL") //{ // messageBox.Dispatcher.Invoke((Action)delegate { messageBox.Show("发送成功"); }); // messageBox.Show("禁止重复登录!"); // this.openForm.IsLegal = 0; //} if (cmd[2] == "LEGAL") { //openForm.OpenMainForm(); this.openForm.IsLegal = 1; } //else } else { ShortMessage receiveMessage = new ShortMessage(); receiveMessage = ConvertByteToShortMessage(Data, Len); MyDBController.Insert(receiveMessage); messageBox.Dispatcher.Invoke((Action) delegate { messageBox.Show("您有新短消息! 发送者:" + receiveMessage.GetSendNumber() + " 接收者:" + receiveMessage.GetReceiveNumber() + " 时间:" + receiveMessage.GetTime() + " 内容:" + receiveMessage.GetContent()); }); //Dispatcher.Invoke((Action)delegate //{ openForm.Dispatcher.Invoke((Action) delegate { openForm.mainForm.UpdateMainForm(); try { openForm.mainForm.dialogForm.UpdateDialogForm(); } catch { } }); //}) } } //messageBox.Show("hehe"); recvThread.Abort(); }
public int GetMessageId(ShortMessage shortMessage) { int result = -1; sql = @"select Id from ShortMessage where Owner = '" + Owner + " 'and SendNumber = '" + shortMessage.GetSendNumber() + "' and ReceiveNumber = '" + shortMessage.GetReceiveNumber() + "' and Time = '" + shortMessage.GetTime().ToString("yyyy/MM/dd HH:mm:ss") + "' and Content = '" + shortMessage.GetContent() + "'"; cmd = new SqlCommand(sql, conn); reader = cmd.ExecuteReader(); while (reader.Read()) { result = (int)reader[0]; reader.Close(); return(result); } return(result); }