即时通讯系统源代码.wps

上传人:qwe****56 文档编号:80598777 上传时间:2023-03-23 格式:WPS 页数:117 大小:894.96KB
返回 下载 相关 举报
即时通讯系统源代码.wps_第1页
第1页 / 共117页
即时通讯系统源代码.wps_第2页
第2页 / 共117页
点击查看更多>>
资源描述

《即时通讯系统源代码.wps》由会员分享,可在线阅读,更多相关《即时通讯系统源代码.wps(117页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、 代码代码一、客户端主要代码一、客户端主要代码1.Communicationusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Threading;using System.Net;using ClientInfo.core;using System.Xml.Serialization;using System.IO;using System.Xml;namespace ClientInfo.core pub

2、lic class MessageNotifyEventArgs:EventArgs public MessageNotifyEventArgs(Message message)this.message=message;private Message message;public Message Message get return this.message;public class Communication public static Socket socket;private Thread receiveThread;private Thread receiveTextThread;pu

3、blic event EventHandler MessageNotify;/public delegate void MessageNotifyEventArgs(object sender,MessageNotifyEventArgs e);private bool connected;public bool Connected get return connected;private bool initialized=false;/连接 public void Connect(string server,int port)if(!connected)socket=new Socket(A

4、ddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);socket.Connect(new IPEndPoint(IPAddress.Parse(server),port);this.connected=true;/验证 private static ServerSendInfo ui;public static ServerSendInfo Ui get return ui;set ui=value;private static string userLName;public static string UserLName

5、 get return userLName;set userLName=value;public void Authorizate(UserInfo message)if(connected)SendMessage(message);string rece=ReceiveMessage();if(rece.Substring(0,2)=ON)throw new Exception(输入的账号或者密码错误,请重新输入!);else if(rece.Substring(0,2)=OK)this.initialized=true;this.StartReceiveMessage();XmlSeria

6、lizer xs=new XmlSerializer(typeof(ServerSendInfo);using(StringReader reader=new StringReader(rece.Substring(2)ui=(ServerSendInfo)xs.Deserialize(reader);userLName=ui.UserRealName;else if(rece.Substring(0,2)=RE)throw new Exception(重复登录:输入的账号已经登录!);private void StartReceiveMessage()receiveThread=new Th

7、read(ProcessServiceMessage);receiveThread.IsBackground=true;receiveThread.Start();/receiveTextThread=new Thread(ParameterizedThreadStart();/receiveTextThread.IsBackground=true;/receiveThread.Start();private void ProcessServiceMessage()while(true)string cmdM=ReceiveMessage();SendMessageNotify(cmdM);p

8、rivate void SendMessageNotify(string cmdM)XmlSerializer xs=new XmlSerializer(typeof(UserMessage);switch(cmdM.Substring(0,4)case TextMessage.CMDNAME:xs=new XmlSerializer(typeof(TextMessage);break;Message message;using(StringReader reader=new StringReader(cmdM.Substring(5)message=(Message)xs.Deseriali

9、ze(reader);if(this.MessageNotify!=null)this.MessageNotify(this,new MessageNotifyEventArgs(message);public string ReceiveMessage()byte msgBuffer=new byte200000;socket.Receive(msgBuffer);string msg=Encoding.UTF8.GetString(msgBuffer).TrimEnd(0);return msg;private void SendMessage(UserInfo message)strin

10、g msg=TransfromMessage(message);socket.Send(Encoding.UTF8.GetBytes(msg);private string TransfromMessage(UserInfo message)StringBuilder sb=new StringBuilder(200000);sb.AppendFormat(0:,message.CommandName);using(XmlWriter writer=XmlWriter.Create(sb)XmlSerializer xs=new XmlSerializer(message.GetType();

11、xs.Serialize(writer,message);return sb.ToString();public void SendTextMessage(TextMessage message)string msg=TransfromTextMessage(message);socket.Send(Encoding.UTF8.GetBytes(msg);public string TransfromTextMessage(TextMessage message)StringBuilder sb=new StringBuilder(200000);sb.AppendFormat(TEXT:);

12、using(XmlWriter writer=XmlWriter.Create(sb)XmlSerializer xs=new XmlSerializer(message.GetType();xs.Serialize(writer,message);return sb.ToString();2.DeptInfousing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ClientInfo class DeptInfo public DeptInfo()public De

13、ptInfo(string deptName,string deptDescription)/this.deptId=deptId;this.deptName=deptName;this.deptDescription=deptDescription;private string deptName;public string DeptName get return deptName;set deptName=value;private string deptDescription;public string DeptDescription get return deptDescription;

14、set deptDescription=value;public override string ToString()return deptName;3.RecvServerMessageusing System;using System.Collections.Generic;using System.Linq;using System.Text;using ClientInfo.core;using System.Threading;using System.Xml.Serialization;using System.IO;namespace ClientInfo public clas

15、s RecvServerMessage private static TextMessage tmsgs=new TextMessage();public static TextMessage Tmsgs get return RecvServerMessage.tmsgs;set RecvServerMessage.tmsgs=value;public RecvServerMessage()public void RecvMessage()while(true)string recvM=RecvMsg();if(recvM!=string.Empty)switch(recvM.Substri

16、ng(0,3)case TEXT:/接受用户的消息 Thread thread=new Thread(new ParameterizedThreadStart(UserMessages);thread.IsBackground=true;thread.Start(recvM.Substring(5);break;case AUTH:/登陆的信息 break;/接收消息 private string RecvMsg()byte msgBuffer=new byte200000;Communication.socket.Receive(msgBuffer);string msg=Encoding.

17、UTF8.GetString(msgBuffer).TrimEnd(0);return msg;private void UserMessages(object obj)string userMessage=(string)obj;XmlSerializer xs=new XmlSerializer(typeof(TextMessage);TextMessage tmsg;using(StringReader reader=new StringReader(userMessage.Substring(5)tmsg=(TextMessage)xs.Deserialize(reader);tmsg

18、s.Body=tmsg.Body;tmsgs.NowTimes=tmsg.NowTimes;tmsgs.Replier=tmsg.Replier;tmsgs.Sender=tmsg.Sender;4.ServerSendInfousing System;using System.Collections.Generic;using System.Linq;using System.Text;using ClientInfo.core;namespace ClientInfo Serializable public class ServerSendInfo:UserInfo private str

19、ing ssiDeptName;public string SsiDeptName get return ssiDeptName;set ssiDeptName=value;private List ssiDeptNames;public List SsiDeptNames get return ssiDeptNames;set ssiDeptNames=value;private List ssiUserName;public List SsiUserName get return ssiUserName;set ssiUserName=value;private string sUserN

20、ame;public string SUserName get return sUserName;set sUserName=value;public ServerSendInfo()ssiUserName=new List();ssiDeptNames=new List();5.TextMessageusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ClientInfo.core public class Message public Message(stri

21、ng commandName)mandName=commandName;private string commandName;public string CommandName get return commandName;set commandName=value;public class TextMessage:Message public const string CMDNAME=TEXT;public TextMessage():base(CMDNAME)private string sender;/发送者 public string Sender get return sender;

22、set sender=value;private string replier;/接受者 public string Replier get return replier;set replier=value;private string body;/具体消息 public string Body get return body;set body=value;private string nowTimes;/发送时间 public string NowTimes get return nowTimes;set nowTimes=value;6.UserInfousing System;using

23、 System.Collections.Generic;using System.Linq;using System.Text;namespace ClientInfo.core public class UserInfo:UserMessage private string userName;public string UserName get return userName;set userName=value;private string userPassword;public string UserPassword get return userPassword;set userPas

24、sword=value;private string userNiceName;public string UserNiceName get return userNiceName;set userNiceName=value;private string userRealName;public string UserRealName get return userRealName;set userRealName=value;private string userSex;public string UserSex get return userSex;set userSex=value;pr

25、ivate string userDepart;public string UserDepart get return userDepart;set userDepart=value;private string userPhone;public string UserPhone get return userPhone;set userPhone=value;private string userEmail;public string UserEmail get return userEmail;set userEmail=value;private string userAddress;p

26、ublic string UserAddress get return userAddress;set userAddress=value;private string userInfo;public string UserInfo1 get return userInfo;set userInfo=value;private int imageIndex;public int ImageIndex get return imageIndex;set imageIndex=value;public UserInfo()public UserInfo(int imageIndex,string

27、userName,string userNiceName,string userRealName,string userSex,string userDepart,string userEmail,string userAddress,string userInfo)this.userName=userName;/this.userPassword=userPassword;this.userNiceName=userNiceName;this.userRealName=userRealName;this.userSex=userSex;this.userDepart=userDepart;t

28、his.imageIndex=imageIndex;this.userEmail=userEmail;this.userAddress=userAddress;this.userInfo=userInfo;public UserInfo(string userName,string userPassword)this.userName=userName;this.userPassword=userPassword;public override string ToString()return userName.ToString();7.UserInfoSourceAUTHusing Syste

29、m;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.IO;namespace ClientInfo.core public class UserInfoSourceAUTH private List userInfos=new List();private List deptInfos=new List();private XmlDocument doc=new XmlDocument();public UserInfoSourceAUTH()/

30、public XmlNode FindUserNode(string username)/查找用户节点 /string path=string.Format(/useruserName=0,username);/XmlNode userNode=GetUserNode().SelectSingleNode(path);/return userNode;/private DeptInfo deptInfo=new DeptInfo();/查找部门所有名字信息 /public List FindDept()/foreach(XmlNode deptInfoNode in doc.DocumentE

31、lement.ChildNodes)/DeptInfo deptInfo=new DeptInfo();/deptInfo.DeptName=deptInfoNode.ChildNodes0.InnerText;/deptInfo.DeptDescription=deptInfoNode.ChildNodes1.InnerText;/deptInfos.Add(deptInfo);/return deptInfos;/查找 public List FindAll()foreach(XmlNode userInfoNode in doc.DocumentElement.ChildNodes)Us

32、erInfo userInfo=new UserInfo();userInfo.UserName=userInfoNode.ChildNodes0.InnerText;userInfo.UserPassword=userInfoNode.ChildNodes1.InnerText;userInfos.Add(userInfo);return userInfos;/打开文件 private string fileName;public void Load(string fileName)this.fileName=fileName;if(File.Exists(fileName)doc.Load

33、(fileName);public void Add(UserInfo userInfo)if(userInfo=null)throw new AccessViolationException(userInfo);if(doc.DocumentElement=null)XmlDeclaration dec=doc.CreateXmlDeclaration(1.0,utf-8,yes);XmlElement root=doc.CreateElement(users);doc.AppendChild(dec);doc.AppendChild(root);doc.DocumentElement.Ap

34、pendChild(CreateContactNode(userInfo);doc.Save(fileName);private XmlNode CreateContactNode(UserInfo userInfo)XmlElement userNode=doc.CreateElement(user);XmlElement userNameNode=doc.CreateElement(userName);XmlElement userPasswordNode=doc.CreateElement(userPassword);XmlElement userRealNameNode=doc.Cre

35、ateElement(userRealName);XmlElement userSexNode=doc.CreateElement(userSex);XmlElement userDeptNode=doc.CreateElement(userDept);userNameNode.InnerText=userInfo.UserName;userPasswordNode.InnerText=userInfo.UserPassword;userRealNameNode.InnerText=userInfo.UserRealName;userSexNode.InnerText=userInfo.Use

36、rSex.ToString();userDeptNode.InnerText=userInfo.UserDepart;userNode.AppendChild(userNameNode);userNode.AppendChild(userPasswordNode);userNode.AppendChild(userRealNameNode);userNode.AppendChild(userSexNode);userNode.AppendChild(userDeptNode);return userNode;/public object FindDept()/throw new NotImpl

37、ementedException();/8.UserMessageusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ClientInfo.core public class UserMessage private string commandName=AUTH;public string CommandName get return commandName;set commandName=value;public UserMessage()public User

38、Message(string commandName)mandName=commandName;9.AboutFrom.Designernamespace ClientCommunication partial class AboutFrom /Required designer variable./private System.ComponentModel.IContainer components=null;/Clean up any resources being used./true if managed resources should be disposed;otherwise,f

39、alse.protected override void Dispose(bool disposing)if(disposing&(components!=null)components.Dispose();base.Dispose(disposing);#region Windows Form Designer generated code /Required method for Designer support-do not modify /the contents of this method with the code editor./private void InitializeC

40、omponent()System.ComponentModel.ComponentResourceManager resources=new System.ComponentModel.ComponentResourceManager(typeof(AboutFrom);this.tabAbout=new System.Windows.Forms.TabControl();this.tabPage1=new System.Windows.Forms.TabPage();this.tabPage2=new System.Windows.Forms.TabPage();this.tabAbout.

41、SuspendLayout();this.SuspendLayout();/tabAbout /this.tabAbout.Controls.Add(this.tabPage1);this.tabAbout.Controls.Add(this.tabPage2);this.tabAbout.Location=new System.Drawing.Point(0,0);this.tabAbout.Name=tabAbout;this.tabAbout.SelectedIndex=0;this.tabAbout.Size=new System.Drawing.Size(319,217);this.

42、tabAbout.TabIndex=0;/tabPage1 /this.tabPage1.Font=new System.Drawing.Font(宋 体,9F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,(byte)(134);this.tabPage1.Location=new System.Drawing.Point(4,21);this.tabPage1.Name=tabPage1;this.tabPage1.Padding=new System.Windows.Forms.Padding(3);thi

43、s.tabPage1.Size=new System.Drawing.Size(311,192);this.tabPage1.TabIndex=0;this.tabPage1.Text=关于;this.tabPage1.UseVisualStyleBackColor=true;/tabPage2 /this.tabPage2.Font=new System.Drawing.Font(宋 体,9F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,(byte)(134);this.tabPage2.Location=n

44、ew System.Drawing.Point(4,21);this.tabPage2.Name=tabPage2;this.tabPage2.Padding=new System.Windows.Forms.Padding(3);this.tabPage2.Size=new System.Drawing.Size(311,192);this.tabPage2.TabIndex=1;this.tabPage2.Text=相信信息;this.tabPage2.UseVisualStyleBackColor=true;/AboutFrom /this.AutoScaleDimensions=new

45、 System.Drawing.SizeF(6F,12F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.BackColor=System.Drawing.SystemColors.ActiveCaptionText;this.BackgroundImage=(System.Drawing.Image)(resources.GetObject($this.BackgroundImage);this.ClientSize=new System.Drawing.Size(331,229);this.Controls.

46、Add(this.tabAbout);this.Icon=(System.Drawing.Icon)(resources.GetObject($this.Icon);this.Name=AboutFrom;this.Text=关于;this.tabAbout.ResumeLayout(false);this.ResumeLayout(false);#endregion private System.Windows.Forms.TabControl tabAbout;private System.Windows.Forms.TabPage tabPage1;private System.Wind

47、ows.Forms.TabPage tabPage2;10.ChatFromusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ClientInfo.core;using System.IO;using System.Xml.Serialization;using System.Thre

48、ading;using ClientInfo;namespace ClientCommunication public delegate void ProcessMessage(ClientInfo.core.Message msg);public partial class ChatFrom:Form private Image userImage;public Image UserImage get return userImage;set userImage=value;private Image friendImage;public Image FriendImage get retu

49、rn friendImage;set friendImage=value;private Communication communication;/Thread thread;/public ChatFrom()/public ChatFrom(Communication communication)/public ChatFrom()munication=communication;this.InitializeComponent();/munication.MessageNotify+=new EventHandler(communication_MessageNotify);/Threa

50、d thread=new Thread(new ThreadStart(RecMessage);/void communication_MessageNotify(object sender,MessageNotifyEventArgs e)/if(e.Message.CommandName=TextMessage.CMDNAME)/DisplayTextMessage(e.Message);/TextMessage msg=e.Message as TextMessage;/txtChatLog.Text=msg.Sender+msg.NowTimes;/txtChatLog.Text+=S

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁