==============================================================================
C-Scene Issue #2
The Wonderful world of Windows Sockets
CyrixBoy
==============================================================================
The Wonderful World of Sockets
An MFC Prospective
Introduction:
I have been asked to write a short article, which would explain some of the
basics of sockets from a windows abuser prospective.If requests are sufficient,
I will even consider a short tutorial on how to implement the CSocket class
into a simple Internet application.
Socket: (sok`it), n., v., -n. 1. A hollow or concave part or piece that
contains or fits a complimentary part.
Just What is a Socket, Bob?
A socket, from an Internet prospective, is a piece of software that can both
send and receive data over a TCP/IP network. A socket needs to know the IP
address it is talking to, the port it is talking to at that address, and the
socket type. To accommodate this, MFC contains a set of functions designed
to handle this two-way communication. If you are to write Internet programs
for Windows, you must grasp the basic concepts of windows Sockets.
Porting isn't what you Think
A socket must be able to know what port it is talking to and at what address.
A port, in Internet lingo, is nothing more than a convention used by the
programs that are running on each of the communicating machines. These ports
do not represent actual hardware, such as your parallel port, nor does it
imply a port like one you would find for your modem. In fact, a port doesn't
even refer to any actual hardware at all. It is just a convenient number
scheme. Different Internet programs use different ports for their activities.
It is through these ports that Internet programs can be allowed to do many
different tasks at once. They are basically communicating on many different
ports at the same time.
Sorting your Sockets
There are two kinds of sockets: stream and datagram sockets. Stream sockets
involve consistent and lengthy data flow, such as one you would find in an FTP
transfer. A datagram socket involves much smaller volumes of data such as that
in a broadcast message application, where the machine initiates a connection,
sends data and terminates the connection.
Getting Windows Involved
Remember that a windows socket is a collection of functions, which deal with
communicating information over the many Internet protocols through the
Internet. The functions for socket can be located in a file called WSOCK32.DLL
(WINSOCK.DLL) under the Csocket class. Luckily for you, windows allows you to
call these socket functions from anywhere inside your windows program. Here is
a listing of the Berkley-Sockets functions. If you have programmed in the UNIX
environment, you will definitely find these functions familiar.
accept htonl, htons, ntohl, ntohs
bind inet_addr, inet_ntoa
closesocket ioctlsocket
connect listen
getpeername recv, recvfrom
getsockname send, sendto
getsockopt, setsockopt shutdown
socket
Since this is merely an introductory article on the world of sockets, I am not
going into any lengthy war stories about any of these functions. Please feel
free to utilize your online help in order to accommodate their usage.
Typing your Sockets
Because Windows programming is message based, it is asynchronous. That means
that things don't necessarily happen in order. Packets arrive all the times,
and in different order. Typical sockets programming, however, are synchronous.
If your program asks the socket to send some information, it will wait for a
reply. Because the multi-tasking nature of windows cannot wait for the
synchronous response of a typical socket, There must be a method to apply the
synchronous behaviour of sockets to the asynchronous world of windows.
Confused yet? Great!
WSAAsynchGetHostByAddr
WSAAsyncGetHostByName
WSAAsyncGetProtoByName
WSAAsyncGetProtoByNumber
WSAAsyncGetServByName
WSAAsyncGetServByPort
WSAStartup,WSACleanup
As you can tell, most of these functions are renamed versions of the common
UNIX socket functions. They are merely the asynchronous versions. Again, feel
free to take a quick look through your online help in order to get a grasp of
these functions.
At a Stab
From a stab, we have learned a bit about sockets and how they apply to Windows.
We found out that MFC includes several functions to deal with most aspects of
sockets programming and concluded with several MFC based functions that
incorporate the MFC class CSocket. If you would like more information about
building your own internet applications, I highly recommend the book from which
I based this information from titled "Building Internet Applications with
Visual C++" written by Kate Gregory, published by QUE books
(ISBN:0-7897-0213-4) I found it to be an indispensable guide in my quest for
writing Internet applications.
C Scene Official Web Site :
http://cscene.oftheinter.net
C Scene Official Email :
cscene@mindless.com
This page is Copyright © 1997 By
C Scene. All Rights Reserved