Waiting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm writing some code that talks to a POP3 server with a TcpClient. I can send and receive using a NetworkStream, but when I send my message, then try to recieve, no response has returned. I'm assuming that this is because the server hasn't had the chance to respond before the recieve buffer is read. If this is the case, then how do I tell my code to wait around before checking the stream?

If I just have a do{}until loop constantly checking the stream will this eat up the processor?
Is there a simple way of telling the code to wait for a few ticks?
Is there some way I can check the TcpClient or NetworkStream to see when to retrieve my data?

Any help?
Cheers,
Burns
 
this eat up the processor? Is there a simple way of telling the code
to wait for a few ticks? Is there some way I can check the TcpClient

Search the docs before you ask. The function you want is called Sleep().
As to which class it's in, well you'll have to find out. Try the help
system, it's useful.
 
Burns,

Maybe you could fire an event when data is received?

Marco
Burns said:
Hi,

I'm writing some code that talks to a POP3 server with a TcpClient. I can
send and receive using a NetworkStream, but when I send my message, then try
to recieve, no response has returned. I'm assuming that this is because the
server hasn't had the chance to respond before the recieve buffer is read.
If this is the case, then how do I tell my code to wait around before
checking the stream?
 
Hi Burns,

Burns said:
Hi,

I'm writing some code that talks to a POP3 server with a TcpClient. I can
send and receive using a NetworkStream, but when I send my message, then try
to recieve, no response has returned. I'm assuming that this is because the
server hasn't had the chance to respond before the recieve buffer is read.
If this is the case, then how do I tell my code to wait around before
checking the stream?
If I just have a do{}until loop constantly checking the stream will this eat up the processor?
Is there a simple way of telling the code to wait for a few ticks?
Is there some way I can check the TcpClient or NetworkStream to see when
to retrieve my data?

I haven't had the opportunity to program a sockets-based app, so I'm not
a expert, but have you tried the BeginRead/BeginWrite methods. I believe
they use async completion ports, which, as I understand it, are the most
efficient way to do async messaging.

Regards,
Dan
 
Using a NetworkStream, the read() method should block until data is
available to read. However, you may be reading only one or two bytes at a
time. You should make sure to try and keep "read()"ing until the full
response has been pulled down. If you do want to delay a bit, call
System.Threading.Thread.Sleep( ... );


Burns said:
Hi,

I'm writing some code that talks to a POP3 server with a TcpClient. I can
send and receive using a NetworkStream, but when I send my message, then try
to recieve, no response has returned. I'm assuming that this is because the
server hasn't had the chance to respond before the recieve buffer is read.
If this is the case, then how do I tell my code to wait around before
checking the stream?
 
Burns said:
Hi,

I'm writing some code that talks to a POP3 server with a TcpClient. I can
send and receive using a NetworkStream, but when I send my message, then try
to recieve, no response has returned. I'm assuming that this is because the
server hasn't had the chance to respond before the recieve buffer is read.
If this is the case, then how do I tell my code to wait around before
checking the stream?
If I just have a do{}until loop constantly checking the stream will this eat up the processor?
Is there a simple way of telling the code to wait for a few ticks?
Is there some way I can check the TcpClient or NetworkStream to see when to retrieve my data?

Any help?

Are you flushing the writestream after you send data?

ie:

streamWriter.WriteLine("some command");
streamWriter.Flush();

string response = streamReader.ReadLine();

(assumming these are the stream reader/writer you obtained from the network
stream of course).

I have exactly the same problem you described (apparently no data being
returned from the server). It wasn't untill I hooked up a sniffer I
realised the server wasn't responding as it hadn't recieved the command.

Alan
 
Normally, you don't need any special code to wait because Read will block
until data is available. And the wait done internally by Read is more
efficient than what you would program (it waits on some kernel object and
lets other processes/threads use the CPU until the kernel object is
signalled).

But you have to make sure that you Flush your output before calling Read.
Otherwise, your output will remain in buffers and the server won't get it.
So, you're likely to run into some kind of hung state (server waiting for
your output which is buffered, you waiting for the server's response) if you
forget to Flush.

Bruno.

Burns said:
Hi,

I'm writing some code that talks to a POP3 server with a TcpClient. I can
send and receive using a NetworkStream, but when I send my message, then try
to recieve, no response has returned. I'm assuming that this is because the
server hasn't had the chance to respond before the recieve buffer is read.
If this is the case, then how do I tell my code to wait around before
checking the stream?
 
to gabriel:

And you should think first, and then reply.
Using Sleep is definitely not the right approach here.
Well the right approach, I'm not gonna tell, and you ain't gonna find it in
the help files.
 
Back
Top