Esempio n. 1
0
        //构建Socket
        public void BuildServerSocket(string strIP, int iPort)
        {
            try
            {
                if (SrvSocket == null)
                {
                    SrvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
                if (SrvSocket.Connected)
                {
                    if (SrvSocket.RemoteEndPoint.ToString() != strIP + ":" + iPort.ToString())
                    {
                        SrvSocket.Disconnect(true);
                    }
                }
                if (!SrvSocket.Connected)
                {
                    //从配置文件获取ip地址和端口号
                    IPEndPoint _ipep = BuildIPEndPoint(strIP, iPort);
                    if (_ipep != null)
                    {
                        AsyncCallback callback = new AsyncCallback(ConnectCallback);
                        SrvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        SrvSocket.BeginConnect(_ipep, callback, SrvSocket);
                    }
                }
            }
            catch
            {
                return;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 同步连接
        /// </summary>
        /// <param name="strIP"></param>
        /// <param name="iPort"></param>
        public void BuildServerSocket(string strIP, int iPort)
        {
            try
            {
                if (SrvSocket == null)
                {
                    SrvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
                if (SrvSocket.Connected)
                {
                    if (SrvSocket.RemoteEndPoint.ToString() != strIP + ":" + iPort.ToString())
                    {
                        SrvSocket.Disconnect(true);
                    }
                }
                if (!SrvSocket.Connected)
                {
                    //从配置文件获取ip地址和端口号
                    IPEndPoint _ipep = BuildIPEndPoint(strIP, iPort);
                    if (_ipep != null)
                    {
                        //AsyncCallback callback = new AsyncCallback(ConnectCallback);
                        SrvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        //SrvSocket.BeginConnect(_ipep, callback, SrvSocket);
                        //尝试连接
                        try
                        {
                            SrvSocket.Connect(_ipep);
                        }
                        //异常处理
                        catch (SocketException e)
                        {
                            OnSocketException(new SyncTCPClientEventArgs("连接失败", SrvSocket), "connect err");
                            return;
                        }
                        if (SrvSocket.Connected)
                        {
                            OnClientConnected(new SyncTCPClientEventArgs("已经成功连接到服务器", SrvSocket), "server cnonnect");
                        }

                        SyncReceiveData();
                    }
                }
            }
            catch
            {
                OnOtherException(new SyncTCPClientEventArgs("创建连接失败", SrvSocket), "connect err");
                return;
            }

        }