socket学习-粗糙的实现

虽然很想搞底层的东西,但是一直没啥机会
写写配置,改改逻辑,那些重要的东西都忘记了
还是得找时间自己更深入点

这份东西可以与cmd的telnet命令配合调试

#include #WinSock2.h#
#include #stdio.h#
 
#pragma comment(lib, "WS2_32")
 
int main(int argc, char* argv[])
{
	int ret = 0;
	int port = 7070;
	int backlog = SOMAXCONN;
 
	// 初始化部分
	struct WSAData wsa;
	WORD sockVersion = MAKEWORD(2, 2);
	ret = (int)WSAStartup(sockVersion, &wsa);
	if (0 != ret)
	{
		printf("WSAStartup fail.\n");
		return 0;
	}
 
	// 创建socket
	SOCKET listsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == listsock)
	{
		ret = WSACleanup();
		if (0 != ret)
		{
			printf("WSACleanup fail.\n");
		}
		return 0;
	}
	unsigned long enable = 1;
	ret = setsockopt(listsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&enable, sizeof(unsigned long));
	if (SOCKET_ERROR == ret)
	{
		ret = closesocket(listsock);
		if (0 != ret)
		{
			printf("close socket fail.\n");
		}
		ret = WSACleanup();
		if (0 != ret)
		{
			printf("WSACleanup fail.\n");
		}
		return 0;
	}
 
	// 绑定端口
	sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	ret = bind(listsock, (sockaddr*)&addr, sizeof(struct sockaddr));
	if (SOCKET_ERROR ==  ret)
	{
		ret = closesocket(listsock);
		if (0 != ret)
		{
			printf("close socket fail.\n");
		}
		ret = WSACleanup();
		if (0 != ret)
		{
			printf("WSACleanup fail.\n");
		}
		return 0;
	}
 
	// 监听
	ret = listen(listsock,backlog);
	if (SOCKET_ERROR == ret)
	{
		ret = closesocket(listsock);
		if (0 != ret)
		{
			printf("close socket fail.\n");
		}
		ret = WSACleanup();
		if (0 != ret)
		{
			printf("WSACleanup fail.\n");
		}
		return 0;
	}
 
	// 接受
	sockaddr_in clientaddr;
	int clientaddrlen = sizeof(struct sockaddr);
	SOCKET clientsock = accept(listsock, (struct sockaddr*)&clientaddr, &clientaddrlen);
	if (INVALID_SOCKET == clientsock)
	{
		ret = closesocket(listsock);
		if (0 != ret)
		{
			printf("close socket fail.\n");
		}
		ret = WSACleanup();
		if (0 != ret)
		{
			printf("WSACleanup fail.\n");
		}
		return 0;
	}
 
	// 进行传输
	char buff[1024];
	ret = 1;
	while(ret > 0)
	{
		ret = recv(clientsock, buff, 1024, 0);
		buff[ret] = 0;
		printf("%s",buff);
		if (ret > 0)
		{
			ret = send( clientsock, buff, ret, 0 );
			if (ret == SOCKET_ERROR)
			{
				ret = closesocket(clientsock);
				if (0 != ret)
				{
					printf("close client socket fail.\n");
				}
 
				ret = closesocket(listsock);
				if (0 != ret)
				{
					printf("close socket fail.\n");
				}
 
				ret = WSACleanup();
				if (0 != ret)
				{
					printf("WSACleanup fail.\n");
				}
				return 0;
			}
		}
		else if (ret == 0)
		{
			printf("Connection closing...\n");
		}
		else
		{
			ret = closesocket(clientsock);
			if (0 != ret)
			{
				printf("close client socket fail.\n");
			}
 
			ret = closesocket(listsock);
			if (0 != ret)
			{
				printf("close socket fail.\n");
			}
 
			ret = WSACleanup();
			if (0 != ret)
			{
				printf("WSACleanup fail.\n");
			}
			return 0;
		}
	}
 
	// 结束传输
	ret = shutdown(clientsock, SD_SEND);
	if (ret == SOCKET_ERROR)
	{
		ret = closesocket(clientsock);
		if (0 != ret)
		{
			printf("close client socket fail.\n");
		}
 
		ret = closesocket(listsock);
		if (0 != ret)
		{
			printf("close socket fail.\n");
		}
 
		ret = WSACleanup();
		if (0 != ret)
		{
			printf("WSACleanup fail.\n");
		}
		return 0;
	}
 
	// 结束
	ret = closesocket(clientsock);
	if (0 != ret)
	{
		printf("close socket fail.\n");
	}
	ret = closesocket(listsock);
	if (0 != ret)
	{
		printf("close socket fail.\n");
	}
	ret = WSACleanup();
	if (0 != ret)
	{
		printf("WSACleanup fail.\n");
	}
 
	system("pause");
	return 0;
}
你可以留言,或者trackback 从你的网站

留言哦