本文整理汇总了C#中Socket.BeginConnect方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginConnect方法的具体用法?C# Socket.BeginConnect怎么用?C# Socket.BeginConnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.BeginConnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnConnect
public void OnConnect (IAsyncResult ar)
{
Socket socket = (Socket) ar.AsyncState;
socket.EndConnect (ar);
//
// Start reading over the first connection. Note that this
// is necessary to reproduce the bug, without this, the
// call to BeginReceive on the second connection works
// fine. With this however, the BeginReceive call on the
// second connection fails with WSAEWOULDBLOCK.
//
byte [] buff = new byte [50];
socket.BeginReceive (buff, 0, buff.Length, SocketFlags.None, new AsyncCallback (OnReceive), socket);
//
// Close immediatly the first connection.
//
socket.Close ();
//
// Establish a second connection.
//
socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = false;
socket.BeginConnect (new IPEndPoint (IPAddress.Loopback, 10000),
new AsyncCallback (OnConnect2), socket);
}
示例2: Connnect
/// <summary>
/// 尝试连接服务器
/// </summary>
/// <param name="address"></param>
/// <param name="remoteport"></param>
/// <returns></returns>
public bool Connnect(string address, int remoteport)
{
Debug.Log("try to connect to " + address + " port number "+remoteport);
if (mSocket != null && mSocket.Connected)
{
return true;
}
IPHostEntry hostEntry = Dns.GetHostEntry(address);
foreach (IPAddress ip in hostEntry.AddressList)
{
try
{
IPEndPoint ipe = new IPEndPoint(ip, remoteport);
//创建Socket
mSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
mSocket.BeginConnect(ipe, new System.AsyncCallback(OnConnection), mSocket);
}
catch (Exception ex)
{
Debug.LogError(ex.Message);
}
}
return true;
}
示例3: Connect
public void Connect(string host, int port)
{
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipA = Dns.GetHostAddresses(host)[0];
IPEndPoint ipEP = new IPEndPoint(ipA, port);
newsock.BeginConnect(ipEP, new AsyncCallback(Connected), newsock);
}
示例4: StartClient
public static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.Resolve("user");
//IPAddress ipAddress = ipHostInfo.AddressList[0];
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
state = new StateObject();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
示例5: ConnectToServer
void ConnectToServer()
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//服务器IP地址
IPAddress ip = IPAddress.Parse(ipaddress);
//服务器端口
IPEndPoint ipEndpoint = new IPEndPoint(ip, port);
// clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ipaddress), port));
//这是一个异步的建立连接,当连接建立成功时调用connectCallback方法
IAsyncResult result = clientSocket.BeginConnect(ipEndpoint, new AsyncCallback(connectCallback), clientSocket);
//这里做一个超时的监测,当连接超过5秒还没成功表示超时
bool success = result.AsyncWaitHandle.WaitOne(5000, true);
if (!success)
{
//超时
//Closed();
Debug.Log("connect Time Out");
}
else
{
//与socket建立连接成功,开启线程接受服务端数据。
//worldpackage = new List<JFPackage.WorldPackage>();
//Thread thread = new Thread(new ThreadStart(ReceiveSorket));
t = new Thread(RecevieMessage);
t.IsBackground = true;
t.Start();
}
}
示例6: StartClient
public static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.Resolve("user");
//IPAddress ipAddress = ipHostInfo.AddressList[0];
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, "This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Release the socket.
// client.Shutdown(SocketShutdown.Both);
// client.Close();
}
catch (Exception e)
{
LogMgr.LogError(e);
}
}
示例7: Connection
public void Connection()
{
if(!connected)
{
string ip = IP.text;
Debug.Log("[CLIENT] Connecting to Server [" + ip + ":" + port + "]");
try
{
IPAddress ipAdress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAdress, port);
PlayerPrefs.SetString ("IP_Address", ip);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
Receive(client);
}
catch (Exception e)
{
distributor.closeConnection();
}
}
else
{
Application.Quit();
}
}
示例8: ConnectSocket
public void ConnectSocket(string host, int port)
{
IPAddress[] ips = Dns.GetHostAddresses(host);
IPEndPoint e = new IPEndPoint(ips[0], port);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.BeginConnect(e, cck, this);
}
示例9: fUniversal
//Universal function
public string fUniversal(string sAction, string sUser = "", string sArgsList = "")
{
try
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Create TCP/IP socket.
connectDone.Reset();
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); // Connect to the remote endpoint.
connectDone.WaitOne();
if (!ReferenceEquals(myException, null)) //No connection present
throw (myException);
sendDone.Reset();
Send(client, sAction + "," + sUser + "," + sArgsList); // Send data to the remote device.
sendDone.WaitOne();
receiveDone.Reset();
Receive(client); // Receive the response from the remote device.
receiveDone.WaitOne();
client.Shutdown(SocketShutdown.Both); //End connection
client.Close();
return response;
}
catch (Exception ex)
{
log.Debug(ex.ToString());
throw ex;
}
}
示例10: AttemptConnection
public void AttemptConnection( string ipAddressString, string portString )
{
debugLog.ReceiveMessage ( "\tAttempting Connection to " + ipAddressString + " on " + portString );
connecting = true;
connectionType = ConnectionType.Connecting;
debugLog.ReceiveMessage ( "\tConnection Type Set to Connecting" );
try
{
IPAddress ipAddress = Dns.GetHostEntry ( ipAddressString ).AddressList[0];
IPEndPoint remoteEndPoint = new IPEndPoint ( ipAddress, Convert.ToInt32 ( portString ));
Socket client = new Socket ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
client.BeginConnect ( remoteEndPoint, new AsyncCallback ( ConnectCallback ), client );
Send ( client, "This is a test<EOF>" );
Receive ( client );
UnityEngine.Debug.Log ( "Response received : " + response );
client.Shutdown ( SocketShutdown.Both );
client.Close ();
} catch ( Exception connectionError )
{
UnityEngine.Debug.LogError ( connectionError );
}
}
示例11: StartClient
public static void StartClient()
{
try
{
byte[] bytes = File.ReadAllBytes("c:\\file.bin");
string x = Convert.ToBase64String(bytes);
IPHostEntry ipHostInfo = Dns.Resolve(Environment.MachineName);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP, ConnectCallback, client);
ConnectDone.WaitOne();
Send(client, x + "<EOF>");
SendDone.WaitOne();
Receive(client);
ReceiveDone.WaitOne();
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
throw;
}
}
示例12: AsynchronousClient
public AsynchronousClient(int port)
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
/*
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
*/
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
示例13: Connect
public void Connect(string ip, int port)
{
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
client.NoDelay = true;
}
示例14: BeginConnect_EndPoint_ListeningSocket_Throws_InvalidOperation
public void BeginConnect_EndPoint_ListeningSocket_Throws_InvalidOperation()
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
socket.Listen(1);
Assert.Throws<InvalidOperationException>(() => socket.BeginConnect(new IPEndPoint(IPAddress.Loopback, 1), TheAsyncCallback, null));
}
}
示例15: Connect
public void Connect(string ip, int port)
{
if (_connecting || _connected) return;
_connecting = true;
_serverEP = new IPEndPoint(IPAddress.Parse(ip), port);
_connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_connection.BeginConnect(_serverEP, new AsyncCallback(ConnectCallBack), null);
}