private void cmdSluchaj_Click(object sender, EventArgs e) { if (cmdSluchaj.Text == "Czekaj na połączenie") { Polaczenie.RunWorkerAsync(); cmdSluchaj.Text = "Rozłącz"; } else { if (czy_polaczono == true) { w.Write(KomunikatySerwera.Rozlacz); listener.Stop(); if (klient != null) { klient.Close(); } czy_polaczono = false; } wyswietl(txtLog, "Rozlaczono\n"); cmdSluchaj.Text = "Czekaj na połączenie"; cmdWyslij.Enabled = false; Polaczenie.CancelAsync(); Odbieranie.CancelAsync(); } }
private void Polaczenie_DoWork(object sender, DoWorkEventArgs e) { klient = new TcpClient(); wyswietl(txtLog, "Próbuje siê po³¹czyæ\n"); klient.Connect(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); wyswietl(txtLog, "Po³¹czenie nawi¹zane\n¯¹dam zezwolenia\n"); NetworkStream stream = klient.GetStream(); w = new BinaryWriter(stream); r = new BinaryReader(stream); w.Write(KomunikatyKlienta.Zadaj); if (r.ReadString() == KomunikatySerwera.OK) { wyswietl(txtLog, "Po³¹czono\n"); czypolaczono = true; cmdWyslij.Enabled = true; Odbieranie.RunWorkerAsync(); } else { wyswietl(txtLog, "Brak odpowiedzi\nRozlaczono\n"); czypolaczono = false; if (klient != null) { klient.Close(); } cmdWyslij.Enabled = false; cmdPolacz.Text = "Po³¹cz"; } }
private void Polaczenie_DoWork(object sender, DoWorkEventArgs e) { klient = new TcpClient(); //wyswietl(txtLog, "próbuję się połączyć\n"); klient.Connect(IPAddress.Parse(txtIP.Text), int.Parse(txtNrPortu.Text)); wyswietl(txtLog, "Połączenie nawiązane\nŻądam zezwolenia\n"); NetworkStream stream = klient.GetStream(); w = new BinaryWriter(stream); r = new BinaryReader(stream); w.Write(KomunikatyKlienta.Zadaj); if (r.ReadString() == KomunikatySerwera.OK) { wyswietl(txtLog, "Połączono\n"); czypoloczono = true; ZmienEnable(cmdWyślij, "on"); //cmdWyślij.Enabled = true; Odbieranie.RunWorkerAsync(); } else { wyswietl(txtLog, "Brak odpowiedzi\nRozlaczono\n"); czypoloczono = false; cmdPlacz.Text = "Połącz"; if (klient != null) { klient.Close(); } } }
private void Klient_FormClosed(object sender, FormClosedEventArgs e) { if (czypolaczono) { w.Write(KomunikatyKlienta.Rozlacz); klient.Close(); czypolaczono = false; } Polaczenie.CancelAsync(); Odbieranie.CancelAsync(); }
private void Polaczenie_DoWork(object sender, DoWorkEventArgs e) { wyswietl(txtLog, "Czekam na połączenie\n"); listener = new TcpListener(IPAddress.Any, int.Parse(txtPort.Text)); listener.Start(); while (!listener.Pending()) { if (this.Polaczenie.CancellationPending) { if (klient != null) { klient.Close(); } listener.Stop(); czy_polaczono = false; cmdSluchaj.Text = "Czekaj na połączenie"; return; } } klient = listener.AcceptTcpClient(); wyswietl(txtLog, "Zażądano połączenia\n"); NetworkStream stream = klient.GetStream(); w = new BinaryWriter(stream); r = new BinaryReader(stream); if (r.ReadString() == KomunikatyKlienta.Zadaj) { w.Write(KomunikatySerwera.OK); wyswietl(txtLog, "Połączono\n"); czy_polaczono = true; cmdWyslij.Enabled = true; Odbieranie.RunWorkerAsync(); //zmienne które ustawiłem ręcznie a powinny być przesyłane ee2 = 7; iloczyn_p_q2 = 368177; label_e2.Text = ee2.ToString(); label_n2.Text = iloczyn_p_q2.ToString(); } else { wyswietl(txtLog, "Klient odrzucony\nRozlaczono\n"); if (klient != null) { klient.Close(); } listener.Stop(); czy_polaczono = false; } }
private void frmSerwer_FormClosed(object sender, EventArgs e) { if (czy_polaczono) { w.Write(KomunikatySerwera.Rozlacz); listener.Stop(); if (klient != null) { klient.Close(); } czy_polaczono = false; } Polaczenie.CancelAsync(); Odbieranie.CancelAsync(); }
private void Polaczenie_DoWork(object sender, DoWorkEventArgs e) { wyswietl(TextLog, "Czekam na połaczenie\n"); listener = new TcpListener(int.Parse(txtPort.Text)); listener.Start(); while (!listener.Pending()) { if (this.Polaczenie.CancellationPending) { if (klient != null) { klient.Close(); } listener.Stop(); czypolaczono = false; cmdSluchaj.Text = "Czekaj na połączenie"; return; } } klient = listener.AcceptTcpClient(); wyswietl(TextLog, "Zażadano połączenia\n"); NetworkStream stream = klient.GetStream(); w = new BinaryWriter(stream); r = new BinaryReader(stream); if (r.ReadString() == komunikatyKlienta.zadaj) { w.Write(komunikatySerwera.OK); wyswietl(TextLog, "Połączono\n"); czypolaczono = true; cmdWyslij.Enabled = true; Odbieranie.RunWorkerAsync(); } else { wyswietl(TextLog, "Klient odrzucony\nRozlaczono\n"); if (klient != null) { klient.Close(); } listener.Stop(); czypolaczono = false; } }