private void ConnectCallback(IAsyncResult ar) { try { state = new ClientSocketObject { fd = (Socket)ar.AsyncState }; state.fd.EndConnect(ar); form1.SetStatusLabelDelegate("Connected to server!"); while (true) { if (state.fd.Poll(-1, SelectMode.SelectRead)) { StartReceiving(); } } } catch (Exception exc) { MessageBox.Show("6. Exception:\t\n" + exc.Message.ToString()); form1.SetStatusLabelDelegate("Check \"Server Info\" and try again!"); form1.SetButtonDelegate(true); } }
public void ReceiveCallback(IAsyncResult ar) { try { ClientSocketObject state = (ClientSocketObject)ar.AsyncState; int size = state.fd.EndReceive(ar); state.stringBuilder.Append(Encoding.ASCII.GetString(state.buffer, 0, size)); //Console.WriteLine(Encoding.ASCII.GetString(state.buffer, 0, size)); if (!state.stringBuilder.ToString().Contains("<<<!EOF!>>>")) { state.fd.BeginReceive(state.buffer, 0, ClientSocketObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); } else { form1.SetStatusLabel("Done."); form1.SetButtonDelegate(true); String filesString = state.stringBuilder.ToString(); Char action = filesString[0]; filesString = filesString.Remove(filesString.IndexOf("<<<!EOF!>>>", 0)); // remove EOF character sequence filesString = filesString.Remove(0, 2); // remove action character if (action == 'f') { form2 = new Form2(filesString, form1, conn); form1.OpenNextFormDelegate(filesString, form2); } else if (action == 'F') { form2.UpdateFilesListDelegate(filesString); } else if (action == 'o') { int index = Int32.Parse(filesString.Substring(0, filesString.IndexOf('#'))); filesString = filesString.Remove(0, filesString.IndexOf('#') + 1); form3 = new Form3(filesString, form2, conn, index); form2.OpenNextFormDelegate(filesString, form3); } else if (action == 'u') { filesString = filesString.Remove(0, filesString.IndexOf('#') + 1); form3.UpdateFileContentDelegate(filesString); } state.stringBuilder = state.stringBuilder.Remove(0, state.stringBuilder.Length); Array.Clear(state.buffer, 0, state.buffer.Length); } } catch (Exception exc) { MessageBox.Show("8. Exception:\t\n" + exc.Message.ToString()); form1.SetStatusLabelDelegate("Check \"Server Info\" and try again!"); form1.SetButtonDelegate(true); } }