C#联合VisionPro实现TCP/IP通信详解

  using System;

  using System.Net;

  //引入支持socket的命名空间

  using System.Net.Sockets;

  using System.Text;

  using System.Threading;

  using System.Windows.Forms;

  namespace TCPIP通信 {

  public partial class Form1 : Form {

  public Form1() {

  InitializeComponent();

  }

  //声明socket需要支持的对象

  public Socket ServerSocket;//监听

  public Socket SocketAccept;//绑定客户端

  public Socket socket;//和客户端通信

  #region 开启监听

  Thread t1;//监听

  Thread t2;//接收

  //开启监听(监听的是客户端的链接)

  private void button1_Click(object sender, EventArgs e) {

  //实例化socket对象

  //参1:绑定一个ip地址,参2:数据流的格式,参3:协议

  ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

  //准备ip对象绑定ip地址

  IPAddress ip = IPAddress.Parse(textBox1.Text);

  //创建端口号

  int port = Convert.ToInt32(textBox2.Text);

  //把输入的ip和端口传入到连接中,尝试链接

  IPEndPoint iPEndPoint = new IPEndPoint(ip, port);

  try {

  //开启服务器的时候很容易报错,所有做异常处理

  ServerSocket.Bind(iPEndPoint);

  //开启监听(参数是监听的数量)

  ServerSocket.Listen(5);

  //禁用按钮,避免重复监听

  button1.Enabled = false;

  //监听是持续的,所以要在分线程中进行

  t1 = new Thread(Listen);

  //设置成后台线程

  t1.IsBackground = true;

  //分线程中开始对socket监听

  t1.Start(ServerSocket);

  } catch (Exception ex) {

  MessageBox.Show(ex.Message);

  }

  }

  private void Listen(object o) {

  SocketAccept = o as Socket;

  try {

  //因为要持续监听,所以用了while循环

  while (true) {

  //阻塞线程,直到有客户端进来的时候才会往下执行

  socket = SocketAccept.Accept();

  //表示有客户端进来了,需要显示到textBox上面

  AddMsg(DateTime.Now.ToString("yyyy-mmddHHMMSS") + ":" + socket.RemoteEndPoint + "链接成功");

  //换行操作

  AddMsg("

  ");

  //开启第二个线程,用来检测客户端传递过来的消息

  t2 = new Thread(Receive);

  t2.IsBackground = true;

  t2.Start(socket);

  }

  } catch (Exception ex) {

  MessageBox.Show(ex.Message);

  }

  }

  //接收消息的方法

  private void Receive(object obj) {

  socket = obj as Socket;

  try {

  while (true) {

  //定义字节,接收数据

  byte[] buffer = new byte[1024 * 1024 * 2];

  int r = socket.Receive(buffer);

  if (r == 0) {

  break;

  }

  string msg = Encoding.Default.GetString(buffer);

  AddMsg("他说:" + msg);

  AddMsg("

  ");

  }

  } catch (Exception ex) {

  MessageBox.Show(ex.Message);

  }

  }

  private void AddMsg(string msg) {

  //异步处理,否则没办法刷新ui

  richText.BeginInvoke(new Action(str => {

  richText.Text += msg;

  }), msg);

  }

  #endregion

  //发送消息

  private void sendBtn_Click(object sender, EventArgs e) {

  //首先获取要发送的消息

  string str = sendText.Text;

  byte[] buffer = Encoding.Default.GetBytes(str);

  try {

  socket.Send(buffer);

  AddMsg("我说:" + str);

  AddMsg("

  ");

  sendText.Clear();

  } catch (Exception ex) {

  MessageBox.Show(ex.Message);

  }

  }

  //关闭客户端

  private void button2_Click(object sender, EventArgs e) {

  if (socket != null) {

  //禁掉socket的接收和发送

  socket.Shutdown(SocketShutdown.Both);

  socket.Close();

  t2.Abort();

  }

  ServerSocket.Close();

  SocketAccept.Close();

  t1.Abort();

  //打开监听的按钮

  button1.Enabled = true;

  }

  }

  }