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


C# Socket.EndConnect方法代码示例

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


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

示例1: Connected

    private void Connected(IAsyncResult iar)
    {
        client = (Socket)iar.AsyncState;
        try
        {
            client.EndConnect(iar);

            sendAuthLogonChallenge();

            client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
        }
        catch (SocketException)
        {
            Console.WriteLine("Connection failed");
            // Error connecting
        }
    }
开发者ID:vipuldivyanshu92,项目名称:Projects,代码行数:17,代码来源:RealmClient.cs

示例2: DualModeBeginConnect_DnsEndPointToHost_Helper

 public void DualModeBeginConnect_DnsEndPointToHost_Helper(IPAddress listenOn, bool dualModeServer)
 {
     int port;
     Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
     using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out port))
     {
         IAsyncResult async = socket.BeginConnect(new DnsEndPoint("localhost", port), null, null);
         socket.EndConnect(async);
         Assert.True(socket.Connected);
     }
 }
开发者ID:eerhardt,项目名称:corefx,代码行数:11,代码来源:DualModeSocketTest.cs

示例3: DualModeBeginConnect_IPAddressListToHost_Helper

 private void DualModeBeginConnect_IPAddressListToHost_Helper(IPAddress[] connectTo, IPAddress listenOn, bool dualModeServer)
 {
     int port;
     Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
     using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out port))
     {
         IAsyncResult async = socket.BeginConnect(connectTo, port, null, null);
         socket.EndConnect(async);
         Assert.True(socket.Connected);
     }
 }
开发者ID:eerhardt,项目名称:corefx,代码行数:11,代码来源:DualModeSocketTest.cs

示例4: Start

    //public static string SERVER = "127.0.0.1";
    void Start()
    {
        inst = this;
        Security.PrefetchSocketPolicy(SERVER,SocketPolicyServer.PORT,2000);

        PlayerInfo._id = Math.Abs(((int)DateTime.Now.Ticks))%10000000 * -1;
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        System.Net.IPAddress    remoteIPAddress  = System.Net.IPAddress.Parse(SERVER);
        System.Net.IPEndPoint   remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, Shoot3KillServer.PORT);

        socket.BeginConnect(remoteEndPoint,(IAsyncResult res)=>{
            _socket = (Socket) res.AsyncState;
            _socket.EndConnect(res);

            bool send_ok = true;

            _request_thread = new Thread(new ThreadStart(() => {
                AsyncReadState state = new AsyncReadState();
                while (true) {
                    int read = _socket.Receive(state._buffer);

                    if (read > 0) {
                        int start = 0;
                        int i = 0;

                        for (; i < read; i++) {
                            if (state._buffer[i] == (byte)Shoot3KillServer.MSG_TERMINATOR) {
                                state._msg.Append(Encoding.ASCII.GetString(state._buffer,start,i));

                                msg_recieved(state._msg.ToString());
                                state._msg.Remove(0,state._msg.Length);
                                start = i + 1;
                                send_ok = true;
                            }
                        }
                        state._msg.Append(Encoding.ASCII.GetString(state._buffer,start,read-start));

                    } else {
                        Debug.Log ("ERROR::request thread end, server down");
                        break;
                    }
                }
            }));
            _request_thread.Start();

            _send_thread = new Thread(new ThreadStart(() => {
                while (true) {
                    if ((_socket == null || !_socket.Connected) || !send_ok) {
                        Thread.Sleep(40);
                        continue;

                    }

                    string msg_text = null;
                    bool cont = false;
                    lock (_msg_send_queue) {
                        cont = _msg_send_queue.Count > 0;
                        if (cont) msg_text = _msg_send_queue.Dequeue();
                    }
                    if (!cont) {
                        Thread.Sleep(40);
                        continue;
                    }

                    byte[] msg_bytes = Encoding.ASCII.GetBytes(msg_text+Shoot3KillServer.MSG_TERMINATOR);

                    _socket.Send(msg_bytes);

                    send_ok = false;
                }
            }));
            _send_thread.Start();
        },socket);
    }
开发者ID:spotco,项目名称:Shoot3Kill,代码行数:75,代码来源:S3KOnlineClient.cs

示例5: MyConnect

                /// -----------------------------------------------------------------------------
                /// <summary>
                /// Callback for connection
                /// </summary>
                /// <param name="ar"></param>
                /// <remarks>
                /// </remarks>
                /// <history>
                /// 	[Caleb]	6/18/2005	Created
                /// </history>
                /// -----------------------------------------------------------------------------
                private void MyConnect(IAsyncResult ar)
                {
                    try
                    {
                        //RaiseEvent OnSomething("Found Connection")
                        m_tmpSocket = ((Socket) ar.AsyncState);

                        m_tmpSocket.EndConnect(ar);

                        if (m_tmpSocket.Connected == true)
                        {
                            Socket obj_Socket = m_tmpSocket;

                            DataObject obj_SocketState = new DataObject();

                            obj_SocketState.WorkSocket = obj_Socket;

                            obj_Socket.BeginReceive(obj_SocketState.Buffer, 0, obj_SocketState.BufferSize, 0, new AsyncCallback( MyDataIn), obj_SocketState);
                            if (OnConnectEvent != null)
                                OnConnectEvent("");
                            // SocketCheck = New Threading.Thread(AddressOf AmConnected)
                            //SocketCheck.Start()
                            obj_SocketState = null;
                            obj_Socket = null;
                            ar = null;
                            // GC.Collect()
                        }
                        else
                        {
                            SendLogMessage("IRC", "MyConnect", BlackLight.Services.Error.Errors.ERROR, "Recieved call back, but not connected", "", "", "");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        SendLogMessage("IRC", "MyConnect", BlackLight.Services.Error.Errors.ERROR, "Problem with connection callback", "", ex.Message, ex.StackTrace);
                        return;
                    }
                }
开发者ID:Mediator,项目名称:Blacklight-IRC-Services,代码行数:50,代码来源:IRC.cs

示例6: DualModeBeginConnect_LoopbackDnsToHost_Helper

 private void DualModeBeginConnect_LoopbackDnsToHost_Helper(IPAddress listenOn, bool dualModeServer, int port)
 {
     Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
     using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, port))
     {
         IAsyncResult async = socket.BeginConnect("localhost", port, null, null);
         socket.EndConnect(async);
         Assert.True(socket.Connected);
     }
 }
开发者ID:hitomi333,项目名称:corefx,代码行数:10,代码来源:DualModeSocketTest.cs

示例7: Socket_BeginConnectDnsEndPoint_Failure

        public void Socket_BeginConnectDnsEndPoint_Failure()
        {
            using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                SocketException ex = Assert.ThrowsAny<SocketException>(() =>
                {
                    IAsyncResult result = sock.BeginConnect(new DnsEndPoint("notahostname.invalid.corp.microsoft.com", UnusedPort), null, null);
                    sock.EndConnect(result);
                });

                SocketError errorCode = ex.SocketErrorCode;
                Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
                    "SocketErrorCode: {0}" + errorCode);

                ex = Assert.ThrowsAny<SocketException>(() =>
                {
                    IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", UnusedPort), null, null);
                    sock.EndConnect(result);
                });

                Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
            }
        }
开发者ID:shmao,项目名称:corefx,代码行数:23,代码来源:DnsEndPointTest.cs

示例8: Socket_BeginConnectDnsEndPoint_Success

        public void Socket_BeginConnectDnsEndPoint_Success(SocketImplementationType type)
        {
            int port;
            SocketTestServer server = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port);

            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", port), null, null);
            sock.EndConnect(result);

            sock.Dispose();
            server.Dispose();
        }
开发者ID:shmao,项目名称:corefx,代码行数:12,代码来源:DnsEndPointTest.cs

示例9: isReachable0

 public static bool isReachable0(object thisInet6AddressImpl, byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope)
 {
     if (addr.Length == 4)
     {
         return Java_java_net_Inet4AddressImpl.isReachable0(null, addr, timeout, inf, ttl);
     }
     // like the JDK, we don't use Ping, but we try a TCP connection to the echo port
     // (.NET 2.0 has a System.Net.NetworkInformation.Ping class, but that doesn't provide the option of binding to a specific interface)
     try
     {
         using (Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
         {
             if (inf != null)
             {
                 sock.Bind(new IPEndPoint(new IPAddress(inf, (uint)if_scope), 0));
             }
             if (ttl > 0)
             {
                 sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.HopLimit, ttl);
             }
             IPEndPoint ep = new IPEndPoint(new IPAddress(addr, (uint)scope), 7);
             IAsyncResult res = sock.BeginConnect(ep, null, null);
             if (res.AsyncWaitHandle.WaitOne(timeout, false))
             {
                 try
                 {
                     sock.EndConnect(res);
                     return true;
                 }
                 catch (SocketException x)
                 {
                     const int WSAECONNREFUSED = 10061;
                     if (x.ErrorCode == WSAECONNREFUSED)
                     {
                         // we got back an explicit "connection refused", that means the host was reachable.
                         return true;
                     }
                 }
             }
         }
     }
     catch (ArgumentException)
     {
     }
     catch (SocketException)
     {
     }
     return false;
 }
开发者ID:Zolo49,项目名称:ikvm,代码行数:49,代码来源:java.net.cs

示例10: Socket_BeginConnectDnsEndPoint_Failure

        public void Socket_BeginConnectDnsEndPoint_Failure()
        {
            using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                // TODO: Behavior difference from .Net Desktop. This will actually throw InternalSocketException.
                SocketException ex = Assert.ThrowsAny<SocketException>(() =>
                {
                    IAsyncResult result = sock.BeginConnect(new DnsEndPoint("notahostname.invalid.corp.microsoft.com", TestPortBase + 6), null, null);
                    sock.EndConnect(result);
                });

                SocketError errorCode = ex.SocketErrorCode;
                Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
                    "SocketErrorCode: {0}" + errorCode);

                // TODO: Behavior difference from .Net Desktop. This will actually throw InternalSocketException.
                ex = Assert.ThrowsAny<SocketException>(() =>
                {
                    IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", TestPortBase + 6), null, null);
                    sock.EndConnect(result);
                });

                Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
            }
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:25,代码来源:DnsEndPointTest.cs

示例11: Socket_BeginConnectDnsEndPoint_Success

        public void Socket_BeginConnectDnsEndPoint_Success()
        {
            SocketTestServer server = SocketTestServer.SocketTestServerFactory(new IPEndPoint(IPAddress.Loopback, TestPortBase + 5));

            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", TestPortBase + 5), null, null);
            sock.EndConnect(result);

            sock.Dispose();
            server.Dispose();
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:11,代码来源:DnsEndPointTest.cs


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