Socket编程

C++ Socket 编程:https://blog.csdn.net/sinat_35866463/article/details/81019778

windows环境下用c++实现socket编程:https://blog.csdn.net/xiaoquantouer/article/details/58001960

win7系统两台电脑之间利用Socket实现文件传输---C++实现:https://blog.csdn.net/sinat_33718563/article/details/80360268

带小工程:https://www.cnblogs.com/devinblog/p/4766085.html

一台机器上:

服务端:

#include 
#include 

#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main() {
	WORD sockV = MAKEWORD(2, 0);
	WSADATA wsaD;
	if (WSAStartup(sockV, &wsaD) != 0) {
		cout << "init error\n";
		return 0;
	}
	SOCKET msock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (msock == INVALID_SOCKET) {
		cout << "socket error\n";
		return 0;
	}
	sockaddr_in s_in;
	s_in.sin_family = AF_INET;
	s_in.sin_port = htons(8888);
	s_in.sin_addr.S_un.S_addr = INADDR_ANY;
	if (bind(msock, (LPSOCKADDR)&s_in, sizeof(s_in)) == SOCKET_ERROR) {
		cout << "bind error\n";
		return 0;
	}
	if (listen(msock, 10) == SOCKET_ERROR) {
		cout << "listen error\n";
		return 0;
	}
	SOCKET sClient;
	sockaddr_in remoteAddr;
	int rAlen = sizeof(remoteAddr);
	char revDate[255], sendDate[255];
	int flag = 1;
	while (1) {
		if (flag) {
			cout << "等待连接...\n";
			sClient = accept(msock, (SOCKADDR*)&remoteAddr, &rAlen);
			if (sClient == INVALID_SOCKET) {
				cout << "accept error\n";
				continue;
			}
			cout << "收到一个连接:" << inet_ntoa(remoteAddr.sin_addr) << endl;
			flag = 0;
		}
		
		int ret = recv(sClient, revDate, 255, 0);
		if (ret > 0) {
			revDate[ret] = '\0';
			cout << revDate << endl;
		}
		cin >> sendDate;
		send(sClient, sendDate, strlen(sendDate), 0);
	}
	closesocket(sClient);
	closesocket(msock);
	WSACleanup();
	return 0;
}

客户端:

#include 
#include 
#include 
#include 
using namespace std;

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

int main() {
	WORD sockV = MAKEWORD(2, 0);
	WSADATA wsaD;
	if (WSAStartup(sockV, &wsaD) != 0) {
		cout << "init error\n";
		return 0;
	}
	char sendData[255];
	char recData[255];
	SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sClient == INVALID_SOCKET) {
		cout << "invalid socket!\n";
		return 0;
	}

	sockaddr_in serverAddr;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(8888);
	serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	int flag = 1;
	while (1) {
		if (flag) {
			if (connect(sClient, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
				cout << "connect error !\n";
				closesocket(sClient);
				return 0;
			}
			cout << "connect success\n";
			flag = 0;
		}
		
		cin >> sendData;
		send(sClient, sendData, strlen(sendData), 0);
		
		
		int ret = recv(sClient, recData, 255, 0);
		if (ret > 0) {
			recData[ret] = '\0';
			cout << recData << endl;;
		}
		
	}

	closesocket(sClient);

	WSACleanup();
	return 0;

}

在VS下使用inet_pton()和inet_ntop() 进行转换:https://blog.csdn.net/zyy617532750/article/details/58595700

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

using namespace std;
int main() {
	WORD sockV = MAKEWORD(2, 0);
	WSADATA wsaD;
	if (WSAStartup(sockV, &wsaD) != 0) {
		cout << "init error\n";
		return 0;
	}
	SOCKET msock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (msock == INVALID_SOCKET) {
		cout << "socket error\n";
		return 0;
	}
	sockaddr_in s_in;
	s_in.sin_family = AF_INET;
	s_in.sin_port = htons(8888);
	s_in.sin_addr.S_un.S_addr = INADDR_ANY;
	if (bind(msock, (LPSOCKADDR)&s_in, sizeof(s_in)) == SOCKET_ERROR) {
		cout << "bind error\n";
		return 0;
	}
	if (listen(msock, 10) == SOCKET_ERROR) {
		cout << "listen error\n";
		return 0;
	}
	SOCKET sClient;
	sockaddr_in remoteAddr;
	int rAlen = sizeof(remoteAddr);
	char revDate[255], sendDate[255];
	int flag = 1;
	char addr_n[255];
	while (1) {
		if (flag) {
			cout << "等待连接...\n";
			sClient = accept(msock, (SOCKADDR*)&remoteAddr, &rAlen);
			if (sClient == INVALID_SOCKET) {
				cout << "accept error\n";
				continue;
			}
			cout << "收到一个连接:" << inet_ntop(AF_INET, &remoteAddr.sin_addr, addr_n, sizeof(addr_n)) << endl;
			flag = 0;
		}

		int ret = recv(sClient, revDate, 255, 0);
		if (ret > 0) {
			revDate[ret] = '\0';
			cout << revDate << endl;
		}
		cin >> sendDate;
		send(sClient, sendDate, strlen(sendDate), 0);
	}
	closesocket(sClient);
	closesocket(msock);
	WSACleanup();
	return 0;
}
#include 
#include
#include
#include
#include 
#include 
#include 
#include 
using namespace std;
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib, "wsock32.lib")
using namespace std;


int main() {
	WORD sockV = MAKEWORD(2, 0);
	WSADATA wsaD;
	if (WSAStartup(sockV, &wsaD) != 0) {
		cout << "init error\n";
		return 0;
	}
	char sendData[255];
	char recData[255];
	SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sClient == INVALID_SOCKET) {
		cout << "invalid socket!\n";
		return 0;
	}

	sockaddr_in serverAddr;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(8888);
	inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr);
	int flag = 1;
	while (1) {
		if (flag) {
			if (connect(sClient, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
				cout << "connect error !\n";
				closesocket(sClient);
				return 0;
			}
			cout << "connect success\n";
			flag = 0;
		}

		cin >> sendData;
		send(sClient, sendData, strlen(sendData), 0);


		int ret = recv(sClient, recData, 255, 0);
		if (ret > 0) {
			recData[ret] = '\0';
			cout << recData << endl;;
		}

	}

	closesocket(sClient);

	WSACleanup();
	return 0;

}

UDP:不需要建立连接

服务端:

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

using namespace std;
int main() {
	WORD sockV = MAKEWORD(2, 0);
	WSADATA wsaD;
	if (WSAStartup(sockV, &wsaD) != 0) {
		cout << "init error\n";
		return 0;
	}
	SOCKET msock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (msock == INVALID_SOCKET) {
		cout << "socket error\n";
		return 0;
	}
	sockaddr_in s_in;
	s_in.sin_family = AF_INET;
	s_in.sin_port = htons(8888);
	s_in.sin_addr.S_un.S_addr = INADDR_ANY;
	if (bind(msock, (LPSOCKADDR)&s_in, sizeof(s_in)) == SOCKET_ERROR) {
		cout << "bind error\n";
		return 0;
	}

	SOCKET sClient;
	sockaddr_in remoteAddr;
	int rAlen = sizeof(remoteAddr);
	char revDate[255], sendDate[255];
	int flag = 1;
	char addr_n[255];
	while (1) {

		int ret = recvfrom(msock, revDate, 255, 0,(SOCKADDR*)&remoteAddr, &rAlen);
		cout << ret << endl;
		if (ret > 0) {
			cout << "收到一个连接:" << inet_ntop(AF_INET, &remoteAddr.sin_addr, addr_n, sizeof(addr_n)) << endl;
			revDate[ret] = '\0';
			cout << revDate << endl;
		}
		cin >> sendDate;
		sendto(msock, sendDate, strlen(sendDate), 0, (SOCKADDR*)&remoteAddr, rAlen);
	}
	closesocket(sClient);
	closesocket(msock);
	WSACleanup();
	return 0;
}

客户端:

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


int main() {
	WORD sockV = MAKEWORD(2, 0);
	WSADATA wsaD;
	if (WSAStartup(sockV, &wsaD) != 0) {
		cout << "init error\n";
		return 0;
	}
	char sendData[255];
	char recData[255];
	SOCKET sClient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sClient == INVALID_SOCKET) {
		cout << "invalid socket!\n";
		return 0;
	}

	sockaddr_in serverAddr;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(8888);
	inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr);
	int flag = 1;
	int len = sizeof(serverAddr);
	while (1) {

		cin >> sendData;
		sendto(sClient, sendData, strlen(sendData), 0, (sockaddr*)&serverAddr, len);

		
		int ret = recvfrom(sClient, recData, 255, 0, (sockaddr*)&serverAddr, &len);
		if (ret > 0) {
			recData[ret] = '\0';
			cout << recData << endl;;
		}

	}

	closesocket(sClient);

	WSACleanup();
	return 0;

}

 

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