关于 Socket.accept
本帖最后由 kyoguys 于 2012-12-02 20:49:56 编辑
如:
Socket s;
Socket ss;
private void CmdStar_Click(object sender, EventArgs e)
{
label1.Text = "监听中";
Thread td = new Thread(new ThreadStart(aa));
td.Start();
}
void aa()
{
int port = Int32.Parse(textBox1.Text.Substring(textBox1.Text.LastIndexOf(":") + 1));
IPEndPoint end = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0],port);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(end);
s.Listen(10);
ss = s.Accept();
if(ss.Connected)
{
label1.Text = "连接成功";
}
}
如上可以连接成功 ,但是只支持单个连接访问
我看了别人的例子 做了如下修改
Socket s;
private Socket[] C;
private int C_Num;//存放客户端数量
private void CmdStar_Click(object sender, EventArgs e)
{
label1.Text = "监听中";
C = new Socket[10];
C_Num = 0;
Thread td = new Thread(new ThreadStart(aa));
td.Start();
}
void aa()
{
int port = Int32.Parse(textBox1.Text.Substring(textBox1.Text.LastIndexOf(":") + 1));
IPEndPoint end = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0],port);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(end);
s.Listen(10);
C[C_Num] = s