欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++结合TCP/IP协议 实现客户端和服务器通讯

程序员文章站 2022-05-15 21:04:48
借鉴,非原创,来自magicsaring的一个简单的tcp通信程序demo。 一、客户端源码: #include #include #include "client.h&quo...

借鉴,非原创,来自magicsaring的一个简单的tcp通信程序demo。

一、客户端源码:

#include 
#include 
#include "client.h"
using namespace std;

#pragma comment(lib, "ws2_32.lib")
//表示链接ws2_32.lib这个库。
#define  port 1024

int connecttoserver(char* send_msg)
{
	socket sock_client;
	struct sockaddr_in server_addr;
	int addr_len = sizeof(struct sockaddr_in);
	char msgbuffer[1000];
	memset(msgbuffer, 0, sizeof(msgbuffer));
	// 初始化 winsock2.dll[12/27/2017 magicscaring]
	wsadata wsadata;
	word wversionrequested = makeword(2, 2);  //生成版本号
	if (wsastartup(wversionrequested, &wsadata) != 0)
	{
		cout << "加载 winsock.dll失败" << endl;
		return 0;
	}
	// 创建套接字 [12/27/2017 magicscaring]
	if ((sock_client = socket(af_inet, sock_stream, 0)) == socket_error)
	{
		cout << "创建套接字失败! 错误代码:" << wsagetlasterror() << endl;
		wsacleanup(); //注销winsock动态链接库
		return 0;
	}
	// 填写服务器地址 [本机地址]
	char ip[20] = { "192.168.1.106" };
	/*char ip[20];
	cout << "输入服务器地址:" << endl;
	cin >> ip;*/
	memset((void*)&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = af_inet;
	server_addr.sin_port = htons(port);
	server_addr.sin_addr.s_addr = inet_addr(ip);

	// 与服务器建立连接 [2018/5/22]
	if (connect(sock_client, (struct sockaddr*)&server_addr, addr_len) == socket_error)
	{
		cout << "连接失败! 错误代码:" << wsagetlasterror() << endl;
		closesocket(sock_client);
		wsacleanup();
		return 0;
	}
	while (true)
	{
		int size;
		if ((size = recv(sock_client, msgbuffer, sizeof(msgbuffer), 0)) == socket_error)
		{
			cout << "接收信息失败! 错误代码:" << wsagetlasterror() << endl;
			closesocket(sock_client);
			wsacleanup();
			return 0;
		}
		else if (size == 0)
		{
			cout << "对方已关闭连接" << endl;
			closesocket(sock_client);
			wsacleanup();
			return 0;
		}
		else
		{
			cout << "the message from server:" << msgbuffer << endl;
		}
		//char send_msg[1000] = { "hello server" };
		//send_msg是要发送的数据,是char指针
		if ((size = send(sock_client, send_msg, sizeof(send_msg), 0)) == socket_error)
		{
			cout << "发送信息失败! 错误代码:" << wsagetlasterror() << endl;
		}
		else if (size == 0)
		{
			cout << "对方已关闭连接" << endl;
		}
		else
		{
			cout << "信息发送成功" << endl;
		}

	}
	closesocket(sock_client);
	wsacleanup();

	return 0;
}

二、主程序调用

connecttoserver(s_buf);

注意主程序头文件库的顺序,winsock2.h放在最前面。

至于为什么,i dont know.

三、服务器源码

#include "stdafx.h"
#include 
#include 
using namespace std;

#pragma comment(lib, "ws2_32.lib")
//表示链接ws2_32.lib这个库。
#define  port 1024

int main()
{
	socket sock_server, newsock;
	struct sockaddr_in server_addr, client_addr;
	char msg[] = { "hello client" };
	// 初始化 winsock2.dll[12/27/2017 magicscaring]
	wsadata wsadata;
	word wversionrequested = makeword(2, 2);  //生成版本号
	if (wsastartup(wversionrequested, &wsadata) != 0)
	{
		cout << "加载 winsock.dll失败" << endl;
		return 0;
	}
	// 创建套接字 [12/27/2017 magicscaring]
	if ((sock_server = socket(af_inet, sock_stream, 0)) == socket_error)
	{
		cout << "创建套接字失败! 错误代码:" << wsagetlasterror() << endl;
		wsacleanup(); //注销winsock动态链接库
		return 0;
	}
	// 填写需要绑定的本地地址 []
	int addr_len = sizeof(struct sockaddr_in);
	memset((void*)&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = af_inet;
	server_addr.sin_port = htons(port);
	server_addr.sin_addr.s_addr = htonl(inaddr_any);

	if (bind(sock_server, (struct sockaddr*)&server_addr, addr_len) != 0)
	{
		cout << "绑定失败!错误代码:" << wsagetlasterror() << endl;
		closesocket(sock_server);//关闭已连接套接字
		wsacleanup();//注销winsock动态链接库
		return 0;
	}

	// 开始监听 [12/27/2017 magicscaring]
	if (listen(sock_server, 0) != 0)
	{
		cout << "listen调用失败!错误代码:" << wsagetlasterror() << endl;
		closesocket(sock_server);
		wsacleanup();
		return 0;
	}
	else
	{
		cout << "listening...." << endl;
	}
	// 循环:接收连接请求并收发数据 [12/27/2017 magicscaring]
	int size;
	while (true)
	{
		if ((newsock = accept(sock_server, (struct sockaddr *)&client_addr, &addr_len)) == invalid_socket)
		{
			cout << "accept 函数调用失败! 错误代码:" << wsagetlasterror() << endl;
			break;
		}
		else
		{
			cout << "成功接收到一个连接请求!" << endl;
			size = send(newsock, msg, sizeof(msg), 0);
			if (size == socket_error)
			{
				cout << "发送信息失败! 错误代码:" << wsagetlasterror() << endl;
				closesocket(newsock);
				continue;
			}
			else if (size == 0)
			{
				cout << "对方已关闭连接" << endl;
				closesocket(newsock);
				continue;
			}
			else
			{
				cout << "信息发送成功" << endl;
			}
			size = recv(newsock, msg, sizeof(msg), 0);
			if (size == socket_error)
			{
				cout << "接收信息失败! 错误代码:" << wsagetlasterror() << endl;
				closesocket(newsock);
				wsacleanup();
				return 0;
			}
			else if (size == 0)
			{
				cout << "对方已关闭连接" << endl;
				closesocket(newsock);
				wsacleanup();
				return 0;
			}
			else
			{
				
				cout << "the message from client:" << msg<< endl;

			}
		}
	}

	return 0;
}