This article explains the nature and purpose of Windows Sockets. This is a simple application to demonstrate about socket, socket types, and general flow diagram of socket communication,
CAsyncSocket and its API call in the easiest manner.
Definition of a Socket
A socket is a communication endpoint — an object through which a Windows Sockets application sends or receives packets of data across a network. A socket has a type and is associated with a running process, and it may have a name. Currently, sockets generally exchange data only with other sockets in the same “communication domain,” which uses the Internet Protocol Suite.
Both kinds of sockets are bi-directional: they are data flows that can be communicated in both directions simultaneously (full-duplex).
Two socket types are available:
Stream sockets provide for a data flow without record boundaries — a stream of bytes. Streams are guaranteed to be delivered and to be correctly sequenced and unduplicated.
Datagram sockets support a record-oriented data flow that is not guaranteed to be delivered and may not be sequenced as sent or unduplicated.
“Sequenced” means that packets are delivered in the order sent. “Unduplicated” means that you get a particular packet only once.
CSocket derives from
CAsyncSocket and inherits its encapsulation of the Windows Sockets API. A
CSocket object represents a higher level of abstraction of the Windows Sockets API than that of a
CAsyncSocket operates in nonblocking mode.
Socket Flow Diagram for Client and Server Application
First, we need to initialize a socket using a function
AfxSocketInit(), before the socket has been created. Its returns value is nonzero if the function is successful; otherwise
if( AfxSocketInit() == FALSE)
AfxMessageBox("Failed to Initialize Sockets");
Server performs the following actions. They are:
Creating Server Socket
ServerSocket class (which is derived from
m_Server, a socket is created with default Port ID “1001” using
BOOL Create( UINT nSocketPort = 0, int nSocketType = SOCK_STREAM,
long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT |
FD_CLOSE, LPCTSTR lpszSocketAddress = NULL );
After creating the socket, we need to make the server listen to the specific port using the
BOOL Listen( int nConnectionBacklog = 5 );
Accepting Client Connection Request
Client connection request is accepted by server using the
accept() function call.
virtual BOOL Accept( CAsyncSocket& rConnectedSocket,
SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL );
Call this member function to receive data from a socket. This function is used for connected sockets and is used to read incoming data.
virtual int Receive( void* lpBuf, int nBufLen, int nFlags = 0 );
Call this member function to send data on a connected socket. Send is used to write outgoing data on connected sockets.
virtual int Send( const void* lpBuf, int nBufLen, int nFlags = 0 );
Shutting Down Connection
Call this member function to disable sends and/or receives on the socket.
ShutDown is used on all types of sockets to disable reception, transmission, or both.
BOOL ShutDown( int nHow = sends );
m_Server.m_Client.ShutDown(0); m_Server.m_Client.ShutDown(1); m_Server.m_Client.ShutDown(2);
This function closes the socket.
virtual void Close( );
Client performs the following actions:
The same set of syntax is used for calling functions like
Connecting to a Server
Call this member function to establish a connection to an unconnected socket.
BOOL Connect( LPCTSTR lpszHostAddress, UINT nHostPort );
About the Application
Start Server Button
When user presses “Start Server” button that time,
create() function is called to create socket with a default port id (1001) and made server to
listen() to 1001 port.
MessageBox("Failed to create");
Stop Server Button
It is used to disable send and receive on the socket by calling
shutdown() function and close the socket with the help of
Send button is used to send data on a connected socket by calling
For better understanding, I created two classes and they are ServerSocket.cpp and ClientSocket.cpp. Both are derived from
When client sends the request on the socket that time
Accept callback function is triggered automatically to accept client request. For understanding, I created the
ServerSocket class and
Accept API function is placed here.
void CServerSocket::OnAccept(int nErrorCode)
m_Client.m_hSocket = INVALID_SOCKET;
AfxMessageBox("Client Request Connection");
ClientSocket class is created to understand that data are received from client. So
Receive API function definition is done in the
void CClientSocket::OnReceive(int nErrorCode)
int ibuf = Receive(buff,1000);
buff[ibuf] = '\0';
udata = (userinfo*)buff;
Connect Server Button
IP address is a must to connect with the server. So before pressing “Connect Server” button, provide IP address in the IP address text box and that address is passed to
connect() function as a parameter.
MessageBox("Failed to Create Socket");
MessageBox("Failed to Connect");
m_Client.pDlg = this;
The same functionality is implemented for disabling send and receive and closing socket and which is cited above.
Send Message Button
Give user name and type message which you are going to send and user name is optional.
Procedure to Run an Application
- Run “ChatSever.exe”.
- Press Start Server button to create a socket.
- Then Run “ChatClient.exe”.
- Give valid IP Address and press Connect Server button to establish a connection to an unconnected socket.
- Now server and client are ready to chat and sample snapshot given is below: