protected bool SendMessage(CommunicationType communicationType, string content, int ttl = 3) { if (ttl < 0) { return(false); } byte[] type = BitConverter.GetBytes((int)communicationType); string data = SplitStr + content + SplitStr; byte[] buf = Encoding.UTF8.GetBytes(data); try { //if (tcpClient != null) //{ // tcpClient.Close(); // tcpClient = new TcpClient(); //} tcpClient = ConnectSocket(RemoteEndPointIP, ShortMesPort); //tcpClient.Client.Send(buf); tcpClient.GetStream().Write(type, 0, type.Length); tcpClient.GetStream().Write(buf, 0, buf.Length); tcpClient.GetStream().Flush(); //tc.Close(); return(true); } catch (Exception ex) { EventPrint?.Invoke(ex.Message); return(SendMessage(communicationType, content, ttl - 1)); } }
public ServiceHost StartWCFByPipe(string hostAddress, Type serviceType, Type implementedContract) { try { Uri baseAddress = new Uri("net.pipe://" + hostAddress + "/Name/"); ServiceHost host = new ServiceHost(serviceType, baseAddress); NetNamedPipeBinding bind = new NetNamedPipeBinding(); //bind.TransferMode = TransferMode.Buffered; //bind.MaxReceivedMessageSize = int.MaxValue; //bind.ReaderQuotas.MaxDepth = 6553500; //bind.ReaderQuotas.MaxBytesPerRead = 6553500; //bind.ReaderQuotas.MaxNameTableCharCount = 6553500; //bind.ReaderQuotas.MaxStringContentLength = int.MaxValue; //bind.ReceiveTimeout = TimeSpan.MaxValue; //bind.OpenTimeout = TimeSpan.MaxValue; //bind.SendTimeout = TimeSpan.MaxValue; host.AddServiceEndpoint(implementedContract, bind, baseAddress); foreach (var endpont in host.Description.Endpoints) { endpont.EndpointBehaviors.Add(new ImplementBehaviorAttribute()); } host.Open(); return(host); } catch (Exception ex) { EventPrint?.Invoke(ex.Message); return(null); } }
public ServiceHost StartWCF(string hostAddress, int port, Type serviceType, Type implementedContract) { try { Uri baseAddress = new Uri("http://" + hostAddress + ":" + port + "/"); ServiceHost host = new ServiceHost(serviceType, baseAddress); host.AddServiceEndpoint(implementedContract, new WSHttpBinding(SecurityMode.None), baseAddress); //foreach (var endpont in host.Description.Endpoints) //{ // endpont.EndpointBehaviors.Add(new ImplementBehaviorAttribute()); //} host.Open(); return(host); } catch (Exception ex) { EventPrint?.Invoke(ex.Message); return(null); } }
public void Start() { EventPrint?.Invoke("Получение локального адреса сервера"); try { host = Dns.GetHostName(); EventPrint?.Invoke($"Имя хоста: {host}"); sSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); foreach (var addr in Dns.GetHostEntry(host).AddressList) { try { sSocket.Bind(new IPEndPoint(addr, port)); EventPrint?.Invoke($"Сокет связан с: {addr}:{port}"); break; } catch (Exception e) { EventPrint?.Invoke($"Не удалось связать с: {addr}:{port}"); } } sSocket.Listen(10); EventPrint?.Invoke("Прослушивание началось..."); while (true) { EventPrint?.Invoke("Ожидание нового подключения..."); var cSocket = sSocket.Accept(); EventPrint?.Invoke("Соединение с клиентом установлено!"); new ConnectedCLient(cSocket, this); } } catch (Exception e) { EventPrint?.Invoke("Что то пошло не так"); } }