By J.V.Ravichandran
Remember, it is not the connection speed that you must fear, in
an internet program, it is the garbage collector, lurking like an
unseen conspirator in a computer game, which may make or break your
computer program. To establish a connection, using the TCP/IP
protocol, use the TCPClient class in the .Net.Sockets namespace
TCPClient newcon=new TCPClient();
The method .Connect((string,int), member of the TCPClient class,
returns an integer (non-zero for unsuccessful connection)
int somenum=newcon.Connect(querystring,portnumber);
You can place this piece of code in a try-catch block or simply
use an if..else block. The first parameter takes the URL with which
you want to establish a connection, as a string and the second
parameter is the port number of the server. To be able to use the
connection, you must be able to utilise the stream that is connected
to the server. For this purpose, you must create a reference to it
through a stream object and since, it will be client/server
interaction, the stream will be a to-fro directional stream.
Using the .GetStream() method of the stream class, a reference to
the stream can be created but, having established a connection and
be able to use the connection is not enough. Your program should
also be able to send and receive data to and from the server.
But, in all client/server interaction, the server's permissions
always plays the heavy hand. So, your program must request the
server, the permission to use its resources, although, this you will
say, has already been achieved with a successful connection. It is
not so. This is the reason why you must create a reference to the
stream of the connected port.
Stream s=newcon.GetStream();
Append a carriage return and a linefeed escape sequences
("\r\n"), to the query string - the URL. Send this query string to
the server using the .Write method of the two-way stream object that
you have already created for the active port. Remember, though, that
a connection object is a very valuable resource and the roaming
garbage collector may destroy it at any time, without any warning.
Another important point, here, is that, since the .Write() method
takes a byte array as its first parameter, the query string (the
URL)l need to be converted into byte array form before being sent.
The .Write() method takes two more parameters - one, the offset and
the other, the length of the string.
Since, you have made a request to the server, you will obviously
get a response. So, wait for it. Read the response from the server
using the .Read() method, which, too, takes on the same type and
number as the .Write() method, but returns the number of bytes read
in the form of an integer.
int bytes=s.read(buf,0,SIZE);
where "s" is the object of type stream, "buf" is the temporary
storage and "0" starts the read operation from left to right and
SIZE is the size of data to be read/will be read. For this purpose,
you will need an array of objects of byte type
Byte[] buf=new Byte[SIZE];
and an object of string builder class to convert the bytes read
into a string.
StringBuilder strresponse=new StringBuilder(" ");
Here, as in many string type classes, a member method of the
string classes rarely take a string directly. The string to process
is usually passed to the string class using a constructor and the
StringBuilder class is no exception. Hence, the overloaded
constructor of the StringBuilder class takes the string to convert
and passes it on to the .GetString() method. To concatenate each
byte read from the stream, use the .Append() method and the enclosed
ASCII Encoding of the read byte using the .GetString() method is as
follows
strresponse.Append(Encoding.ASCII.GetString(buf,0,SIZE));
The above steps could help you succeed in getting information
from a server on a domainname and interestingly, if you own a
webmail account, the same information will be displayed for you,
when you simply log in to find info regarding your account - but,
the only difference will be your status. You will be querying as an
user !