C++_Windows Socket 学习记录_01

主要实现服务器-服务器传输消息

Server.cpp
#include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #include <iostream> #pragma comment(lib,"ws2_32.lib") using namespace std; #define PORT 5000 #define SendBUFSIZE 512 #define RevBUFSIZE 512 void ErrorHanding(const char* message); int main(int argc, char* argv[]) { //Init wasData WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { ErrorHanding("WSAStartup() error"); return 0; } //Send buff RevBuff //接受缓冲区 发送缓冲区 char SendMes[SendBUFSIZE] = {}; char RevMes[RevBUFSIZE] = {}; //Create Socket SOCKET ServerSocK, ClientSocK; ServerSocK = socket(PF_INET, SOCK_STREAM, 0); if (ServerSocK == INVALID_SOCKET) { ErrorHanding("Create Socket Error"); return 0; } //Bind Socket //将Socket 与协议族 IP地址 端口号信息进行绑定 SOCKADDR_IN servAddr; memset(&servAddr, 0, sizeof(servAddr)); //sin_family addrs port //端口号信息 Ip地址 servAddr.sin_family = AF_INET; servAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(PORT); if (bind(ServerSocK, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) { ErrorHanding("bind error"); return 0; } //改为监听状态 if (listen(ServerSocK, 5) == SOCKET_ERROR) { ErrorHanding("Listen error"); return 0; } cout << "wait client to connect" << endl; //等待客户端进行连接 ClientSocK = accept(ServerSocK, nullptr, nullptr); if (ClientSocK == INVALID_SOCKET) { ErrorHanding("Accept error"); } while (true) { cout << "ServerSend:"; cin >> SendMes; if (send(ClientSocK, SendMes, sizeof(SendMes), 0) < 0) { //客户端退出时发送失败推出closeSocket ErrorHanding("Send Error"); break; } if (recv(ClientSocK, RevMes, RevBUFSIZE, 0) > 0) { cout<<"Recv:" << RevMes << endl; } } closesocket(ServerSocK); closesocket(ClientSocK); WSACleanup(); return 0; } void ErrorHanding(const char* message) { cout << message << endl; }

 

Client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
void ErrorHanding(const char* message);
using namespace std;
#define PORT 5000
#define SendBUFSIZE 512
#define RevBUFSIZE 512

int main()
{
	WSADATA wsaData;
	//Init wsaData过程
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		ErrorHanding("WSAStartup Error");
	}

	char Sendmessage[SendBUFSIZE] = {};
	char RevMessage[RevBUFSIZE] = {};
	SOCKET hsocket;

	//CreateSocket
	hsocket = socket(PF_INET, SOCK_STREAM, 0);
	if (hsocket == INVALID_SOCKET)
	{
		ErrorHanding("Socket error");
	}


	SOCKADDR_IN servAddr;
	memset(&servAddr,0,sizeof(servAddr));
	//指定端口号信息 IP地址
	servAddr.sin_family = AF_INET;
	servAddr.sin_port = htons(PORT);
	servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");// htonl(INADDR_ANY);
	
	//	
	if (connect(hsocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
	{
		ErrorHanding("Connect error");
		return 0;
	}

	int strlen=0;
	while (true)
	{
		strlen = recv(hsocket, RevMessage, BUFSIZ, 0);
		if (strlen > 0)
		{
			cout << "Recv:"<<RevMessage << endl;
		}
		memset(&RevMessage, 0, sizeof(RevMessage));
		cout<<"this Client Send:";
		cin >> Sendmessage;
		if (send(hsocket, Sendmessage, BUFSIZ, 0) < 0)
		{
			ErrorHanding("Send Error:");
			break;
		}
	}
	
	closesocket(hsocket);
	WSACleanup();
	return 0;
}

void ErrorHanding(const  char* message)
{
	cout << message << endl;
}

 

 

 

 

注:发送不能发送空格?

如何实现客户端/服务器同时发送消息?

注意cin 空格问题

posted on 2022-10-01 02:51  Animer  阅读(30)  评论(0编辑  收藏  举报