winform下Socket通信的简单应用
2012-05-11 13:22
253 查看
今天给大家来讲解下socket通信的原理,通常socket通信分为TCP,UDP两种不同的用法,我把两种方法都写出来了。
先看效果图:
![](http://my.csdn.net/uploads/201205/11/1336713491_3673.jpg)
好了,基本上的通信完成了。有什么不懂的问我。
先看效果图:
![](http://my.csdn.net/uploads/201205/11/1336713491_3673.jpg)
using 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 System.Net; using System.Net.Sockets; using System.Threading; namespace APP { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /****************************************** * * 利用Socket通讯,TCP和UDP的不同用法 * TCP:面向连接,三次握手,数据安全性较高 * UDP:面向无连接,数据易丢失,安全性不高 * ******************************************/ //启动服务端 Socket SocketUdp; Socket SocketTcp; delegate void SetTextCallBack(string text);
//这个是启动服务端的按钮,在启动服务端后,会根据客户端发送回来的信息请求,自动将信息显示在文本框上 private void button1_Click(object sender, EventArgs e) { //UDP类型 SocketUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint ep = new IPEndPoint(IPAddress.Any, 3000); SocketUdp.Bind(ep);//绑定本机IP和Port Thread th = new Thread(new ThreadStart(ReceiveMsg)); th.IsBackground = true; th.Start(); label2.Text = "服务器已启动"; button1.Enabled = false; ////TCP类型 //SocketTcp = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); //EndPoint ep = new IPEndPoint(IPAddress.Any, 3000); //SocketTcp.Bind(ep); //SocketTcp.Listen(2000); //Thread th = new Thread(new ThreadStart(ReceiveMsg)); //th.IsBackground = true; //th.Start(); //label2.Text = "服务器已启动"; //button1.Enabled = false; } public void ReceiveMsg() { while (true) { byte[] buffer = new byte[5555]; EndPoint ep = new IPEndPoint(IPAddress.Any, 3000); int lea = SocketUdp.ReceiveFrom(buffer, ref ep);//接收远程; string msg = Encoding.Unicode.GetString(buffer, 0, lea); SetText(msg); } //while(true) //{ // Socket client = SocketTcp.Accept(); // byte[] buffer=new byte[1024]; // int lea = client.Receive(buffer);//接收远程; // string msg = Encoding.Unicode.GetString(buffer, 0, lea); // SetText(msg); // client.Close(); //} } public void SetText(string text) {
//在这里,我强调一下,为什么我会用invoke呢,或许很多人对此不太了解,如果不用的话,会报错。我会另外抽时间给大家讲解下invoke,beigninvoke,多线程,委托与事件之间的联系的,反正大家先这么写着。 if(richTextBox1.InvokeRequired) { SetTextCallBack st = new SetTextCallBack(SetText); this.Invoke(st,new object[]{text}); } else { richTextBox1.Text += DateTime.Now.ToString() + "\n" + text + "\n"; } } //客户端 private void button2_Click(object sender, EventArgs e) { if (button1.Enabled ==true) { MessageBox.Show("服务端未启动,请先启动服务器!"); } else { string msg = richTextBox2.Text; string ip = textBox1.Text; //UDP Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); byte[] buffer = Encoding.Unicode.GetBytes(msg); IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), 3000); client.SendTo(buffer, buffer.Length, SocketFlags.None, ipe); MessageBox.Show("发送成功!"); ////TCP //Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), 3000); //client.Connect(ipe); //byte[] buffer = Encoding.Unicode.GetBytes(msg); //client.Send(buffer); //MessageBox.Show("发送成功!"); } } } }
好了,基本上的通信完成了。有什么不懂的问我。
相关文章推荐
- socket上http协议应用(使用socket进行http通信的例子,准备好报头以后,简单read/write就可以了)
- (转)winform下UPD通信的简单应用
- 关于socket用winform实现简单通信 大牛可绕道 初学者可以看看 我做的时候把客户端窗口隐藏了 大家可以自己改一下
- winform下UPD通信的简单应用
- (转)winform下TCP通信的简单应用
- winform下TCP通信的简单应用
- Java用socket实现简单的一对一通信
- 使用Socket模拟简单的C/S通信,客户端之间如何互发文件
- linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)
- socket通信中的select应用简要说明
- socket通信简单介绍
- WinForm(C#) DataGridView简单应用示例
- Linux C TCP Socket实现客户与服务器简单通信
- linux socket 编程一:简单的服务器和客户端通信
- android使用Socket通信实现多人聊天应用
- 简单的Socket通信
- C# winform使用redis完成简单通信
- 详解C# Socket简单例子(服务器与客户端通信)
- linux下java程序与C语言程序通过SOCKET通信的简单例子
- [转] 最简单的使用UDP通信的Python Socket例子