滴水网络编程socket学习【一】

滴水网络编程TCP阻塞迭代,课后习题是编写出客户端及课堂演示的服务器,进行通信,以下为我写的代码示例(后续会对这方面内容进行详细解释)

//服务器
#include 

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

using std::cin;
using std::cout;
using std::endl;

void dbgprintf(const char* str)
{
	cout << str << endl;
}

//绑定、监听
SOCKET Bind_Listen(int nBacklog)
{
	//获取所需库
	WSADATA wsadata;
	WSAStartup(MAKEWORD(2, 2), &wsadata);
	//创建套接字绑定
	SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	//创建绑定的端口地址
	sockaddr_in sock_add;
	sock_add.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
	sock_add.sin_family = AF_INET;
	sock_add.sin_port = htons(8888);
	int sock_add_len = sizeof(sock_add);

	int a = bind(server, (sockaddr*)&sock_add, sock_add_len);
	if (a != NULL)
	{
		WSACleanup();
		closesocket(server);
		dbgprintf("bind error!");
		return false;
	}
	a = listen(server, nBacklog);
	if (a != 0)
	{
		WSACleanup();
		closesocket(server);
		dbgprintf("listen error!");
		return false;
	}
	return server;
}

//与客户端建立连接,并返回套接字
SOCKET AcceptConnetion(SOCKET hSocket)
{
	sockaddr_in Sock_client;
	int len = sizeof(Sock_client);
	SOCKET a = accept(hSocket, (sockaddr*)&Sock_client, &len); 
	
	if (a == 0)
	{
		WSACleanup();
		closesocket(hSocket);
		dbgprintf("connect error!");
		return false;
	}
	return a;
}

//处理客户端数据,并发送和接受
BOOL ClientConFun(SOCKET sd)
{
	int a;
	int b;
	char buff[255];
	do 
	{
		memset(buff, 0, 255);
		a = recv(sd, buff, 255, 0);
		if (a == 0)
		{
			dbgprintf("recv error!");
			break;
		}
		else if (a == -1)
		{
			dbgprintf("客户离线!");
			break;
		}
		else if(a > 0)
		{
			dbgprintf(buff);
		}
	} while (a != 0);
	
	return true;
}
//关闭连接
BOOL CloseConnect(SOCKET sd)
{
	int a = shutdown(sd, SD_SEND);
	char buf[255];
	if (a != 0)
	{
		dbgprintf("shutdown error!");
		WSACleanup();
		closesocket(sd);
		return false;
	}
	do 
	{
		a = recv(sd, buf, 255, 0);
		if (a == 0)
		{
			dbgprintf("CloseConnect->recv error!");
			WSACleanup();
			closesocket(sd);
			return false;
		}
	} while (a != 0);

	closesocket(sd);
	return true;
}
//服务器主体
void MyTcpSerFun()
{
	//绑定端口
	SOCKET ser = Bind_Listen(1);
	//接受请求
	SOCKET sd;

	bool p;
	do 
	{
		sd = AcceptConnetion(ser);
		p = ClientConFun(sd);
		closesocket(sd);
	} while (p);
	
	CloseConnect(sd);

}

int main()
{
	MyTcpSerFun();
}
//客户端
#include 
#include 
#pragma comment(lib,"ws2_32.lib")

int main(void)
{
	WSADATA wsadata;
	WSAStartup(MAKEWORD(2, 2), &wsadata);
	//创建套接字绑定
	SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	//创建绑定的端口地址
	sockaddr_in sock_add;
	sock_add.sin_addr.s_addr = inet_addr("127.0.0.1");
	sock_add.sin_family = AF_INET;
	sock_add.sin_port = htons(8888);
	int sock_add_len = sizeof(sock_add);
	int a;
	int b;
	sockaddr_in Sock_client;
	int len = sizeof(Sock_client);
	//建立连接,服务器没开,连接不成功
	do 
	{
		b = connect(server, (sockaddr*)&sock_add, sock_add_len);

	} while (b != 0);
	printf("连接成功!\n");
	char buff[255];
	do 
	{
		scanf("%s", buff);
		a = send(server, buff, 255, 0);
		if (a == 0)
		{
			WSACleanup();
			closesocket(server);
			printf("send error !\n");
		}
	} while (a != 0);

	a = shutdown(server, SD_SEND);
	char buf[255];
	if (a != 0)
	{
		printf("shutdown error!");
		WSACleanup();
		closesocket(server);
		return false;
	}
	do
	{
		a = recv(server, buf, 255, 0);
		if (a == 0)
		{
			printf("CloseConnect->recv error!");
			WSACleanup();
			closesocket(server);
			return false;
		}
	} while (a < 0);

	return 0;
}

你可能感兴趣的:(网络编程,网络,学习,tcp/ip)