Most of you might have worked with
Internet Transfer Control which is very handy control when it comes to Internet
Programming but there is another control which even more robust and helps
programmers creating more flexible applications. Winsock control comes with VB6
and is used to create applications that access the low-level functions of the
Transmission Control Protocol/Internet Protocol (TCP/IP).
TCP/IP is a specification that defines a
series of protocols used to standardize how computers exchange information with
each other. TCP/IP provides communication across interconnected networks that
use diverse hardware architectures and various operating systems. The protocols
in TCP/IP are arranged in a series of layers known as a protocol stack. Each
layer has its own functionality.
Winsock is a standard that is maintained
by Microsoft. This standard is basically a set of routines that describe
communications to and from the TCP/IP stack. These routines reside in a dynamic
link library that runs under Windows. The winsock DLL is interfaced with TCP/IP
and from there through the Internet.
In this article, I am going to show how to
use the winsock in a client server environment, we will create two separate
applications, one of which will be a server and the other will be a client. Both
client and server will interact with each other to exchange data. Client will
send a request to the server and the server which will be connected to a
database will retrieve the information requested by the client from the database
and will return the requested information back to the client. You will a
database with this article, the database contains the item numbers and their
prices. In real life situations, database might be located on a machine
different from the one that hosts the client application.
I think it would be better to talk about
the ports before we proceed any further. A port is a special memory location
that exists when two computers are in communication via TCP/IP. Applications use
a port number as an identifier to other computers, both the sending and
receiving computers use this port to exchange data.
To make the job of communication easier,
some port numbers have been standardized. These standard port numbers have no
inherent value other than that users have agreed to use them with certain
applications. Table below lists a number of popular and publicly accepted port
numbers and their corresponding applications.
Using the Winsock Control
Winsock is above the TCP/IP protocol stack in the ISO/OSI
model. TCP/IP is an industry standard communication protocol that defines
methods for packaging data into packets for transmission between computing
devices on a heterogeneous network. TCP/IP is the standard for data transmission
over networks, including the Internet. TCP establishes a connection for data
transmission and IP defines the method for sending data packets.
The Microsoft Winsock control makes using the TCP/IP a
breeze. Microsoft has wrapped up the Winsock and INetAPI API calls into a nice
neat package that you can easily incorporate into your Visual Basic
Winsock Operating Modes
The Transport layer (also known as the Host-to-Host Transport
layer) is responsible for providing the Application layer with session and
datagram communication services. The core protocols of the Transport layer are
TCP and User Datagram Protocol (UDP). The Winsock control supports the following
two operating modes:
Winsock enables you to create clients and servers using the
same control. This dual functionality enables you to specify through property
setting the type of application you will be building. The Winsock control uses a
number of the same properties, whether you are creating client or a server,
thereby all but eliminating the learning curve needed to create applications.
Some of the important properties of the control are as following:
This property returns the number of bytes currently in the
receive buffer. This is a read-only property and is unavailable at design time.
The value returned is a long integer.
The LocalHostName property returns the name of the local host
system. This is read-only property and is unavailable at the design time. The
value returned is a string.
The LocalIP property returns the local host system IP address
in the form of a string, such as 188.8.131.52. This property is read-only and is
unavailable at design time.
This property returns or sets the local port number. This can
be both read from and written to and is available at both design time and
runtime. The value returned is a long integer.
Returns or sets the protocol, either TCP or UDP, used by the
The RemoteHost property returns or sets the remote host. This
can be both read from and written to and is available both in design time and
runtime. The value returned is a string and can be specified either as an IP
address or as a DNS name.
This property returns or sets the remote port number.
This returns the state of the control as expressed by an
enumerated list. This is read-only property and is unavailable at design time.
Some of the important methods of Winsock control are as