标签:
//从配置文件获取要监听的IP和端口 string strIP = System.Configuration.ConfigurationManager.AppSettings["IPAddress"]; int port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]); //开始监听 Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse(strIP), port)); AddLineToLog("初始化完成,正在监听:");
//连接信息对象 public ConnectionInfo connInfo = null; //连接对象 Connection newTcpConnection; public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //给连接信息对象赋值 //为了简单直接从文本框获取,也可以设置为从app.config配置文件获取IP和端口 connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text)); //根据连接信息对象,启动一个到服务器端的连接,如果不成功,会弹出异常信息 newTcpConnection = TCPConnection.GetConnection(connInfo); button1.Enabled = false; button1.Text = "连接成功"; }
标签:
原文地址:http://www.cnblogs.com/networkcomms/p/4261464.html