prev
next
Sockets Interface
This page we look at the WinSock Application Programmer's Interface (API).
This is a set of functions, written in C and accessible to either C or C++, that provide
connectivity to any machine connected to your local network or accessible through
the internet, e.g., has a known IP address.
You access the winsock API by inserting, at the beginning of your code, the statement:
#include <winsock2.h>
and adding wsock32.lib to the files to be linked with your application.
The winsock API conforms to a sockets standard, functional on both UNIX and Windows
platforms, so a sockets-based program written for a UNIX platform should be able to
communicate with a sockets-based program running on a Windows platform.
Win32 Sockets API
-
int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
-
Loads the winsock library version requested in wVersionRequested.
-
Returns zero if successful.
-
wVersionRequested is the winsock version, either 1.1 or 2.2,
written with major version in the low part of a DWORD and minor
version in the high part.
-
lpwSAData is a WSAData structure, filled by WSAStartup.
-
Example:
WSAData wsaData;
WSAStartup(MAKEWORD(2,2), wsaData);
-
SOCKET socket(int af, int type, int protocol)
-
creates a socket that is bound to a specific service provider.
-
returned value is a valid socket if no error, otherwise INVALID_SOCKET.
-
af is the address family of the protocol. AF_INET is the
address family of the internet protocol (IP).
-
type is the socket type. SOCK_STREAM is the type we will use.
-
protocol input qualifies the specific transport. IPPROTO_IP
is used for IP protocol.
-
Example:
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
-
int bind(SOCKET s, const struct sockaddr name, int namelen)
-
bind associates a socket with a specific port.
-
returned value is 0 if no error, otherwise SOCKET_ERROR.
-
s is a socket used for listening for incoming connection requests.
-
sockaddr is an address buffer specific to the protocol used.
-
namelen is the length of the address buffer
-
Example:
SOCKET S;
struct sockaddr_in tcpaddr;
int port = 5150;
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
tcpaddr.sin_family = AF_INET;
tcpaddr.sin_port = htons(port);
tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY) // accepts any incoming
bind(s, (SOCKADDR*)&tcpaddr, sizeof(tcpaddr));
-
int listen(SOCKET s, int backlog)
-
Starts socket s listening for incoming requrests.
-
returned value is 0 if no error, otherwise SOCKET_ERROR.
-
s is the listener socket.
-
backlog is the maximum queue length for pending connections.
-
SOCKET accept(SOCKET s, struct sockaddr addr, int addrlen)
-
Accepts an incoming request for a connection.
-
Return value of type SOCKET is the server socket to use for
this connection, if successful, otherwise INVALID_SOCKET.
-
s is the listening socket.
-
addr is an address buffer, specific to the protocol used.
-
addrlen is length of addr.
-
int connect(SOCKET s, const struct sockaddr name, int namelen)
-
Request to connect to a listener socket specified in name.
-
s is a valid socket you want to use to make a connection.
-
name is a tcp socketaddr structure that identifies the server
you want to connect to.
-
namelen is the length of the socket address structure.
-
Example:
SOCKET sClient;
int iPort = 4040;
char szServer = "127.0.0.1";
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(iPort);
server.sin_addr.s_addr = inet_addr(szServer);
connect(sClient, (struct sockaddr*)&server, sizeof(server));
-
int send(SOCKET s, const char *buf, int len, int flags)
-
Send a buffer of len bytes to a connected socket.
-
returns the number of bytes actually sent.
-
s is the sender's socket.
-
buf is the buffer of characters to be sent.
-
len is the number of buffer characters to send.
-
flags is zero for a routed message.
-
int recv(SOCKET s, char *buf, int len, int flags)
-
Accept incoming data on a valid socket s.
-
Returns the number of bytes received if successful. Returns 0 if the
connection has been closed by sender. Otherwise, returns -1 to indicate
an error.
-
s is the receiving socket.
-
buf is a character buffer you provide to receive data.
-
flags should be zero.
prev
next