private void Btn_cont_Click(object sender, RoutedEventArgs e) { if (btn_cont.IsChecked == true) { serverSocket = new ServerSocket(10001); serverSocket.AsyncDataReceive -= OnReceiveData; serverSocket.AsyncDataReceive += OnReceiveData; serverSocket.Connect(); MSG("服务开启"); } else { serverSocket.Disconnect(); MSG("服务断开"); } }
protected virtual byte[] SendRequest(string message) { WriteLog("Connecting to server socket..."); ServerSocket.Connect(EndPoint); WriteLog("Server Soket connected. Sending message..."); byte[] messageSent = $"{message}<EOF>".ToUTFBytes(); int byteSent = ServerSocket.Send(messageSent); WriteLog("Message Sent. Waiting for server's response..."); var wholeMessage = new List <byte[]>(); byte[] messageReceived = new byte[2048]; while (true) { int byteRecv = ServerSocket.Receive(messageReceived); if (byteRecv == 0) { break; } else { wholeMessage.Add(messageReceived.Take(byteRecv).ToArray()); } Console.Write($"Downloaded: "); WriteColored(byteRecv.ToString(), ConsoleColor.Green); Console.WriteLine(" bytes"); } WriteLog("Sutting down request."); ServerSocket.Shutdown(SocketShutdown.Both); ServerSocket.Close(); return(wholeMessage.SelectMany(a => a).ToArray()); }
//监听 private void BtnConnect_Click(object sender, RoutedEventArgs e) { if (isliten == false) { isliten = true; ipaddress = IPAddress.Parse(labIpAdd.Content.ToString()); port = int.Parse(labPort.Text); mySeverSocket = new ServerSocket(port); mySeverSocket.Connect(); mySeverSocket.Event_ReceiveMsg += ShowMesg; mySeverSocket.Event_ClientRegister += MySeverSocket_Event_ClientRegister; btnConnect.Content = "关闭监听"; } else { btnConnect.Content = "开始监听"; mySeverSocket.Disconnect(); isliten = false; } }
private void DelayReconnect(int ms) { Task.Run(new Action(() => { Thread.Sleep(ms); cSocket.Connect(lastServer, clientName, clientId); })); }
private bool ConnectToHost() { string str; int port = _session.Port; string hostPort = _session.Host; Utilities.CrackHostAndPort(hostPort, out str, ref port); var remoteEp = Proxy.Gateway; if (remoteEp != null) { WasForwarded = true; } else { IPAddress address; try { address = DNSResolver.GetIPAddress(str, true); } catch { _session.Request.FailSession(0x1f6, "DNS Lookup Failed", "DNS Lookup for " + HttpUtility.HtmlEncode(str) + " failed."); return(false); } if ((port < 0) || (port > 0xffff)) { _session.Request.FailSession(0x1f6, "Invalid Request", "HTTP Request specified an invalid port number."); return(false); } remoteEp = new IPEndPoint(address, port); } try { var socket = new Socket(remoteEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true, ReceiveTimeout = 10000, SendTimeout = 10000 }; ServerSocket = socket; ServerSocket.Connect(remoteEp); return(true); } catch (Exception exception) { if (WasForwarded) { _session.Request.FailSession( 0x1f6, "Gateway Connection Failed", string.Format(CultureInfo.InvariantCulture, "Connection to Gateway failed.<BR>Exception Text: {0}", exception.Message)); } else { _session.Request.FailSession( 0x1f6, "Connection Failed", string.Format(CultureInfo.InvariantCulture, "Connection to {0} failed.<BR>Exception Text: {1}", HttpUtility.HtmlEncode(str), exception.Message)); } return(false); } }