当前位置: 首页>>代码示例>>C#>>正文


C# TcpClient.EndConnect方法代码示例

本文整理汇总了C#中TcpClient.EndConnect方法的典型用法代码示例。如果您正苦于以下问题:C# TcpClient.EndConnect方法的具体用法?C# TcpClient.EndConnect怎么用?C# TcpClient.EndConnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TcpClient的用法示例。


在下文中一共展示了TcpClient.EndConnect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: setupSocket

 public void setupSocket()
 {
     //		try {
     //			host = hostField.text;
     //			if (string.IsNullOrEmpty(host))
     //				host = "localhost";
     //			port = int.Parse(portField.text);
     //			mySocket = new TcpClient(host, port);
     //			theStream = mySocket.GetStream();
     //			theWriter = new StreamWriter(theStream);
     //			theReader = new StreamReader(theStream);
     //			theReader.BaseStream.ReadTimeout = 1000;
     //			socketReady = true;
     //			Debug.Log ("Connected");
     //			menu.SetActive(false);
     //			world.SetActive(true);
     //			EventsManager.em.speedController.SetActive (true);
     //		}
     try {
         host = hostField.text;
         if (string.IsNullOrEmpty(host))
             host = "localhost";
         port = int.Parse(portField.text);
         mySocket = new TcpClient();
         var result = mySocket.BeginConnect(host, port, null, null);
         bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2));
         if (!success)
             throw new Exception("Connection timed out.");
         mySocket.EndConnect(result);
         theStream = mySocket.GetStream();
         theWriter = new StreamWriter(theStream);
         theReader = new StreamReader(theStream);
         theReader.BaseStream.ReadTimeout = 1000;
         socketReady = true;
         Debug.Log ("Connected");
         menu.SetActive(false);
         world.SetActive(true);
         EventsManager.em.speedController.SetActive (true);
         EventsManager.em.dallesPanels.SetActive (true);
     }
     catch (Exception e) {
         Debug.Log("Socket error: " + e);
         EventsManager.em.msgBox.ServerMessage("Connection failed: " + e.Message, Color.red);
         //Application.LoadLevel(0);
     }
 }
开发者ID:gbersac,项目名称:zappy_42,代码行数:46,代码来源:Connection.cs

示例2: TestConnection

    static bool TestConnection(string ipAddress, int Port, TimeSpan waitTimeSpan)
    {
        Debug.Log("testing server connection");
        using (TcpClient tcpClient = new TcpClient())
        {
            IAsyncResult result = tcpClient.BeginConnect(ipAddress, Port, null, null);
            WaitHandle timeoutHandler = result.AsyncWaitHandle;
            try
            {
                if (!result.AsyncWaitHandle.WaitOne(waitTimeSpan, false))
                {
                    tcpClient.Close();
                    return false;
                }

                tcpClient.EndConnect(result);
            }
            catch (Exception ex)
            {
                Debug.Log("connection to server not made");
                return false;
            }
            finally
            {
                timeoutHandler.Close();
            }
            return true;
        }
    }
开发者ID:CovalentGaming,项目名称:Super-Dash,代码行数:29,代码来源:Socket.cs


注:本文中的TcpClient.EndConnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。