日期:2014-05-17 浏览次数:20931 次
private void button_StartServer_Click(object sender, EventArgs e)
{
String strHostName = "";
try
{
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry( strHostName );
}
catch( Exception ex )
{
Console.WriteLine ("Error trying to get local address {0} ", ex.Message );
}
//创建一个套接字
m_sServer = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
//绑定套接字到端口
m_sServer.Bind(new IPEndPoint(IPAddress.Parse(tbServIP.Text), int.Parse(textBox_port.Text)));
//侦听是否有连接传入,指定挂起的连接最大值为1
m_sServer.Listen( 1 );
//开始一个异步操作来接受一个传入的连接尝试
//采用 IAsyncResult 参数,该参数随后可用来获取异步操作的结果
m_sServer.BeginAccept( new AsyncCallback( OnConnectRequest ), m_sServer);
}