非阻塞式TCP 服务器/客户端 基础

服务器端

------------------------------------------------------------------------

 

#define _WINSOCK_DEPRECATED_NO_WARNINGS

 

#include 

#pragma comment(lib, "ws2_32.lib")

 

#include 

 

 

#define BUF_SIZE 64

 

int main()

{

WSADATA wsaData;

WSAStartup(MAKEWORD(2, 2), &wsaData);

 

SOCKET sockServer;

sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (INVALID_SOCKET == sockServer)

{

WSACleanup();

return -1;

}

 

int mode = 1;

int nRes = 0;

nRes = ioctlsocket(sockServer, FIONBIO, (u_long*)&mode);

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

SOCKADDR_IN  addrServ;

addrServ.sin_family = AF_INET;

addrServ.sin_port = htons(2345);

addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

 

nRes = bind(sockServer, (sockaddr*)&addrServ, sizeof(sockaddr));

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

nRes = listen(sockServer, 5);

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

printf("服务器启动成功\n");

 

SOCKET sockClient;

SOCKADDR_IN addrClient;

int nLen = sizeof(SOCKADDR);

while (true)

{

sockClient = accept(sockServer, (sockaddr*)&addrClient, &nLen);

if (INVALID_SOCKET == sockClient)

{

nRes = WSAGetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(100);

continue;

}

else

{

closesocket(sockServer);

WSACleanup();

return -1;

}

}

 

break;

}

 

 

char chBuf[1024];

 

while (true)

{

nRes = recv(sockClient, chBuf, BUF_SIZE, 0);

if (SOCKET_ERROR == nRes)

{

nRes = WSAGetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(300);

continue;

}

else

{

if (WSAETIMEDOUT == nRes || WSAENETDOWN == nRes)

{

closesocket(sockServer);

closesocket(sockClient);

WSACleanup();

return -1;

}

}

}

else

{

printf("客户端信息:%s\n", chBuf);

}

 

if (0 == strcmp(chBuf, "quit"))

{

exit(-1);

}

else

{

while (true)

{

ZeroMemory(chBuf, BUF_SIZE);

printf("---请输入:");

gets_s(chBuf);

nRes = send(sockClient, chBuf, strlen(chBuf)+2, 0);

if (SOCKET_ERROR == nRes)

{

nRes = GetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(300);

continue;

}

else

{

closesocket(sockServer);

closesocket(sockClient);

WSACleanup();

return -1;

}

}

break;

}

}

}

 

return 0;

}

 

 

 


客户端

 

 

#define _WINSOCK_DEPRECATED_NO_WARNINGS

 

#include 

#pragma comment(lib, "ws2_32.lib")

#include   

#include 

 

#define BUF_SIZE 64

 

int main()

{

WSAData wsaData;

int nRes = 0;

nRes = WSAStartup(MAKEWORD(2, 2), &wsaData);

 

SOCKET sockServer;

sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (INVALID_SOCKET == sockServer)

{

WSACleanup();

return -1;

}

 

int nMode = 1;

nRes = ioctlsocket(sockServer, FIONBIO, (u_long*)&nMode);

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

sockaddr_in addrServer;

addrServer.sin_family = AF_INET;

addrServer.sin_port = htons(2345);

addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

 

int nLen = sizeof(sockaddr);

 

while (true)

{

nRes = connect(sockServer, (sockaddr*)&addrServer, sizeof(sockaddr));

if (SOCKET_ERROR == nRes)

{

nRes = WSAGetLastError();

if (WSAEWOULDBLOCK == nRes || WSAEINVAL == nRes)

{

Sleep(300);

continue;

}

else if(WSAEISCONN == nRes)

{

break;

}

else

{

closesocket(sockServer);

WSACleanup();

return 0;

}

}

}

char chBuf[BUF_SIZE];

printf("和服务器连接成功\n");

 

while (true)

{

while (true)

{

ZeroMemory(chBuf, BUF_SIZE);

printf("---请输入:");

gets_s(chBuf);

 

nRes = send(sockServer, chBuf, strlen(chBuf)+2, 0);

if (SOCKET_ERROR == nRes)

{

nRes = GetLastError();

if (WSAEWOULDBLOCK == nRes )

{

Sleep(300);

continue;

}

else

{

closesocket(sockServer);

WSACleanup();

return 0;

}

}

break;

}

 

while (true)

{

nRes = recv(sockServer, chBuf, BUF_SIZE, 0);

if (SOCKET_ERROR == nRes)

{

nRes = GetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(300);

continue;

}

else

{

closesocket(sockServer);

WSACleanup();

return 0;

}

}

printf("服务器信息:%s\n", chBuf);

break;

}

}

 

return 0;

}

 

 

 

 

 

 

C++11 多线程

 

#define _WINSOCK_DEPRECATED_NO_WARNINGS

 

#include 

#pragma comment(lib, "ws2_32.lib")

 

#include 

#include 

#define BUF_SIZE 64

 

#include 

 

void connThread(SOCKET sock)

{

char chBuf[1024];

int nRes = 0;

 

while (true)

{

nRes = recv(sock, chBuf, BUF_SIZE, 0);

if (SOCKET_ERROR == nRes)

{

nRes = WSAGetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(300);

continue;

}

else

{

if (WSAETIMEDOUT == nRes || WSAENETDOWN == nRes)

{

closesocket(sock);

return;

}

}

}

else

{

std::thread::id  id = std::this_thread::get_id();

std::cout << "线程" << id;

printf("----客户端信息:%s\n", chBuf);

}

 

if (0 == strcmp(chBuf, "quit"))

{

exit(-1);

}

else

{

while (true)

{

ZeroMemory(chBuf, BUF_SIZE);

std::thread::id  id = std::this_thread::get_id();

std::cout << "线程" << id;

printf("---请输入:");

gets_s(chBuf);

nRes = send(sock, chBuf, strlen(chBuf) + 2, 0);

if (SOCKET_ERROR == nRes)

{

nRes = GetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(300);

continue;

}

else

{

closesocket(sock);

return;

}

}

break;

}

}

}

}

 

 

 

int main()

{

WSADATA wsaData;

WSAStartup(MAKEWORD(2, 2), &wsaData);

 

SOCKET sockServer;

sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (INVALID_SOCKET == sockServer)

{

WSACleanup();

return -1;

}

 

int mode = 1;

int nRes = 0;

nRes = ioctlsocket(sockServer, FIONBIO, (u_long*)&mode);

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

SOCKADDR_IN  addrServ;

addrServ.sin_family = AF_INET;

addrServ.sin_port = htons(2345);

addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

 

nRes = bind(sockServer, (sockaddr*)&addrServ, sizeof(sockaddr));

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

nRes = listen(sockServer, 5);

if (SOCKET_ERROR == nRes)

{

closesocket(sockServer);

WSACleanup();

return -1;

}

 

printf("服务器启动成功\n");

 

SOCKET sockClient;

SOCKADDR_IN addrClient;

int nLen = sizeof(SOCKADDR);

while (true)

{

sockClient = accept(sockServer, (sockaddr*)&addrClient, &nLen);

if (INVALID_SOCKET == sockClient)

{

nRes = WSAGetLastError();

if (WSAEWOULDBLOCK == nRes)

{

Sleep(100);

continue;

}

else

{

closesocket(sockServer);

WSACleanup();

return -1;

}

}

 

std::thread threadA(connThread, sockClient);

threadA.detach();

Sleep(600);

}

return 0;

}

 







服务器端
------------------------------------------------------------------------

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include 
#pragma comment(lib, "ws2_32.lib")

#include 


#define BUF_SIZE 64

int main()
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);

	SOCKET sockServer;
	sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sockServer)
	{
		WSACleanup();
		return -1;
	}

	int mode = 1;
	int nRes = 0;
	nRes = ioctlsocket(sockServer, FIONBIO, (u_long*)&mode);
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	SOCKADDR_IN  addrServ;
	addrServ.sin_family = AF_INET;
	addrServ.sin_port = htons(2345);
	addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	nRes = bind(sockServer, (sockaddr*)&addrServ, sizeof(sockaddr));
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	nRes = listen(sockServer, 5);
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	printf("服务器启动成功\n");

	SOCKET sockClient;
	SOCKADDR_IN addrClient;
	int nLen = sizeof(SOCKADDR);
	while (true)
	{
		sockClient = accept(sockServer, (sockaddr*)&addrClient, &nLen);
		if (INVALID_SOCKET == sockClient)
		{
			nRes = WSAGetLastError();
			if (WSAEWOULDBLOCK == nRes)
			{
				Sleep(100);
				continue;
			}
			else
			{
				closesocket(sockServer);
				WSACleanup();
				return -1;
			}
		}

		break;
	}

	

	char chBuf[1024];

	while (true)
	{
		nRes = recv(sockClient, chBuf, BUF_SIZE, 0);
		if (SOCKET_ERROR == nRes)
		{
			nRes = WSAGetLastError();
			if (WSAEWOULDBLOCK == nRes)
			{
				Sleep(300);
				continue;
			}
			else
			{
				if (WSAETIMEDOUT == nRes || WSAENETDOWN == nRes)
				{
					closesocket(sockServer);
					closesocket(sockClient);
					WSACleanup();
					return -1;
				}
			}
		}
		else
		{
			printf("客户端信息:%s\n", chBuf);
		}

		if (0 == strcmp(chBuf, "quit"))
		{
			exit(-1);
		}
		else
		{
			while (true)
			{
				ZeroMemory(chBuf, BUF_SIZE);
				printf("---请输入:");
				gets_s(chBuf);
				nRes = send(sockClient, chBuf, strlen(chBuf)+2, 0);
				if (SOCKET_ERROR == nRes)
				{
					nRes = GetLastError();
					if (WSAEWOULDBLOCK == nRes)
					{
						Sleep(300);
						continue;
					}
					else
					{
						closesocket(sockServer);
						closesocket(sockClient);
						WSACleanup();
						return -1;
					}
				}
				break;
			}
		}
	}

	return 0;
}




客户端


#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include 
#pragma comment(lib, "ws2_32.lib")
#include   
#include 

#define BUF_SIZE 64

int main()
{
	WSAData wsaData;
	int nRes = 0;
	nRes = WSAStartup(MAKEWORD(2, 2), &wsaData);

	SOCKET sockServer;
	sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sockServer)
	{
		WSACleanup();
		return -1;
	}

	int nMode = 1;
	nRes = ioctlsocket(sockServer, FIONBIO, (u_long*)&nMode);
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	sockaddr_in addrServer;
	addrServer.sin_family = AF_INET;
	addrServer.sin_port = htons(2345);
	addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	int nLen = sizeof(sockaddr);

	while (true)
	{
		nRes = connect(sockServer, (sockaddr*)&addrServer, sizeof(sockaddr));
		if (SOCKET_ERROR == nRes)
		{
			nRes = WSAGetLastError();
			if (WSAEWOULDBLOCK == nRes || WSAEINVAL == nRes)
			{
				Sleep(300);
				continue;
			}
			else if(WSAEISCONN == nRes)
			{
				break;
			}
			else
			{
				closesocket(sockServer);
				WSACleanup();
				return 0;
			}
		}
	}
	
	char chBuf[BUF_SIZE];
	printf("和服务器连接成功\n");

	while (true)
	{
		while (true)
		{
			ZeroMemory(chBuf, BUF_SIZE);
			printf("---请输入:");
			gets_s(chBuf);

			nRes = send(sockServer, chBuf, strlen(chBuf)+2, 0);
			if (SOCKET_ERROR == nRes)
			{
				nRes = GetLastError();
				if (WSAEWOULDBLOCK == nRes )
				{
					Sleep(300);
					continue;
				}
				else
				{
					closesocket(sockServer);
					WSACleanup();
					return 0;
				}
			}
			break;
		}

		while (true)
		{
			nRes = recv(sockServer, chBuf, BUF_SIZE, 0);
			if (SOCKET_ERROR == nRes)
			{
				nRes = GetLastError();
				if (WSAEWOULDBLOCK == nRes)
				{
					Sleep(300);
					continue;
				}
				else
				{
					closesocket(sockServer);
					WSACleanup();
					return 0;
				}
			}
			printf("服务器信息:%s\n", chBuf);
			break;
		}
	}

	return 0;
}






C++11 多线程

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include 
#pragma comment(lib, "ws2_32.lib")

#include 
#include 
#define BUF_SIZE 64

#include 

void connThread(SOCKET sock) 
{
	char chBuf[1024];
	int nRes = 0;

	while (true)
	{
		nRes = recv(sock, chBuf, BUF_SIZE, 0);
		if (SOCKET_ERROR == nRes)
		{
			nRes = WSAGetLastError();
			if (WSAEWOULDBLOCK == nRes)
			{
				Sleep(300);
				continue;
			}
			else
			{
				if (WSAETIMEDOUT == nRes || WSAENETDOWN == nRes)
				{
					closesocket(sock);
					return;
				}
			}
		}
		else
		{
			std::thread::id  id = std::this_thread::get_id();
			std::cout << "线程" << id;
			printf("----客户端信息:%s\n", chBuf);
		}

		
		if (0 == strcmp(chBuf, "quit"))
		{
			exit(-1);
		}
		else
		{
			while (true)
			{
				ZeroMemory(chBuf, BUF_SIZE);
				std::thread::id  id = std::this_thread::get_id();
				std::cout << "线程" << id;
				printf("---请输入:");
				gets_s(chBuf);
				nRes = send(sock, chBuf, strlen(chBuf) + 2, 0);
				if (SOCKET_ERROR == nRes)
				{
					nRes = GetLastError();
					if (WSAEWOULDBLOCK == nRes)
					{
						Sleep(300);
						continue;
					}
					else
					{
						closesocket(sock);
						return;
					}
				}
				break;
			}
		}
	}
}



int main()
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);

	SOCKET sockServer;
	sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sockServer)
	{
		WSACleanup();
		return -1;
	}

	int mode = 1;
	int nRes = 0;
	nRes = ioctlsocket(sockServer, FIONBIO, (u_long*)&mode);
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	SOCKADDR_IN  addrServ;
	addrServ.sin_family = AF_INET;
	addrServ.sin_port = htons(2345);
	addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	nRes = bind(sockServer, (sockaddr*)&addrServ, sizeof(sockaddr));
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	nRes = listen(sockServer, 5);
	if (SOCKET_ERROR == nRes)
	{
		closesocket(sockServer);
		WSACleanup();
		return -1;
	}

	printf("服务器启动成功\n");

	SOCKET sockClient;
	SOCKADDR_IN addrClient;
	int nLen = sizeof(SOCKADDR);
	while (true)
	{
		sockClient = accept(sockServer, (sockaddr*)&addrClient, &nLen);
		if (INVALID_SOCKET == sockClient)
		{
			nRes = WSAGetLastError();
			if (WSAEWOULDBLOCK == nRes)
			{
				Sleep(100);
				continue;
			}
			else
			{
				closesocket(sockServer);
				WSACleanup();
				return -1;
			}
		}

		std::thread threadA(connThread, sockClient);
		threadA.detach();
		Sleep(600);
	}
	return 0;
}


你可能感兴趣的:(C++)