private void Friend_button_Click(object sender, EventArgs e) { if (Administration.User != null) { FriendsForm form = new FriendsForm(Administration); this.Visible = false; var closing = form.ShowDialog(); if (closing == DialogResult.OK) { Administration = form.Administration; UserRefresh(); this.Visible = true; } } }
public void ConnectWithServer() { try { socketForServer = new TcpClient("localHost", port); connection = true; } catch { // log.WriteLog(Level.ERROR, //"Failed to connect to server at {0}:999" + "localhost"); //return; // AppendConsoleText("Failed to connect to server at {0}:999" + "localhost"); clientForm.SetText("Failed to connect to server at {0}:999" + "localhost"); return; } networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); streamWriter = new System.IO.StreamWriter(networkStream); // log.WriteLog(Level.INFO, "*******This is client program who is connected to localhost on port No:10*****"); clientForm.SetText("*******This is client program who is connected to localhost on port No:10*****"); try { string outputString = ""; // read the data from the host and display it { string str = outputString; while (str != "exit" && connection == true) { outputString = streamReader.ReadLine(); //clientForm.SetText("Message Recieved by server:" + outputString); if (outputString == null) { continue; } if (outputString == "exit") { break; } if (outputString.Count() > 8 && outputString.Substring(0, 9) == "/download") { string[] tmpStr = outputString.Split(' '); int allBytesToRead = Int32.Parse(tmpStr[1]); DownloadFile(allBytesToRead); outputString = ""; str = ""; } else if (outputString.Count() > 8 && outputString.Substring(0, 13) == "/loginSuccess") { string[] tmpStr = outputString.Split(' '); // clientForm.SetText("Hello " + tmpStr[1] + ", have a nice day!"); outputString = ""; str = ""; clientForm.ActiveForm("Hello " + tmpStr[1] + ", have a nice day!"); } else if (outputString.Count() > 8 && outputString.Substring(0, 12) == "/loginFailed") { clientForm.SetText("Login failed. There is no user with gived params"); outputString = ""; str = ""; } else if (outputString.Count() > 8 && outputString.Substring(0, 12) == "/showFriends") { //clientForm.SetText("Login failed. There is no user with gived params"); if (friendsListOpened == false) { friendsGrid.FillGridWithFriends(outputString); friendsGrid.SetClientConnector(this); friendsListOpened = true; friendsGrid.ShowDialog(); } else { friendsGrid.FillGridWithFriends(outputString); } outputString = ""; str = ""; } else { streamWriter.WriteLine(str); streamWriter.Flush(); } } if (str == "exit") { streamWriter.WriteLine(str); streamWriter.Flush(); connection = false; } } } catch (Exception ex) { clientForm.SetText("Exception while reading from server"); connection = false; } // tidy up networkStream.Close(); connection = false; //clientForm.SetText("Press any key to exit from client program"); }