简单的c++服务端与客户端的通信

        本篇博客是本人没有深入学习网络通信,对其浅浅的了解了一下,只知道怎么连接以及发送内容,若内容有什么错误的地方还麻烦各位大佬可以指出来大家一起讨论一下。

        服务端代码如下:

#include 
#include
#include
#include "winsock2.h"	//头文件
#include
#pragma comment (lib,"ws2_32.lib")
using namespace std;
DWORD WINAPI threadpro(LPVOID pParam)	//线程函数
{
	SOCKET hsock = (SOCKET)pParam;	//定义一个套接字
	char buffer[1024] = { 0 };
	char sendbuffer[1024];
	if (hsock != INVALID_SOCKET)
		cout << "Start Receive" << endl;

	while (1)	//循环接受客户端发来的消息,并且以另一种方式发送给客户端
	{
		int num = recv(hsock, buffer, 1024, 0);
		if (num >= 0)
			cout << "Receive form client" << buffer << endl;
		cout << WSAGetLastError() << endl;
		if (!strcmp(buffer, "A"))
		{
			memset(sendbuffer, 0, 1024);
			strcpy_s(sendbuffer, "B");
			int ires = send(hsock, sendbuffer, sizeof(sendbuffer), 0);
			cout << "send to client" << sendbuffer << endl;
		}
		else if (!strcmp(buffer, "C"))
		{
			memset(sendbuffer, 0, 1024);
			strcpy_s(sendbuffer, "D");
			int ires = send(hsock, sendbuffer, sizeof(sendbuffer), 0);
			cout << "send to client" << sendbuffer << endl;
		}
		else if (!strcmp(buffer, "exit"))
		{
			cout << "client close" << endl;
			cout << "sever process close" << endl;
			return 0;
		}
		else
		{
			memset(sendbuffer, 0, 1024);
			strcpy_s(sendbuffer, "ERR");
			int ires = send(hsock, sendbuffer, sizeof(sendbuffer), 0);
			cout << "send to client" << sendbuffer << endl;
		}
	}
	return 0;
}

    int main()
    {
	    WSADATA wsd;
	    DWORD err = WSAStartup(MAKEWORD(2, 2), &wsd);
	    cout << err << endl;
	    SOCKET	 m_sockserver;	//套接字
	    sockaddr_in serveraddr;	//服务器地址
	    sockaddr_in serveraddrfrom;
	    SOCKET m_server[20];	//可接受最大的连接数

	    serveraddr.sin_family = AF_INET;
	    serveraddr.sin_port = htons(4600);
	    serveraddr.sin_addr.S_un.S_addr = htonl(2130706433);
	
	    m_sockserver = socket(AF_INET, SOCK_STREAM, 0);	//定义套接字

	    int i = bind(m_sockserver, (sockaddr*) & serveraddr, sizeof(serveraddr));	
                //与该地址进行绑定,成功则返回0
	    cout << "bind:" << i << endl;

	    int imaxconnect = 20;
	    int iconnect=0;
	    int ilisret;
	    char buf[] = "this is server\0";
	    char warnbuf[] = "it is over max connect\0";
	    int len = sizeof(sockaddr);
	    while (1)
	    {
		    ilisret = listen(m_sockserver, 0);	
            //进入监听模式,流式传输只可以在监听模式下才可以连接
		    m_server[iconnect] = accept(m_sockserver, (sockaddr*)&serveraddrfrom, &len);
	        //接受连接申请

		    if (m_server[iconnect] != INVALID_SOCKET)
		    {
			    int ires = send(m_server[iconnect], buf, sizeof(buf), 0);	//向其发送内容
			    cout << "accept" << ires << endl;
			    iconnect++;
			    if (iconnect > imaxconnect)
			    {
				    int ires = send(m_server[iconnect], warnbuf, sizeof(warnbuf), 0);
			    }
			    else
			    {	//启动线程
				    HANDLE m_handle;
				    DWORD nThreadId = 0;
				    m_handle = (HANDLE)::CreateThread(NULL, 0, threadpro,     (LPVOID)m_server[--iconnect], 0, &nThreadId);
			}	
		}
	}
	WSACleanup();
	return 0;
}

 具体客户端的代码如下:

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

void main()
{
	WSADATA wsd;		//定义一个WSADATA对象
	WSAStartup(MAKEWORD(2,2),&wsd);		//初始化WSASDATA对象
	SOCKET m_sockclient;		//定义套接字
	sockaddr_in clientaddr;		//定义连接的地址

	clientaddr.sin_family = AF_INET;
	clientaddr.sin_port = htons(4600);
	clientaddr.sin_addr.S_un.S_addr = htonl(2130706433);
	m_sockclient = socket(AF_INET, SOCK_STREAM, 0);
	int i = connect(m_sockclient, (sockaddr*)&clientaddr, sizeof(clientaddr));	//与服务器地址连接
	cout << "connect" << i << endl;

	char buffer[1024];
	char inbuf[1024];
	int num;
	num = recv(m_sockclient, buffer, 1024, 0);	//接受服务端发来的信息
	if (num > 0)
	{
		cout << "Receive form server" << buffer << endl;
		while (1)	//循环向服务端发生消息
		{
			num = 0;
			cin >> inbuf;
			if (!strcmp(inbuf, "exit"))	//只要输入的不是exit就一直循环下去
			{
				send(m_sockclient, inbuf, sizeof(inbuf), 0);
				return ;
			}
			send(m_sockclient, inbuf, sizeof(inbuf), 0);
			num = recv(m_sockclient, buffer, 1024, 0);
			if (num >= 0)
				cout << "receive form server" << buffer << endl;
		}
	}
}

注意这里一直存在一个新手很无奈的问题,我已经写出来了代码并且可以运行但是一直在报错,如果遇见这样的问题可以尝试 先运行服务端的代码,在运行客户端的代码。

如上的代码运行结果如下:

简单的c++服务端与客户端的通信_第1张图片

服务端和客户端的代码书写有一些相同的地方:

下面的图可以给大家一些参考:(对于连接流传输和非连接流传输的代码架构)

简单的c++服务端与客户端的通信_第2张图片 简单的c++服务端与客户端的通信_第3张图片

对于遇到无连接流和连接流大概的步骤就如上图所示,具体细节可以期待一下下一期的博客会详细介绍服务器使用的一些函数的具体应用例如send(),bind(),listen(),recv()等等一些函数的具体用法,以及参数所代表的的含义都会在下篇博客写出。

        很期待大家评论反馈内容的不足,大家一起学习进步!!!

 

 

你可能感兴趣的:(C++内容讲解,c++)