/// <summary> /// Starte Verbindung / Server /// </summary> /// <returns></returns> public override bool Connect() { base.Connect(); try { lock (SyncConnect) { System.Net.IPAddress ip = System.Net.IPAddress.Any; if ((endpointAdress != null) && (endpointAdress != String.Empty)) { ip = System.Net.IPAddress.Parse(endpointAdress); } if (this.commType == Communicationtype.Command) { if (listen) { server = new TCPServer.CommandTcpServer(cmdHandler, ip, endpointPort); ((TCPServer.CommandTcpServer)server).StateChanged += new TCPServer.TCPServer.ConnectionStateChangeHandler(TCPCommunicationEndPoint_StateChanged); } else { client = new TcpCommandClient(endpointAdress, endpointPort, cmdHandler); client.StateChanged += new TcpBaseClient.ConnectionStateChangeHandler(client_StateChanged); client.Connect(); } } else if (commType == Communicationtype.Recieve) { if (listen) { server = new TCPServer.DataTcpServer(ip, endpointPort); ((TCPServer.DataTcpServer)server).DataRecieved += new TCPServer.DataTcpServer.RecieveHandler(server_DataRecieved); ((TCPServer.DataTcpServer)server).StateChanged += new TCPServer.TCPServer.ConnectionStateChangeHandler(TCPCommunicationEndPoint_StateChanged); } else { // if ((client != null) && (client.client.Connected)) client.client.Client.Disconnect(); client = new TcpDataClient(endpointAdress, endpointPort, commType); ((TcpDataClient)client).DataRecieved += new TcpDataClient.RecieveHandler(server_DataRecieved); ((TcpDataClient)client).StateChanged += new TcpBaseClient.ConnectionStateChangeHandler(client_StateChanged); client.Connect(); } } else if (commType == Communicationtype.Send) { if (listen) { server = new TCPServer.DataTcpServer(ip, endpointPort); ((TCPServer.DataTcpServer)server).StateChanged += new TCPServer.TCPServer.ConnectionStateChangeHandler(TCPCommunicationEndPoint_StateChanged); } else { client = new TcpDataClient(endpointAdress, endpointPort, commType); ((TcpDataClient)client).StateChanged += new TcpBaseClient.ConnectionStateChangeHandler(client_StateChanged); client.Connect(); } } else { //should never be the case return(false); } } } catch (Exception ex) { // Console.WriteLine("Exception in TCPCommunication Endpoint"+ex.Message+ex.StackTrace.ToString()); } finally { if (this.reconnecter == null) { Reconnect(); } } return(true); }
/// <summary> /// Starte Verbindung / Server /// </summary> /// <returns></returns> public override bool Connect() { base.Connect(); try { lock (SyncConnect) { System.Net.IPAddress ip = System.Net.IPAddress.Any; if ((endpointAdress != null)&&(endpointAdress != String.Empty)) ip = System.Net.IPAddress.Parse(endpointAdress); if (this.commType == Communicationtype.Command) { if (listen) { server = new TCPServer.CommandTcpServer(cmdHandler, ip, endpointPort); ((TCPServer.CommandTcpServer)server).StateChanged += new TCPServer.TCPServer.ConnectionStateChangeHandler(TCPCommunicationEndPoint_StateChanged); } else { client = new TcpCommandClient(endpointAdress, endpointPort, cmdHandler); client.StateChanged += new TcpBaseClient.ConnectionStateChangeHandler(client_StateChanged); client.Connect(); } } else if (commType == Communicationtype.Recieve) { if (listen) { server = new TCPServer.DataTcpServer(ip, endpointPort); ((TCPServer.DataTcpServer)server).DataRecieved += new TCPServer.DataTcpServer.RecieveHandler(server_DataRecieved); ((TCPServer.DataTcpServer)server).StateChanged += new TCPServer.TCPServer.ConnectionStateChangeHandler(TCPCommunicationEndPoint_StateChanged); } else { // if ((client != null) && (client.client.Connected)) client.client.Client.Disconnect(); client = new TcpDataClient(endpointAdress, endpointPort,commType); ((TcpDataClient)client).DataRecieved += new TcpDataClient.RecieveHandler(server_DataRecieved); ((TcpDataClient)client).StateChanged += new TcpBaseClient.ConnectionStateChangeHandler(client_StateChanged); client.Connect(); } } else if (commType == Communicationtype.Send) { if (listen) { server = new TCPServer.DataTcpServer(ip, endpointPort); ((TCPServer.DataTcpServer)server).StateChanged += new TCPServer.TCPServer.ConnectionStateChangeHandler(TCPCommunicationEndPoint_StateChanged); } else { client = new TcpDataClient(endpointAdress, endpointPort,commType); ((TcpDataClient)client).StateChanged += new TcpBaseClient.ConnectionStateChangeHandler(client_StateChanged); client.Connect(); } } else { //should never be the case return false; } } } catch (Exception ex) { // Console.WriteLine("Exception in TCPCommunication Endpoint"+ex.Message+ex.StackTrace.ToString()); } finally { if (this.reconnecter == null) Reconnect(); } return true; }