本文整理汇总了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);
}
}
示例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;
}
}