public void Connect(int timeoutMilliseconds = 2000) { TcpClientWithTimeout tcpClient = new TcpClientWithTimeout(remoteEndPoint); tcpClient.Connect(timeoutMilliseconds); InternalTcpClient = tcpClient.InternalClient; CoutingStream = new CountingStream(InternalTcpClient.GetStream()); InternalBinaryReader = new BinaryReader(CoutingStream); InternalBinaryWriter = new BinaryWriter(CoutingStream); readerThread = new Thread(ReaderRun); readerThread.IsBackground = true; readerThread.Start(); }
private bool PingHostTCP(TcpClientWithTimeout c, string ipendpoint, int port) { try { stopwatch.Restart(); WriteLog("Pinged " + ipendpoint + "..."); c.Connect(ipendpoint, port); stopwatch.Stop(); //Is this the right placement? //writeLog("Pinged " + ipendpoint + "..."); return(true); } catch (Exception) { //writeLog("[TCP] Caught exception"); return(false); } }
//Do all the work in this backgroundworker method private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string host = txtIP.Text; if (radICMP.Checked) { Ping ping = new Ping(); PingReply reply; try { do { if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } reply = ping.Send(host, (int)nmrPingDelay.Value * 1000); WriteLog("Pinged " + host + "..."); }while (reply.Status != IPStatus.Success); WriteLog(host + " is up! " + "(" + reply.RoundtripTime + " ms)"); PlaySong(); } catch (Exception ex) { WriteLog(ex.Message.ToString()); } } else { bool success = false; int port = Convert.ToUInt16(txtPort.Text); //TcpClient client = new TcpClient(); TcpClientWithTimeout client = new TcpClientWithTimeout(); WriteLog("Initiating TCP ping sequence..."); try { do { if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } if (PingHostTCP(client, host, port)) { success = true; WriteLog(host + " is up! " + "(" + stopwatch.ElapsedMilliseconds + " ms)"); PlaySong(); } else { Thread.Sleep((int)nmrPingDelay.Value * 1000); } }while (!success); } catch (Exception ex) { WriteLog(ex.Message.ToString()); } } //BackgroundWorker should never touch the UI so use invoke Invoke((MethodInvoker) delegate { btnCheckIP.Enabled = true; }); Invoke((MethodInvoker) delegate { btnStopPinging.Enabled = false; }); Invoke((MethodInvoker) delegate { radICMP.Enabled = true; }); Invoke((MethodInvoker) delegate { radTCP.Enabled = true; }); if (chkLaunchProgram.Checked) { LaunchProgram(); } }
private void GetResponse() { System.Diagnostics.Stopwatch curcall = new System.Diagnostics.Stopwatch(); curcall.Start(); try { var retry = 0; while (++retry < maximumRetryCount) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } SetHeader("Host", uri.Host); int timeout = Network.http.MyHttp._timeout; var client = new TcpClientWithTimeout(uri.Host, uri.Port, timeout).Connect(); // var client = new TcpClient(); // client.Connect (uri.Host, uri.Port); using (var stream = client.GetStream()) { var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { ostream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient(uri.Host); } catch (Exception e) { LogUtil.LogError("Exception: " + e.Message); return; } } WriteToStream(ostream); response = new Response(); response.request = this; state = RequestState.Reading; response.ReadFromStream(ostream); } client.Close(); switch (response.status) { case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); continue; default: retry = maximumRetryCount; break; } } if (useCache) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags[uri.AbsoluteUri] = etag; } } } catch (Exception e) { #if !UNITY_EDITOR Console.WriteLine("Unhandled Exception, aborting request."); Console.WriteLine(e); #else LogUtil.LogError("Unhandled Exception, aborting request."); LogUtil.LogException(e); #endif exception = e; response = null; } state = RequestState.Done; isDone = true; responseTime = curcall.ElapsedMilliseconds; if (byteStream != null) { byteStream.Close(); } if (completedCallback != null) { if (synchronous) { completedCallback(this); } else { // we have to use this dispatcher to avoid executing the callback inside this worker thread ResponseCallbackDispatcher.Singleton.requests.Enqueue(this); } } if (LogAllRequests) { #if !UNITY_EDITOR System.Console.WriteLine("NET: " + InfoString(VerboseLogging)); #else if (response != null && response.status >= 200 && response.status < 300) { LogUtil.Log(InfoString(VerboseLogging)); } else if (response != null && response.status >= 400) { LogUtil.LogError(InfoString(VerboseLogging)); } else { LogUtil.LogWarning(InfoString(VerboseLogging)); } #endif } }
//Do all the work in this backgroundworker method private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string serverString = "Login server"; string royalsIP = "185.125.230.117"; //Login server 1 int port = 8484; //Login server port if (radLoginServer2.Checked) { royalsIP = "185.125.230.180"; //Login server 2 serverString = "Login server 2"; } if (radGameServer.Checked) { royalsIP = "51.68.205.179"; port = 7575; serverString = "Game server"; } bool successfulPing = false; //TcpClient client = new TcpClient(); TcpClientWithTimeout client = new TcpClientWithTimeout(); WriteLog("Starting ping sequence..."); try { do { if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } if (PingHostTCP(client, royalsIP, port)) { successfulPing = true; WriteLog(serverString + " is up! " + "(" + stopwatch.ElapsedMilliseconds + " ms)"); if (chkPlaySong.Checked) { PlaySong(); } } else { Thread.Sleep((int)nmrPingDelay.Value * 1000); } }while (!successfulPing); } catch (Exception ex) { WriteLog(ex.Message.ToString()); } //BackgroundWorker should never touch the UI so use invoke Invoke((MethodInvoker) delegate { btnCheckIP.Enabled = true; }); Invoke((MethodInvoker) delegate { btnStopPinging.Enabled = false; }); if (chkLaunchProgram.Checked) { LaunchProgram(); } }