StreamReader issue when reading a TCPClient in a WebService

D

Donar

Good morning Ladies and Gents,

I am trying to develop a WebService and could use some pro's help with
an issue I ran into.

Among other things, the WebService involves querying a Whois server at
well-known port 43. When attempting to access the TCP client's data
with a StreamReader, the code hangs. No exception is thrown, it just
times out.

What am I doing wrong here? Is it possible that the WebService does
not allow such a connection?

Greatly appreciating your answer.
Regards.


This is the code snippet in question:

//Get the Internet address of the Whois server for this
Registry.
//With it, we can create a TcpClient (to well-known Whois
port 43)
string sWhoisAddr = "whois.arin.net";
TcpClient oTCPClt = new TcpClient(sWhoisAddr, 43);

//Querying the registry for the Whois record about an IP
in sIP.
//For example we have in sIP = "62.202.32.88"
byte[] abIP = Encoding.ASCII.GetBytes(sIP);
Stream oCltStream = oTCPClt.GetStream();
oCltStream.Write(abIP, 0, sIP.Length);

//Store the Whois server's response
StreamReader oPage =
new StreamReader(oTCPClt.GetStream(), Encoding.ASCII);

//THIS TIMES OUT, NO DATA IS RECEIVED. WHY?
//string sPage = oPage.ReadToEnd(); //Read the
answer
 
P

Peter Duniho

Good morning Ladies and Gents,

I am trying to develop a WebService and could use some pro's help with
an issue I ran into.

Among other things, the WebService involves querying a Whois server at
well-known port 43. When attempting to access the TCP client's data
with a StreamReader, the code hangs. No exception is thrown, it just
times out.

What am I doing wrong here? Is it possible that the WebService does
not allow such a connection?

If it didn't, I would think you'd get an exception.

Your code looks mostly correct, but there are a couple of subtle issues.
One is that you should write "abIP.Length" bytes to the stream, not
"sIP.Length" bytes. For ASCII encoding, that should be the same, but it's
better to use the correct variable anyway. Less chances of things
breaking if the code is copied for use in another context.

I also think that since you already calling TcpClient.GetStream() once and
saved the result in a variable, you ought to just use the same Stream when
you create your StreamReader, rather than calling GetStream() again.
Again, I don't see any particular problem in this example; it just seems
like good form to me.

The bigger issue is the question of how to terminate your query. My
recollection is that the query needs to be terminated with a CR/LF pair,
which you aren't sending in your code example. Without termination, I
would fully expect the server to just sit there waiting for you to finish
your query, and thus not send anything in reply.

Either append the necessary characters (CR and LF) to your string before
converting to bytes, or just use StreamWriter to send the string, and use
the WriteLine() method to send it.

Pete
 
D

Donar

Thanks, Pete, for your constructive answer.
It is running now, you helped loads.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top