Data transmission using WinSock
Understand the concept and the role of WinSock and practice the method approaching
WinSock using MFC
Embody chatting program between computers using CasyncSocket
TCP and IP were developed by a Department of Defense (DOD) research project to connect a
number different networks designed by different vendors into a network of networks (the
"Internet"). It was initially successful because it delivered a few bas
ic services that everyone
needs (file transfer, electronic mail, remote logon) across a very large number of client and
server systems. Several computers in a small department can use TCP/IP (along with other
protocols) on a single LAN. The IP component pr
ovides routing from the department to the
enterprise network, then to regional networks, and finally to the global Internet. On the
battlefield a communications network will sustain damage, so the DOD designed TCP/IP to be
robust and automatically recover
from any node or phone line failure. This design allows the
construction of very large networks with less central management. However, because of the
automatic recovery, network problems can go undiagnosed and uncorrected for long periods of
all other communications protocol, TCP/IP is composed of layers:
is responsible for moving packet of data from node to node. IP forwards each packet based
on a four
byte destination address (the IP number). The Internet authorities assign ranges of
numbers to different organizations. The organizations assign groups of their numbers to
departments. IP operates on gateway machines that move data from department to organization
to region and then around the world.
is responsible for verifying the
correct delivery of data from client to server. Data can be
lost in the intermediate network. TCP adds support to detect errors or lost data and to trigger
retransmission until the data is correctly and completely received.
is a name given to t
he package of subroutines that provide access to TCP/IP on most
The concept and role of WinSock
development of internet from ARPANET, DARPA (
Research Projects Agency)
was expecting to use the protocol supportin
g internet in UNIX OS
which many computers was using that time. They supported to make a layer of socket in
Software Distribution) Unix. Ever since that time, TCP/IP is used in most of the
station. WinSock is a something modified from UN
IX OS to Windows OS.
, WinSock can be thought to be a collection of
), which is used in making TCP/IP applications in Windows,
and a standard documents
which is arranged and standardized and ope
ned to the public. The initial intension of the
development of WinSock was to suggest and support a consistent
to various OS made by Microsoft. In other words, it was made to supply a method for making a
program consistently on TC
P/IP. Figure 1 shows the location of WinSock and relation to MFC
class library in TCP/IP. As you can see in Figure 1, WinSock performs the role of passing data
between application layer and TCP layer.
< The location of WinSock and relation to MFC class library in TCP/IP >
The structure of WinSock
WinSock support point
point communication with other
Because it is based on
TCP/IP, the user should know the IP address and port number required for communication with
the companion. The usual flow of WinSock using TCP/IP is like figure 2. At first, the server
te socket and keep
state and wait for the request from clients. When a client wants
to connect to the server, he also creates a socket and tries to connect. When the connection is
Window Socket API
Hardware Interface (Ethernet,etc)
successful, server accepts the request and creates a new socket and
connects it to the requested
state, is still in the same state and responds other
requests. You should pay attention to the fact that in a server, there
s a socket for listen and
another socket which is used i
n real communication. A server and a client
classified only by
functional meaning, and anyone of two similar computers can be a server. In other words, the
computer, which performs main part,
flow using TCP >
c r e a t e s o c k e t
give a name to socket
wait for the request
when a request comes, create a new socket
and connects it to the client. The original
socket waits for a new connection
try to connect
Classes supporting WinSock in MFC
MFC (Microsoft Foundation Class)
are classes that
made for the convenience of
users. WinSock is embodied by two types, and is supported to b
e easy for users. The two types
are CAsyncSocket and CSocket. In this experiment, we will use CAsyncSocket class. Next
functions are member functions of CAsyncSocket class and virtual functions which
acquire or set various features of socket
Accept() : accept a request
try to connect
Send() : transmit
This function is called when a request is
This function is called when data come.
This function is called when it is ready to send data.
This function is called when the connected socket of companion is closed.
Usually to use these classes, w
e should inherit the original one and embody again the
required functions in each case. We perform these tasks in Main Lab.
Making a new class and overriding member function
The tasks each users want to use are very different each other. So in MFC, users
can derive and
utilize the class proper for his tasks based on CAsyncSocket class. In this section, we will
explain the method to make a new class in Visual C++ 6.0
and to override the old
class member function in new class.
method to make a
>ClassWizard or ctrl
instrument to help the tasks as creating class, member functions, member variables,
message map without direct typing.
Click the button,
Type the name you want and select CAsyncSocket class as base class. Now you
created a new class derived from CAsyncSocket. Click the
in left side of
Workspace and you can confirm that the class you made is registered.
Select the cla
ss you created in Class
. You can see many items
window, and select one message and click ‘
Now you can override the
member function. For example, if you want to override OnReceive(), select the
OnReceive among messages and c
Investigate about the role of WinSock
Investigate the concept of inheritance in C++ and
Investigate the difference of TCP protocol and UDP protocol.
Explain precisely about OnAccept , OnClose, OnConnect, OnRe
ceive, OnSend which are
functions called in CAsyncSocket
by various events.
Investigate the prototype of
Create(), Listen(), Connect(), Accept(), Send(), Receive()
In this tutorial, the author
Visual C++ 6, p
Sung Kim for the
concrete process to embody the program. I suppose that you are a
of Visual C++, so
ll explain the initial step to begin a new project. But about the overall process, this material
provide only summary to co
nstitute the program, so it is strongly recommended that you should
refer the book of Yong
Sung Kim or other Visual C++ guide book to compete the
Example of program and User Interface
First, you should embody the structure of the
elect SDI in class wizard,
and select window socket.
Select CEditView as a Base class.
Now you should make a partition window. Create a new class CWriteView and
base class is also CEditView.
Now, you hav
e a partition window like this figure.
From now on, we will add communication ability to this basic program.
Creation of socket classes in common use :
You should embody three
. They are CserverSocket class, CDataSocket class,
class. CServerSocket functions as an socket in a server class that accepts the request from a
client and connects the client and a data socket in the server. , CdataSocket is also a class
which inherits from Csocket class, and it performs the roll to
give and receive real data.
CData class includes data format which is necessary for each side during communication.
Creation of CServerSocket class :
To make a
class, which can be,
used in common case, it is recommended to make a
program where a
request is performedWhen a request comes to the
server, OnAccept function is called, and UM_Accept message is sent to main window
of program. You should make Init function to initialize the server socket.
class CServerSocket : public CSocket
void Init(CWnd *pWnd, int nPortNum);
// ClassWizard generated virtual function overrides
virtual void OnAccept(int nErrorCode);
nerated message map functions
the ClassWizard will add and remove member functions here.
In Init function, Create function is called to initialize the socket, and Listen
is called to start the connect request sensing.
void CServerSocket::Init(CWnd *pWnd, int nPortNum)
m_pWnd = pWnd;
void CServerSocket::OnAccept(int nErrorCode)
Creation of data socket CDataSocket class.
To make a class
is able to be used in common case, it is recommended to make
a program where OnReceive function doesn
t receive the data for itself and instead of it,
the function sen
d UM_DATARECEIVE message to main window and make the main
window could receive the data.
class CDataSocket : public CSocket
void operator << (CData &data);
void operator >> (CData &data);
void Send(CData *pData);
void Receive(CData *pData);
void Init(CWnd *pWnd);
// ClassWizard generated v
irtual function overrides
virtual void OnReceive(int nErrorCode);
virtual void OnClose(int nErrorCode);
// Generated message map functions
the ClassWizard wil
l add and remove member functions here.
And you should also embody a few functions, Init(CWnd * pWnd), Receive(CData *
pData), Send(CData * pData), OnReceive(int nErrorCode), OnClose(int nErrorCode)
etc. For m
ore detail, it
s recommended to refer Visual C++ guide book.
The data to be sent is prototyped in CData class .
class CData : public CObject
virtual void Serialize(CArchive &ar);
unction should be over
rided like this.
void CData::Serialize(CArchive &ar)
ar << m_strData;
ar >> m_strData;
Add the three classes in Component Gallery. When you should add the classes to
your chatting program
First, you should add the socket class to your project. We will define the socket class as a
member variable of main frame. We will make a program which can perform as both client
class CMainFrame : public CFrameWnd
d make the program be able to perform both as a server and as a client. It
difficult task, so it
s strongly recommended to refer other guide book for detail. In this
material, only a summary to constitute the program is provided. First you should initialize
the server socket and handle the ability to accept the request when a request comes. To
ute this function, you need to handle with the UM_ACCEPT message. You should
also embody the ability as a client. You should prepare menu button to connect to server
and make a dialog box to input IP address and your name and port number. Your
ould be able to request connection to server and be able to give and take data
with the server. I
s recommended to use commend message handler function of
OnConnect(). You also should set up the type of data to give and take. We made CData
class before, s
o set up the data as a member variable of CData. In this program we will use
, so it
s recommended to set up the data type as a CString. To send data,
modify the OnKeyDown(
). When a new data is received, OnReceive function in
called, and UM_DATARECEIVE message is sent to main frame. So you
should modify CMainFrame::OnReceive(
). Please refer other guide book for detail.
We have made chatting program using WinSock. You might have checked that your computer can
municate with other computer. I guess, you might have had hard time to perform this experiment
if you are a beginner of Visual C++. But from now on, we will deal with transferring more various
of data. Good luck to you.