I'm trying to access the string of messageRecieved
, which should pass to frmMain.Sort_Data(messageRecived);
. However it's null
once it hits here. The Console.WriteLine
shows the correct data.
public class Client
{
frm_main frmMain = new frm_main();
public string ReturnDat;
//for TCP communications
TcpClient _client = null;
//for sending/receiving data
byte[] data;
public Client(TcpClient client)
{
_client = client;
//start reading data from the client in a separate thread
data = new byte[_client.ReceiveBufferSize];
_client.GetStream().BeginRead(
data, 0, _client.ReceiveBufferSize, receiveMessage, null);
}
public void receiveMessage(IAsyncResult ar)
{
//read from client
int bytesRead;
lock (_client.GetStream())
{
bytesRead = _client.GetStream().EndRead(ar);
}
//if client has disconnected
if (bytesRead < 1)
return;
else
{
//get the message sent
string messageReceived =
ASCIIEncoding.ASCII.GetString(data, 0, bytesRead);
//Console.WriteLine(messageReceived);
frmMain.Sort_Data(messageReceived);
//ReturnDat = messageReceived;
}
//continue reading from client
lock (_client.GetStream())
{
_client.GetStream().BeginRead(
data, 0, _client.ReceiveBufferSize,
receiveMessage, null);
}
}
}
Below is the Sort_Data
, which returns nothing when it's hit in the class.
Public void Sort_Data(string data)
{
Messagebox.show(data);
}
Perhaps I'm missing something, or not calling something right?