然后就是下面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TCP通信
{
public partial class Form1 : Form
{
Socket listener;
Thread thrListener;
bool isStop =false;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
//打开Listener开始监听
thrListener = new Thread(new ThreadStart(Listen));
thrListener.IsBackground = true;
thrListener.Start();
}
//监听数据
private void Listen()
{
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 2014));
//不断监听端口
while (true)
{
if (isStop == true)
break;