accept 阻塞模式

SOCKET accept(
  __in     SOCKET s,
  __out    struct sockaddr* addr,
  __inout  int* addrlen
);

Parameters

s

A descriptor that identifies a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.

addr

An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the sockaddr structure was created.

addrlen

An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.

Return Value

If no error occurs, accept returns a value of type SOCKET that is a descriptor for the new socket. This returned value is a handle for the socket on which the actual connection is made.

Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

The integer referred to by addrlen initially contains the amount of space pointed to by addr. On return it will contain the actual length in bytes of the address returned.

 

代码示例

#include <winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
#include <stdio.h>

int main(void) {

	//----------------------
	// Initialize Winsock.
	WSADATA wsaData;
	int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR) {
		printf("Error at WSAStartup()\n");
		return -1;
	}

	//----------------------
	// Create a SOCKET for listening for
	// incoming connection requests.
	SOCKET ListenSocket;
	ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ListenSocket == INVALID_SOCKET) {
		printf("Error at socket(): %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.
	sockaddr_in service;
	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr("127.0.0.1");
	service.sin_port = htons(80);

	if (bind(ListenSocket,
		(SOCKADDR*)&service,
		sizeof(service)) == SOCKET_ERROR) {
		printf("bind() failed.%d\n",WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return -1;
	}

	//----------------------
	// Listen for incoming connection requests.
	// on the created socket
	if (listen(ListenSocket, 1) == SOCKET_ERROR) {
		printf("Error listening on socket.\n");
		closesocket(ListenSocket);
		WSACleanup();
		return -1;
	}

	//----------------------
	// Create a SOCKET for accepting incoming requests.
	SOCKET AcceptSocket;
	printf("Waiting for client to connect...\n");

	//----------------------
	// Accept the connection.
	AcceptSocket = accept(ListenSocket, NULL, NULL);
	if (AcceptSocket == INVALID_SOCKET) {
		printf("accept failed: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return -1;
	}
	else
		printf("Client connected.\n");

        closesocket(AcceptSocket);
	// No longer need server socket
	closesocket(ListenSocket);

	WSACleanup();
	return 0;
}

使用 accept 读取用户连接的信息,成功的话创建一个新的SOCKET 用来与当前连接的客户端单独通信 每一个连接都会生成一个socket

只要使用accept,它就会一直等待监听的socket,直到有客户端连接 每次不读取的时候记得关闭资源

 

4.png

它在这死等 这里 bind的IP是本机IP 那再打开一个窗口连接试下吧

QQ截图20190404132011.png

 

本博客所有文章如无特别注明均为原创。作者:odaycaogen复制或转载请以超链接形式注明转自 123``blog
原文地址《accept 阻塞模式

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)