Information Technology Reference
In-Depth Information
connect()
The
connect()
function establishes a connection with a peer. If the specified socket is un-
bound then unique values are assigned to the local association by the system and the socket
is marked as bound. Its syntax is
int connect (SOCKET s, const struct sockaddr FAR * name, int namelen)
where
s
Descriptor identifying an unconnected socket.
name
Name of the peer to which the socket is to be connected.
namelen
Name length.
If no error occurs then it returns a zero value. Otherwise, it returns
SOCKET_ERROR
, and the
specific error code can be tested with
WSAGetLastError
.
listen()
The
listen()
function establishes a socket which listens for an incoming connection. The
sequence to create and accept a socket is:
•
socket()-
Creates a socket.
•
listen()-
this creates a queue for incoming connections and is typically used by a
server that can have more than one connection at a time.
•
accept()-
these connections are then accepted with accept.
The syntax of
listen()
is
int listen (SOCKET
s
, int
backlog
)
where
s
Describes a bound, unconnected socket.
backlog
Defines the queue size for the maximum number of pending connections
may grow (typically a maximum of 5).
If no error occurs then it returns a zero value. Otherwise, it returns
SOCKET_ERROR
, and the
specific error code can be tested with
WSAGetLastError
.
#include <windows.h>
#include <winsock.h>
int main(void)
{
SOCKADDR_IN sin;
SOCKET s;
s = socket(AF_INET,SOCK_STREAM,0);
if (s == INVALID_SOCKET)
{