private void readMessages() { SocketStateObject state = new SocketStateObject(); state.m_SocketFd = this.socketFd; this.socketFd.BeginReceive(state.m_DataBuf, 0, SocketStateObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); }
private void ReceiveCallback(IAsyncResult ar) { try { /* retrieve the SocketStateObject */ SocketStateObject state = (SocketStateObject)ar.AsyncState; //Socket socketFd = state.m_SocketFd; /* read data */ int size = this.socketFd.EndReceive(ar); if (size > 0) { state.m_StringBuilder.Append(Encoding.ASCII.GetString(state.m_DataBuf, 0, size)); //Console.WriteLine(state.m_StringBuilder.ToString()); //Console.WriteLine("Otrzymałem jakąś wiadomość"); string message = state.m_StringBuilder.ToString(); //this.receiveMessage(message); //this.handleAnswers(message); this.parsingReceiveData(message); this.readMessages(); /* get the rest of the data */ //socketFd.BeginReceive(state.m_DataBuf, 0, SocketStateObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); } else { /* all the data has arrived */ if (state.m_StringBuilder.Length > 1) { Console.WriteLine("Otrzymałem jakąś wiadomość"); string message = state.m_StringBuilder.ToString(); //this.receiveMessage(message); this.parsingReceiveData(message); this.readMessages(); } Console.WriteLine("Pusta wiadomość"); } } catch (Exception exc) { //MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); this.login.setThreadedErrorLabel("Check \"Server Info\" and try again!"); Console.WriteLine("Check \"Server Info\" and try again!"); } }
private void readMessages() { SocketStateObject state = new SocketStateObject(); state.m_SocketFd = this.socketFd; this.socketFd.BeginReceive(state.m_DataBuf, 0, SocketStateObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); }