Information Technology Reference
In-Depth Information
// Socket failed
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(100); // port=100
if (bind(s, (struct sockaddr FAR *)&sin, sizeof (sin))==SOCKET_ERROR)
{
// Bind failed
}
if (listen(s,4)==SOCKET_ERROR)
{
// Listen failed
}
return(0);
}
accept()
The accept() function accepts a connection on a socket. It extracts any pending connections
from the queue and creates a new socket with the same properties as the specified socket.
Finally, it returns a handle to the new socket. Its syntax is
SOCKET accept(SOCKET s , struct sockaddr FAR * addr , int FAR * addrlen )
where
s
Descriptor identifying a socket that is in listen mode.
addr
Pointer to a buffer that receives the address of the connecting entity, as
known to the communications layer.
addrlen
Pointer to an integer which contains the length of the address addr .
If no error occurs then it returns a zero value. Otherwise, it returns INVALID_SOCKET , and the
specific error code can be tested with WSAGetLastError .
#include <windows.h>
#include <winsock.h>
int main(void)
{
SOCKADDR_IN sin;
SOCKET s;
int sin_len;
s = socket(AF_INET,SOCK_STREAM,0);
if (s == INVALID_SOCKET)
{
// Socket failed
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(100); // port=100
Search WWH ::




Custom Search