基本信息
源码名称:C# Socket通信示例源码
源码大小:0.19M
文件格式:.rar
开发语言:C#
更新时间:2017-11-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
套接字通信(xuanjiwang)

客户端:






使用系统; 使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Text; 使用System.Windows.Forms; 使用System.Threading; 使用XuanJiSocket; 使用System.Net.Sockets; 使用System.Net; 命名空间ClientDemo { 公共部分类Form1:Form { public Form1() { 的InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } SocketHelper.TcpClients客户端; string ip = string.Empty; string port = string.Empty; 私人无效Form1_Load(对象发件人,EventArgs e) { //客户端如何处理异常等信息参照服务端 SocketHelper.pushSockets = new SocketHelper.PushSockets(Rec);//注册推送器 client = new SocketHelper.TcpClients(); ip = txtIP.Text; port = txtPort.Text; List<test> t = new List<test>() { new test(){Adds="黄土高坡",Age="12",Info="他很棒很棒",Name="哈哈", Gun="kar98K"}, }; dgvTest.DataSource = t; } /// <summary> /// 处理推送过来的消息 /// </summary> /// <param name="rec"></param> private void Rec(SocketHelper.Sockets sks) {  this.Invoke(new ThreadStart(delegate { if (infolist.Items.Count > 1000) { infolist.Items.Clear(); } if (sks.ex != null) { //在这里判断ErrorCode  可以自由扩展 switch (sks.ErrorCode) { case SocketHelper.Sockets.ErrorCodes.objectNull: break; case SocketHelper.Sockets.ErrorCodes.ConnectError: break; case SocketHelper.Sockets.ErrorCodes.ConnectSuccess: statuslist.Items.Add("连接成功.!"); break; case SocketHelper.Sockets.ErrorCodes.TrySendData: break; default: break; }  infolist.Items.Add(string.Format("客户端信息{0}", sks.ex));  } else { byte[] buffer = new byte[sks.Offset]; Array.Copy(sks.RecBuffer, buffer, sks.Offset); string str = Encoding.UTF8.GetString(buffer); if (str == "ServerOff") { infolist.Items.Add("服务端主动关闭"); } else { infolist.Items.Add(string.Format("服务端{0}发来消息:{1}", sks.Ip, str)); txtThis.Text  = "\r\n"; txtThis.Text  = string.Format("服务端{0}发来消息:{1}", sks.Ip, str); } }  })); } private void Recs(SocketHelper.Sockets sks) { this.Invoke(new ThreadStart(delegate { if (listBox1.Items.Count > 1000) { listBox1.Items.Clear(); } if (sks.ex != null) { //在此处理异常信息 // lb_ServerInfo.Items.Add(string.Format("客户端出现异常:{0}.!", sks.ex.Message)); cmbClient.Items.Remove(sks.Ip); //labClientCount.Text = (cmbClient.Items.Count).ToString();  } else { if (sks.NewClientFlag) { // lb_ServerInfo.Items.Add(string.Format("新客户端:{0}连接成功.!", sks.Ip)); cmbClient.Items.Add(sks.Ip); //  labClientCount.Text = (cmbClient.Items.Count).ToString(); } else { byte[] buffer = new byte[sks.Offset]; Array.Copy(sks.RecBuffer, buffer, sks.Offset); string str = string.Empty; if (sks.Offset == 0) { str = "客户端下线"; //  lb_ServerInfo.Items.Add(str); cmbClient.Items.Remove(sks.Ip); //   labClientCount.Text = (cmbClient.Items.Count).ToString(); } else { str = Encoding.UTF8.GetString(buffer); listBox1.Items.Add(string.Format("客户端{0}发来消息:{1}", sks.Ip, str)); }  } } })); }  private void bn_LoopSend_Click(object sender, EventArgs e) { client.SendData("客户端消息.!    "   Guid.NewGuid().ToString()); } private void bn_Connect_Click(object sender, EventArgs e) { try { ip = txtIP.Text; port = txtPort.Text; client.InitSocket(ip, int.Parse(port)); client.Start();  } catch (Exception ex) {  statuslist.Items.Add(string.Format("连接失败!原因:{0}", ex.Message)); btnConnect.Enabled = true; } }  private void bn_Disconnect_Click(object sender, EventArgs e) { client.Stop(); } private void bn_MaxConn_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(o => { //默认1024,自行修改更大的连接数. for (int i = 0; i < 1024; i  ) { SocketHelper.TcpClients clientx = new SocketHelper.TcpClients(); //初始化类库  clientx.InitSocket(ip,int.Parse(port)); clientx.Start(); } MessageBox.Show( “完成!”); });  }  private void btnSendThis_Click(object sender,EventArgs e) { client.SendData(this.txtThis.Text.Trim()); } SocketHelper.TcpServer服务器; private void btnSendTo_Click(object sender,EventArgs e) { if(cmbClient.SelectedItem!= null) { server.SendToClient((IPEndPoint)cmbClient.SelectedItem,string.Format(“服务端随机消息... {0}”,Guid.NewGuid()。ToString())); }  }  } }