TcpClient and TcpListener problem

Posted by Dipu710646 under C# on 2/23/2011 | Points: 10 | Views : 2287 | Status : [Member] | Replies : 1
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("", 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)
TcpClient w1 = new TcpClient();
w1.Connect("", 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);
// MessageBox.Show("Done..");


data = System.Text.ASCIIEncoding.ASCII.GetString(bytes, 0, i);
catch (Exception ex)
MessageBox.Show("Some problem in reading from the stream..");
// 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 =false

How will I solve this...



Posted by: Naimishforu on: 2/23/2011 [Member] [MVP] Bronze | Points: 25

I'm not sure about the same, can someone look into this please??

Dipu710646, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response