求助:Socket多线程问题 急!!!
代码如下:为什么我的代码只能监听到一个端口,不能同时监听两个或者多个端口,谢谢!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bosch.OpCon.DDL.Client.Xml;
using Bosch.OpCon.DDL.Xml;
using Bosch.
using Bosch.
using
using
using System.Threading;
using
namespace opconxml
{
public partial class Form1 : Form
{
Thread myThread;
Thread dataThread;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
myThread = new Thread(new ThreadStart(Listen));
}
#region 监听
public void Listen()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.172.6.147"), int.Parse("2005"));
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(ipep);
server.Listen(10);
Socket client = server.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
byte[] data = new byte[1024];
int SendedCount = 0;
while (true)
{
client.Receive(data);
//string content = System.Text.Encoding.UTF8.GetString(data);
if (data.Length == 0)
{
break;
}
else
{
SendedCount++;
this.listView1.Items.Add("已发送包个数" + SendedCount.ToString());
}
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
myThread.Start();
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
myThread.Abort();
button1.Enabled = true;
button2.Enabled = false;
}
}
}