You are on page 1of 17

 Winsock is short for Windows Sockets, it is a

specification that defines how Windows


network applications should access network
services.
 Winsock is based on BSD Sockets, but it

provides additional functionality to allow the


API to comply with the standard Windows
programming model
 Windows extensions to socket functions:
 – WSAAsyncSelect,
 – WSAAsyncGetHostByAddr,
 – WSAAsyncGetHostByName
 Windows implements BSD sockets:
 – socket – create socket
 – connect – connect client to server
 – bind – bind to specific address
 – send – send data to the other side of the
socket
 – recv – receive data sent on a socket
 – listen – set up listening socket (server)
 – accept – accept incoming connection
(server)
 A socket is very much like a telephone - it's
the endpoint of a two-way communications
channel. By connecting two sockets
together you can pass data between
processes, even processes running on
different computers, just as you can talk
over the telephone once you've made a
phone call connecting your telephone with
someone else's.
• Before using sockets Windows application
must
call WSAStartup
• Before shutdown, Windows application
should
call WSACleanup
• WSAStartup requests specific version of
Windows
Socket implementation. This function can fail
if
Windows does not support particular version
of
Windows Sockets.
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char** argv)
{
WSADATA WSAData;
WSAStartup(0x202, &WSAData);
return 0;
}
• call WSAStartup
• create socket with socket function
• set socket to blocking mode with ioctlsocket
(in UNIX ioctl)
• connect to the server with connect function
• send or receive some data to/from server with
send and recv
• close the socket with closesocket (in UNIX
close)
• call WSACleanup
• call WSAStartup
• create socket with socket function
• bind socket to a port with bind function
• start listening for requests with listen
function
• call select function in a loop to find out if
connection request on listening socket is pending
• if there is connect request, call accept to
accept
connection request.
• accept creates new socket used for
communication with a client
• communicate with the client using send
and
recv functions
• close the client socket with closesocket
• (optional) accept new requests from clients
• close listening socket with closesocket
• call WSACleanup
• Socket can operate in one of two modes:
– blocking
– non blocking
• In blocking mode functions like connect, recv,
send wait for the operation to complete
• In non blocking mode those functions return
immediately and:
– connect starts background connect operation
– send, recv send or receive only the amount of
data
that will not block. The operation should be repeated
by the application
Socket mode can be set using ioctlsocket
function, for example:
ioctlsocket( so, FIONBIO, 1 ); // set non-
blocking
ioctlsocket( so, FIONBIO, 0 ); // set
blocking mode
Servers that must communicate with many clients
at the same time, must do one of the following:
– use blocking sockets and start one thread for each
client (each socket)
– use non blocking sockets and run select in a loop
to
find out which sockets are available for
reading/writing
– use non blocking sockets and use Windows
extension
to sockets (WSAAsyncSelect)
• The same applies to clients using many
connections at the same time
 Struct sockaddr_in addr;
SOCKET acceptSocket;
acceptSocket = socket( AF_INET,
SOCK_STREAM, 0 );
if ( acceptSocket == INVALID_SOCKET ) {
return false;
}
memset( &addr, 0, sizeof( addr ));
addr.sin_family = AF_INET;
addr.sin_port = htons( portNumber );
addr.sin_addr.S_un.S_addr = 0;
if ( bind( acceptSocket, (struct sockaddr
*)&addr,
sizeof( addr )) != 0 ) {
return false;
}
if ( listen( acceptSocket, 5 ) != 0 )
{return false;
}
 WSAAsyncSelect converts sockets events to
Windows messages
int WSAAsyncSelect(
SOCKET s, // socket
HWND hWnd, // window that will receive messages
unsigned int wMsg, // message number
long lEvent // events that should be sent to the
window
);
• lEvent is a combination of flags:
– FD_READ, FD_WRITE, FD_ACCEPT,
– FD_CONNECT, FD_CLOSE, FD_OOB
– FD_QOS, FD_GROUP_QOS,
– FD_ROUTING_INTERFACE_CHANGE,
FD_ADDRESS_LIST_CHANGE
Windows extensions: functions with WSA
prefix
• WSAAsyncGetHostByName
• WSAGetLastError
• WSAStartup
• WSACleanup
Thanks

You might also like