private void dataavailable(string msg) // Used for cross-thread operations on the dataavailLBL { if (InvokeRequired) { LabelChanger method = new LabelChanger(dataavailable); Invoke(method, msg); return; } dataavailLBL.Text = "Data available!"; }
private void RecvTCPData(IAsyncResult res) { LabelChanger lblchgr = new LabelChanger(dataavailable); // Used for cross thread operations on dataavailLBL ButtonStateChanger btnchgr = new ButtonStateChanger(BtnStateChanger); NetworkStream stream = tcprecvr.GetStream(); // Get the TCP data stream int nbytes = stream.EndRead(res); // Get the number of bytes read, and end the read if (nbytes == 0) // Finished reading { tcprecvr.Close(); // Close the TCP connection lblchgr.Invoke("Data available!"); // Inform the user there is a .WAV file to be played btnchgr.Invoke(true); BeginListening(); // Begin listening to connection requests again } else // Not finished reading, data in buffer { wavstream.Write(readbuf, 0, nbytes); // Write the data read into the .WAV stream stream.BeginRead(readbuf, 0, readbuf.Length, new AsyncCallback(RecvTCPData), null); // Start reading in data again } }
private void RecvTcp(IAsyncResult res) // Event function that will handle TCP connection attempts { LabelChanger lblchgr = new LabelChanger(dataavailable); // Used for cross thread operations on dataavailLBL ButtonStateChanger btnchgr = new ButtonStateChanger(BtnStateChanger); // Used for cross thread operations on the play & stop buttons wavstream = new MemoryStream(); // Clear the wav stream, we don't want two wav files in one stream, it would cause errors. lblchgr.Invoke("Data unavailable."); // No data available yet. btnchgr.Invoke(false); // Disable the play, stop buttons tcprecvr = tlisten.EndAcceptTcpClient(res); // Create a new TCP connection with the requester NetworkStream stream = tcprecvr.GetStream(); // Get the TCP network stream if (stream.CanRead) { stream.BeginRead(readbuf, 0, readbuf.Length, new AsyncCallback(RecvTCPData), null); } else { tcprecvr.Close(); MessageBox.Show("An error has occurred. Unable to read incoming TCP stream.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } }