通信(Socket)
来源:互联网 发布:阿里妈妈淘宝客 编辑:程序博客网 时间:2024/05/22 11:52
代码部分:
Client:
Socket sClient; private void txtConnect_Click(object sender, EventArgs e) { sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sClient.Connect(txtIp.Text, int.Parse(txtPort.Text)); txtMsg.Text += "连接成功\r\n"; Thread t = new Thread(() => { byte[] bs = new byte[1024]; while (true) { int len = sClient.Receive(bs); txtMsg.Invoke(new Action<string>((msg) => { txtMsg.Text += msg + "\r\n"; }), Encoding.UTF8.GetString(bs, 0, len)); } }); t.IsBackground = true; t.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { sClient.Send(new byte[] { (byte)2 }); sClient.Shutdown(SocketShutdown.Both); sClient.Dispose(); sClient.Close(); } private void btnSend_Click(object sender, EventArgs e) { //byte[] bs = new[] //{ // (byte)0 //}; byte[] bs = new byte[1024]; bs[0] = 0; byte[] bs2 = Encoding.UTF8.GetBytes(txtSend.Text); Array.Copy(bs2, 0, bs, 1, bs2.Length); sClient.Send(bs); } private void btnSP_Click(object sender, EventArgs e) { byte[] bs = new[] { (byte)1 }; sClient.Send(bs); }
Server:
Dictionary<string, Socket> dicClient = new Dictionary<string, Socket>(); private void btnStart_Click(object sender, EventArgs e) { Socket sServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sServer.Bind(new IPEndPoint(IPAddress.Parse(txtIp.Text), int.Parse(txtPort.Text))); sServer.Listen(10); txtMsg.Text += "启动成功\r\n"; //启动新线程,用于接收客户端请求。 Thread tAccept = new Thread(() => { int index = 0; while (true) { Socket sClient = sServer.Accept(); index++; string key = "用户" + index; dicClient.Add(key, sClient); lbClient.Invoke(new Action<string>((clientName) => { lbClient.Items.Add(clientName); }), key); Thread tReceive = new Thread(() => { byte[] bs = new byte[1024]; while (true) { int len = sClient.Receive(bs); if (bs[0] == 1) { //闪屏 Point old = this.Location; Random r = new Random(); for (int i = 0; i < 10; i++) { this.Invoke(new Action(() => { this.Location = new Point(old.X + r.Next(-10, 10), old.Y + r.Next(-10, 10)); })); Thread.Sleep(100); this.Invoke(new Action(() => { this.Location = new Point(old.X + r.Next(-10, 10), old.Y + r.Next(-10, 10)); })); Thread.Sleep(10); } } else if (bs[0] == 0) { //文本 txtMsg.Invoke(new Action<string>((msg) => { txtMsg.Text += "\r\n"+msg; }), sClient.RemoteEndPoint + ":" + Encoding.UTF8.GetString(bs, 1, len - 1)); } else if (bs[0] == 2) { break; } } }); tReceive.IsBackground = true; tReceive.Start(); } }); tAccept.IsBackground = true; tAccept.Start(); } private void btnSend_Click(object sender, EventArgs e) { Socket client = dicClient[lbClient.SelectedItem.ToString()]; client.Send(Encoding.UTF8.GetBytes(txtSend.Text)); }
0 0
- Socket通信(续)
- Socket 通信(Linux)
- Socket通信(双向)
- 通信(Socket)
- socket通信(一)
- Java(socket通信)
- Java Socket应用(一)--Socket通信
- 异步Socket通信(转)
- Socket通信总结(C#)
- android socket通信(上)
- android socket通信(下)
- Java--Socket通信(单向)
- Java--Socket通信(双向)
- Java Socket通信(多线程)
- android socket通信(上)
- android socket通信(下)
- Java--Socket通信(双向)
- Socket(Tcp)通信服务端
- tq2440开发板使用过程中遇到的问题
- java设计模式--适配器模式
- SQL SELECT DISTINCT 语句
- solr权重
- android home键重写,屏蔽,模拟
- 通信(Socket)
- oc错误:control reaches end of non-void function
- HTML5+Canvas 正方体旋转
- Raspberry Pi 图片网络化--curl 上传文件至Aliyun的ftp服务器
- 浅谈iOS中MVVM的架构设计与团队协作
- 个人疑问,待解决
- 各国开源软件和商业软件
- Ruby运算符重载
- 画图板的简单实现