private void Server_StateInfo(object sender, EventArgs e) { if (e is MyTcpListenerEventArgs) { MyTcpListenerEventArgs myArgs = e as MyTcpListenerEventArgs; this.LogBox.Text += string.Format("\nDelegat odebrany z argumentami ServerState: {0}, ClientState: {1} \n", myArgs.ServerState, myArgs.ClientState); if (myArgs.ServerState == true) { this.ServerStateDisp.Text = "Serwer włączony"; this.ServerStateDisp.Foreground = Brushes.Green; this.LogBox.Text += ("ServerStateDisp set: Text Serwer włączony, Foreground Gree\n\n"); } else { this.ServerStateDisp.Text = "Serwer wyłączony"; this.ServerStateDisp.Foreground = Brushes.Red; this.LogBox.Text += ("ServerStateDisp set: Text Serwer wyłączony, Foreground Red\n\n"); } if (myArgs.ClientState == true) { this.ClientStateDisp.Text = "Klient podłączony"; this.ClientStateDisp.Foreground = Brushes.Green; this.LogBox.Text += ("ClientStateDisp set: Text Serwer włączony, Foreground green\n\n"); } else { this.ClientStateDisp.Text = "Klient niepodłączony"; this.ClientStateDisp.Foreground = Brushes.Red; this.LogBox.Text += ("ClientStateDisp set: Text Serwer włączony, Foreground Red\n\n"); } } }
//Zatrzymaj serwer public void StopServer() { client.Close(); this.ClientConnected = false; server.Stop(); this.ServerOn = false; myargs = new MyTcpListenerEventArgs(this.ServerOn, this.ClientConnected); NewState(myargs); }
/// <summary> /// Delegat aktualizacji stanu serwera i połączenia /// </summary> /// <param name="e"></param> public void NewState(MyTcpListenerEventArgs e) { EventHandler newStateInfo = StateInfo; if (newStateInfo != null) { newStateInfo(this, e); } }
/// <summary> /// /// </summary> /// <param name="port">Port na którym serwer będzie nasłuchiwać</param> /// <param name="filePath">Scieżka do pliku</param> /// <param name="fileContent">Zawartość pliku</param> public void CreateAndStartListeningServer(Int32 port, MainWindow mainWindow) { try { IPAddress localAddr = IPAddress.Parse("127.0.0.1"); //Nowy TcpListener z adresem lokalnym i portem z kontrolki PortComboBox server = new TcpListener(localAddr, port); if (mainWindow.fs == null) { mainWindow.WybierzPlik_Click(this, null); } server.Start(); // this will start the server this.ServerOn = true; mainWindow.LogBox.Text += "Serwer aktywny\n"; myargs = new MyTcpListenerEventArgs(this.ServerOn, this.ClientConnected); NewState(myargs); while (true) //we wait for a connection { client = server.AcceptTcpClient(); //if a connection exists, the server will accept it ( tylko 1 ) this.ClientConnected = true; mainWindow.LogBox.Text += "Klient połączony\n"; myargs = new MyTcpListenerEventArgs(this.ServerOn, this.ClientConnected); NewState(myargs); NetworkStream ns = client.GetStream(); mainWindow.fs.CopyTo(ns); // like ns.write simplier ns.Close(); mainWindow.LogBox.Text += "Wysyłanie danych zakończone\n"; break; } } catch (SocketException e) { mainWindow.LogBox.Text += string.Format("SocketException: {0}", e); StopServer(); } catch (Exception e) { mainWindow.LogBox.Text += string.Format("Exception: {0}", e); StopServer(); } finally { mainWindow.LogBox.Text += "Serwer zakończył nadawanie\n"; StopServer(); } }