C#通讯编程(吐血整理)(2)

2018-12-10 23:57

//如果是发给不在线好友的信息

if (_str.StartsWith(\)) {

string UserName = _str.Substring(\.Length, 20).Trim(); string MessageS = _str.Substring(\.Length + 20, _str.Length - \.Length - 20);

SaveMessage(obj as string, UserName, MessageS); continue; }

//如果是离线请求

if (_str.StartsWith(\)) {

_transmit_tb.Remove(obj);

UpdateFriendList((string)obj, false, \);

// string svrlog = string.Format(\系统消息]用户 {0} 在 {1} 已断开... 当前在线人数: {2}\\r\\n\\r\\n\ // Console.WriteLine(svrlog); //向所有客户机发送系统消息

//foreach (DictionaryEntry de in _transmit_tb) //{

// string _clientName = de.Key as string; // Socket _clientSkt = de.Value as Socket;

// _clientSkt.Send(Encoding.Unicode.GetBytes(svrlog)); //}

Thread.CurrentThread.Abort(); }

//如果是请求好友列表

if (_str.StartsWith(\)) {

SerializeFriendList(obj, clientSkt); // 将该用户不在线时的信息发送给用户

DataTable TabMessage = ReadMessage(obj as string); if (TabMessage != null) {

foreach (DataRow myrow in TabMessage.Rows) {

if (myrow[\].ToString() == \) {

clientSkt.Send(Encoding.Unicode.GetBytes(myrow[\].ToString())); } else {

6 / 39

clientSkt.Send(Encoding.Unicode.GetBytes(\ + myrow[\].ToString().PadRight(20, ' ') + myrow[\].ToString())); } }

} //这里不需要再继续接受后继数据包了,跳出当前循环体。 continue; }

////如果是请求好友列表

//if (_str.StartsWith(\ //{

// byte[] onlineBuff = SerializeOnlineList(); // //先发送响应信号,用户客户机的判断

// clientSkt.Send(Encoding.Unicode.GetBytes(\ // clientSkt.Send(onlineBuff);

// //这里不需要再继续接受后继数据包了,跳出当前循环体。 // continue; //} //查找用户

if (_str.StartsWith(\)) {

DataTable TabFind = TabUser.Clone(); DataRow [] FindRow =null ;

string UserName = _str.Substring(\.Length, _str.Length - \.Length);

if (UserName.Equals(\)) { //看谁在线

FindRow = TabUser.Select(\); }

else//精确查找 {

FindRow = TabUser.Select(\ + UserName + \); }

foreach (DataRow myrow in FindRow) {

TabFind.ImportRow(myrow); }

clientSkt.Send(Encoding.Unicode.GetBytes(\)); IFormatter format = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); format.Serialize(stream, TabFind); stream.Position = 0;

byte[] ret = new byte[_maxPacket]; int count = 0;

count = stream.Read(ret, 0, _maxPacket);

7 / 39

while (count >0) {

clientSkt.Send(ret);

count = stream.Read(ret, 0, _maxPacket); }

clientSkt.Send(Encoding.Unicode.GetBytes(\)); stream.Close(); TabFind = null;

FindRow = null; //这里不需要再继续接受后继数据包了,跳出当前循环体。 continue; } //请求添加好友

if (_str.StartsWith(\)) {

string UserName = _str.Substring(\.Length, _str.Length - \.Length);

//通过转发表查找接收方的套接字

if (_transmit_tb.Count != 0 && _transmit_tb.ContainsKey(UserName)) {

Socket receiverSkt = _transmit_tb[UserName] as Socket;

receiverSkt.Send(Encoding.Unicode.GetBytes(\ + obj as string));

}

//这里不需要再继续接受后继数据包了,跳出当前循环体。 continue; }

//回复答应添加好友

if (_str.StartsWith(\)) {

string UserName = _str.Substring(\.Length, _str.Length - \.Length); //// 保存数据

DataTable TabmyFriend = new DataTable() ; //保存该用户

TabmyFriend.ReadXml(MyPath + \ + obj as string + \); DataRow newRow = TabmyFriend.NewRow(); newRow[\] = UserName; TabmyFriend.Rows.Add(newRow);

TabmyFriend.WriteXml(MyPath + \ + obj as string + \, XmlWriteMode.WriteSchema, false); //保存其好友

TabmyFriend = new DataTable();

TabmyFriend.ReadXml(MyPath + \ + UserName + \); DataRow newRow1 = TabmyFriend.NewRow(); newRow1[\] = obj as string;

8 / 39

TabmyFriend.Rows.Add(newRow1);

TabmyFriend.WriteXml(MyPath + \ + UserName + \, XmlWriteMode.WriteSchema, false);

TabmyFriend = null;

SerializeFriendList(obj, clientSkt); //\开始\按钮事件

private void button1_Click(object sender, System.EventArgs e) {

//取得预保存的文件名

string fileName=textBox3.Text.Trim();

//远程主机

string hostName=textBox1.Text.Trim();

//端口

int port=Int32.Parse(textBox2.Text.Trim());

//得到主机信息

IPHostEntry ipInfo=Dns.GetHostByName(hostName);

//取得IPAddress[]

IPAddress[] ipAddr=ipInfo.AddressList;

//得到ip

IPAddress ip=ipAddr[0];

//组合出远程终结点

IPEndPoint hostEP=new IPEndPoint(ip,port);

//创建Socket 实例

Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); try {

9 / 39

//尝试连接

socket.Connect(hostEP); }

catch(Exception se) {

MessageBox.Show(\连接错误\+se.Message,\提示信息

,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information); }

//发送给远程主机的请求内容串

string sendStr=\ + hostName +

\;

//创建bytes字节数组以转换发送串

byte[] bytesSendStr=new byte[1024];

//将发送内容字符串转换成字节byte数组

bytesSendStr=Encoding.ASCII.GetBytes(sendStr); try {

//向主机发送请求

socket.Send(bytesSendStr,bytesSendStr.Length,0); }

catch(Exception ce) {

10 / 39


C#通讯编程(吐血整理)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2016年高考语文试题分类汇编:成语(完整解析)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: