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


C# Socket.BeginConnect方法代码示例

本文整理汇总了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);
	}
开发者ID:mono,项目名称:gert,代码行数:28,代码来源:client.cs

示例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;
    }
开发者ID:Ribosome2,项目名称:MySocketProject,代码行数:32,代码来源:SocketController.cs

示例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);
 }
开发者ID:vipuldivyanshu92,项目名称:Projects,代码行数:7,代码来源:RealmClient.cs

示例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());
        }
    }
开发者ID:yangwenlong,项目名称:ywl,代码行数:25,代码来源:Client.cs

示例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();
        }

       
    }
开发者ID:wtuickqmq,项目名称:BadugiPokerClient,代码行数:31,代码来源:SocketManager.cs

示例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);
		}
	}
开发者ID:learnUnity,项目名称:KU_NET,代码行数:34,代码来源:AsynchronousClient.cs

示例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();
        }
    }
开发者ID:stefanseibert,项目名称:padawan101,代码行数:32,代码来源:RotationDistributor.cs

示例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);
 }
开发者ID:fajoy,项目名称:RTSPExample,代码行数:7,代码来源:SockHelper.cs

示例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;
     }
 }
开发者ID:samardzicnenad,项目名称:CooperUnion,代码行数:27,代码来源:AsynchronousClient.cs

示例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 );
        }
    }
开发者ID:2CatStudios,项目名称:TradingCardGame,代码行数:32,代码来源:NetworkManager.cs

示例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;
        }
    }
开发者ID:EttienneS,项目名称:Contingency,代码行数:29,代码来源:Program.cs

示例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());
        }
    }
开发者ID:cheetahray,项目名称:Projects,代码行数:31,代码来源:AsynchronousClient.cs

示例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;
 }
开发者ID:lwtbn1,项目名称:WWL,代码行数:8,代码来源:NetWorkManager.cs

示例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));
     }
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:9,代码来源:ArgumentValidationTests.cs

示例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);
    }
开发者ID:florisdh,项目名称:LCPiratesOnline,代码行数:9,代码来源:NetworkClient.cs


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