win socket编程(萌新初战)

       不要觉得这玩意儿很难!!!

       不要觉得这玩意儿很难!!!

       不要觉得这玩意儿很难!!!

       本文涉及的例子非常非常简单!基本没有难度!不是难度很低,是基本没有!

       好了终于能保证浏览量了...

       用的函数很简单,就是基本的winsock里面的函数,注释里面写的应该很详细了。没见过的函数百度百科就有,非常简单就可以查到。

服务器端:

#include
#include
#include
#include
using namespace std;
#pragma comment (lib,"ws2_32.lib")
int main()
{
	//初始化WSA
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA wsaData;
	if (WSAStartup(sockVersion, &wsaData) != 0)
	{
		printf("Error_1!\n");
		return 0;
	}
	printf("%s\n", wsaData.szDescription);
	printf("%s\n", wsaData.szSystemStatus);
	printf("------------------------------------\n");
	printf("------------SYSTEM START------------\n");
	printf("------------------------------------\n");

	//创建套接字
	SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (slisten == INVALID_SOCKET)
	{
		printf("socket error!\n");
		return 0;
	}
	//绑定IP和端口
	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(8888);
	sin.sin_addr.S_un.S_addr = INADDR_ANY;
	if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
	{
		printf("bind error!\n");
	}
	//开始监听
	if (listen(slisten, 5) == SOCKET_ERROR)
	{
		printf("listen error!\n");
		return 0;
	}
	//循环接收数据
	SOCKET sClient;
	sockaddr_in remoteAddr;
	int nAddrlen = sizeof(remoteAddr);
	char revData[255];
	while (true)
	{
		printf("Waiting for connection ...\n");
		sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
		if (sClient == INVALID_SOCKET)
		{
			printf("accept error!\n");
			continue;
		}
		printf("Conneted a connection ! : %s \r\n", inet_ntoa(remoteAddr.sin_addr));
		//接收数据
		int ret = recv(sClient, revData, 255, 0);
		if (ret > 0)
		{
			revData[ret] = 0x00;
			printf("The massage from the sclient(%s):\n", inet_ntoa(remoteAddr.sin_addr));
			printf(revData);
			printf("\n");
		}
		//发送数据
		const char * sendData ;
		string data;
		cin>>data;
		sendData=data.c_str();
		send(sClient, sendData, strlen(sendData), 0);
		closesocket(sClient);
	}
	closesocket(slisten);
	WSACleanup();
	return 0;
}

客户端:

#include
#include
#include
#include
#include
using namespace std;
#pragma comment(lib,"ws2_32.lib")

int main()
{
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA data;
	if (WSAStartup(sockVersion, &data) != 0)
	{
		printf("Error_1 !\n");
		return 0;
	}
	while (true)
	{
		SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (sclient == INVALID_SOCKET)
		{
			printf("invalid socket !\n");
			return 0;
		}
		sockaddr_in serAddr;
		serAddr.sin_family = AF_INET;
		serAddr.sin_port = htons(8888);
		serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
		if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
		{//链接失败
			printf("connect error !\n");
			return 0;
		}
		string data;
		cin >> data;
		const char * sendData;
		sendData = data.c_str();//大抵是因为sting不限制长度所以作者用了这个函数强转
		send(sclient, sendData, strlen(sendData), 0);
		//send()用来将数据由指定的socket传给对方主机
		//int send(int s, const void * msg, int len, unsigned int flags)
		//s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0
		//成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error
		char recData[255];
		int ret = recv(sclient, recData, 255, 0);
		if (ret > 0)
		{
			recData[ret] = 0x00;
			printf(recData);
		}
		closesocket(sclient);
	}
	WSACleanup();
	return 0;
}

代码全部基于本机测试,如果想链接外网请自行更改客户端代码当中的IP设置

你可能感兴趣的:(计算机网络编程)