Winsock API: listen
2013-03-11 17:26
316 查看
参考MSDN:http://msdn.microsoft.com/en-us/library/windows/desktop/ms739168(v=vs.85).aspx
The listen function places a socket in a state in which it is listening for an incoming connection.
Syntax
int listen(
_In_ SOCKET s,
_In_ int backlog
);
A descriptor identifying a bound, unconnected socket.
backlog [in]
The maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket
s will set the backlog to a maximum reasonable value. There is no standard provision to obtain the actual backlog value.
SOCKET_ERROR is returned, and a specific error code can be retrieved by calling
WSAGetLastError.
socket function and bound to a local address with the
bind function. A backlog for incoming connections is specified with
listen, and then the connections are accepted with the
accept function. Sockets that are connection oriented, those of type
SOCK_STREAM for example, are used with listen. The socket
s is put into passive mode where incoming connection requests are acknowledged and queued pending acceptance by the process.
A value for the backlog of SOMAXCONN is a special constant that instructs the underlying service provider responsible for socket
s to set the length of the queue of pending connections to a maximum reasonable value.
The listen function places a socket in a state in which it is listening for an incoming connection.
Syntax
int listen(
_In_ SOCKET s,
_In_ int backlog
);
Parameters
s [in]A descriptor identifying a bound, unconnected socket.
backlog [in]
The maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket
s will set the backlog to a maximum reasonable value. There is no standard provision to obtain the actual backlog value.
Return value
If no error occurs, listen returns zero. Otherwise, a value ofSOCKET_ERROR is returned, and a specific error code can be retrieved by calling
WSAGetLastError.
Remarks
To accept connections, a socket is first created with thesocket function and bound to a local address with the
bind function. A backlog for incoming connections is specified with
listen, and then the connections are accepted with the
accept function. Sockets that are connection oriented, those of type
SOCK_STREAM for example, are used with listen. The socket
s is put into passive mode where incoming connection requests are acknowledged and queued pending acceptance by the process.
A value for the backlog of SOMAXCONN is a special constant that instructs the underlying service provider responsible for socket
s to set the length of the queue of pending connections to a maximum reasonable value.
Example Code
The following example demonstrates the use of the listen function.#ifndef UNICODE #define UNICODE #endif #define WIN32_LEAN_AND_MEAN #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> // Need to link with Ws2_32.lib #pragma comment(lib, "ws2_32.lib") int wmain() { //---------------------- // Initialize Winsock WSADATA wsaData; int iResult = 0; SOCKET ListenSocket = INVALID_SOCKET; sockaddr_in service; iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { wprintf(L"WSAStartup() failed with error: %d\n", iResult); return 1; } //---------------------- // Create a SOCKET for listening for incoming connection requests. ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ListenSocket == INVALID_SOCKET) { wprintf(L"socket function failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port for the socket that is being bound. service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr("127.0.0.1"); service.sin_port = htons(27015); iResult = bind(ListenSocket, (SOCKADDR *) & service, sizeof (service)); if (iResult == SOCKET_ERROR) { wprintf(L"bind function failed with error %d\n", WSAGetLastError()); iResult = closesocket(ListenSocket); if (iResult == SOCKET_ERROR) wprintf(L"closesocket function failed with error %d\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // Listen for incoming connection requests // on the created socket if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) wprintf(L"listen function failed with error: %d\n", WSAGetLastError()); wprintf(L"Listening on socket...\n"); iResult = closesocket(ListenSocket); if (iResult == SOCKET_ERROR) { wprintf(L"closesocket function failed with error %d\n", WSAGetLastError()); WSACleanup(); return 1; } WSACleanup(); return 0; }
相关文章推荐
- WinSock API网络编程——TCP/IP协议
- 《ASCE1885的网络编程》---Winsock APIの网络信息获取函数
- 蛙蛙推荐:c#使用winsock api实现同步Socket服务端
- Winsock API: bind
- WinSock API网络编程——TCP/IP协议详解
- Winsock API 编程介绍
- 用WINSOCK API实现同步阻塞方式的网络通讯
- Getting Started with Winsock - Windows Socket 2 API 教程文档 - WinSock API 参考 - HackerJLY
- winsock API积累
- 《ASCE1885的网络编程》---Winsock APIの网络信息获取函数
- 《ASCE1885的网络编程》---Winsock API基本函数の数据传输
- 用WINSOCK API实现同步非阻塞方式的网络通讯
- 基于VC++的网络编程接口Winsock API
- Winsock API: accept
- UDP协议的WinSock API编程示例
- 基于Visual C++的Winsock API研究
- Winsock API学习记录
- Winsock API 函数大全
- Winsock API 函数大全
- Winsock API: connect