Hello All,
I have a TcpListener and a TcpClient.I am trying to send and receive data from Server.
Here is the code in the client..
System.Net.Sockets.TcpClient w1 = new TcpClient();
w1.Connect("10.1.5.91", 188);
NetworkStream MyStream = w1.GetStream();
byte[] stream = System.Text.ASCIIEncoding.ASCII.GetBytes(txtData.Text);
MyStream.Write(stream, 0, stream.Length);
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveResponse),w1);
public void ReceiveResponse(object state)
{
try
{
TcpClient w1 = new TcpClient();
w1.Connect("10.1.5.91", 188);
NetworkStream st = w1.GetStream();
Byte[] bytes = new Byte[1024];
string data;
int i=0;
if (st.CanRead)
{
if (st.DataAvailable)
{
st.Read(bytes, 0, bytes.Length);
string ReplyFromServer = System.Text.Encoding.ASCII.GetString(bytes);
AddToListBox(ReplyFromServer);
}
// MessageBox.Show("Done..");
}
data = System.Text.ASCIIEncoding.ASCII.GetString(bytes, 0, i);
}
catch (Exception ex)
{
MessageBox.Show("Some problem in reading from the stream..");
MessageBox.Show(ex.Message);
}
// AddToListbox(data);
}
But the problem is I am not able to read from the networkstream more than once.
That is only for the first time I am read the stream and other time it is showing
st.DataAvailable =falseHow will I solve this...
Thanks,
Dipankar