Server Development - Learning Notes - Cross-Platform (win, ubtu) - Fallacies to Be Modified

Keywords: socket Windows network Ubuntu

Links to the original text: https://mp.csdn.net/postedit/98461694

In the previous article, we realized the communication between the client and the server, which made good use of the structure. In this article, we mainly transplanted the program under windows to Ubuntu and realized the communication.

1. New empty console project, new server.cpp, paste the previous code

#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib") 

enum CMD
{
	CMD_NEW_USER,
	CMD_LOGIN,
	CMD_LOGIN_RST,
	CMD_LOGOUT,
	CMD_LOGOUT_RST,
};

struct DataHeader
{
	short dataLth;
	CMD   cmd;
};
struct NewUsr : public DataHeader
{
	NewUsr()
	{
		
		dataLth = sizeof(NewUsr);
		cmd = CMD_NEW_USER;
	}
};
struct Login : public DataHeader
{
	Login()
	{

		dataLth = sizeof(Login);
		cmd = CMD_LOGIN;
		strcpy(usrName, "Xiaoming");
		strcpy(passWord, "Xiaoming Code");
	}
	char usrName[32];
	char passWord[32];
};

struct LoginRst : public DataHeader
{
	LoginRst()
	{

		dataLth = sizeof(LoginRst);
		cmd = CMD_LOGIN_RST;
		rst = 1;
	}
	int rst;
};


struct Logout : public DataHeader
{
	Logout()
	{

		dataLth = sizeof(Logout);
		cmd = CMD_LOGOUT;
		strcpy(usrName, "Xiaoming");
	}
	char usrName[32];
};

struct LogoutRst : public DataHeader
{
	int rst;
	LogoutRst()
	{

		dataLth = sizeof(LogoutRst);
		cmd = CMD_LOGOUT_RST;
		rst = -1;
	}
};


int main()
{
	WORD ver = MAKEWORD(2, 2);
	WSADATA dat;
	WSAStartup(ver, &dat);
	//1. Setting up sockets
	SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	//2 Binding network ports for receiving client links
	sockaddr_in _sin = {};
	_sin.sin_family = AF_INET;
	_sin.sin_port = htons(4567);//Host to network byte address
	_sin.sin_addr.S_un.S_addr = INADDR_ANY; //Receive
	if (bind(_sock, (sockaddr*)&_sin, sizeof(_sin)) == SOCKET_ERROR)
	{
		printf("Failed to bind network ports!");
	}
	else{
		printf("Successful binding of network ports!");
	}

	//3 listening ports
	if (SOCKET_ERROR == listen(_sock, 5))
	{
		printf("Failed to monitor!");
	}
	else
	{
		printf("Successful monitoring!");
	}
	//4 accept waits for client links
	sockaddr_in _clientAddr = {};
	int nAddrLen = sizeof(sockaddr_in);
	SOCKET _cSock = INVALID_SOCKET;
	
	_cSock = accept(_sock, (sockaddr*)&_clientAddr, &nAddrLen);

	if (_cSock == INVALID_SOCKET)
	{
		printf("Currently an invalid client socket!!!\n");
	}
	
	//printf("Receive new client join: IP =% s!!!  n", inet_ntoa (_clientAddr. sin_addr));

	while (true)
	{
		//Receiving header
		DataHeader recvDH;
		int nLen = recv(_cSock, (char*)&recvDH, sizeof(recvDH), 0);
		if (nLen <= 0)
		{
			printf("Client has exited!!!\n");
			break;
		}
       //Processing requests
		switch(recvDH.cmd)
		{
			case CMD_NEW_USER:
			{
				printf("command CMD_NEW_USER,Add a new client!!!\n");
			}
			break;
			case CMD_LOGIN:
			{
				printf("command CMD_LOGIN,Client!!Sign in!\n");
				Login login;
				int ret = recv(_cSock, (char*)(&login + sizeof(DataHeader)), sizeof(Login)-sizeof(DataHeader),0);
				printf("User Name:%s User password:%s\n", login.usrName, login.passWord);
			}
			break;
			case CMD_LOGOUT:
			{
				printf("command CMD_LOGOUT,Client logout!!!\n");
				Logout logout;
				int ret = recv(_cSock, (char*)(&logout + sizeof(DataHeader)), sizeof(Logout) - sizeof(DataHeader), 0);
				printf("User Name:%s", logout.usrName);
			}
			break;
			case CMD_LOGIN_RST:
			{
				printf("command CMD_LOGIN_RST,Client login results!!!\n");
				LoginRst loginrst;
				int ret = recv(_cSock, (char*)(&loginrst + sizeof(DataHeader)), sizeof(loginrst) - sizeof(DataHeader), 0);
				printf("Login results:%d\n", loginrst.rst);
			}
			break;
			case CMD_LOGOUT_RST:
			{
				printf("command CMD_LOGOUT_RST,Client logout results!!!\n");
				LoginRst logoutrst;
				int ret = recv(_cSock, (char*)(&logoutrst + sizeof(DataHeader)), sizeof(logoutrst) - sizeof(DataHeader), 0);
				printf("Log out the results:%d\n", logoutrst.rst);
			}
			break;
		}
	}

	//6 Close sockets
	closesocket(_sock);
	WSACleanup();
	getchar();
	return 0;
}

2. Create a new client.cpp under Ubuntu. After pasting the code below, compile it with the command g++ client.cpp-o client and run it with the command. / client.

Attention should be paid to modifying IP address by following methods: cmd |ipconfig

Note that this ipv4 address.

#ifdef _WIN32
    #define WIN32_LEAN_AND_MEAN
    #define _WINSOCK_DEPRECATED_NO_WARNINGS
    #define _CRT_SECURE_NO_WARNINGS
    #include <Windows.h>
    #include <WinSock2.h>
#else
    #include <unistd.h>//uni std
    #include <arpa/inet.h>
    #include <string.h>
    #define SOCKET int
    #define INVALID_SOCKET  (SOCKET)(~0)
    #define SOCKET_ERROR            (-1)
#endif
#include <stdio.h> 



enum CMD
{
	CMD_NEW_USER,
	CMD_LOGIN,
	CMD_LOGIN_RST,
	CMD_LOGOUT,
	CMD_LOGOUT_RST,
};

struct DataHeader
{
	short dataLth;
	CMD   cmd;
};
struct NewUsr : public DataHeader
{
	NewUsr()
	{

		dataLth = sizeof(NewUsr);
		cmd = CMD_NEW_USER;
	}
};
struct Login : public DataHeader
{
	Login()
	{

		dataLth = sizeof(Login);
		cmd = CMD_LOGIN;
		strcpy(usrName, "Xiaoming");
		strcpy(passWord, "Xiaoming Code");
	}
	char usrName[32];
	char passWord[32];
};

struct LoginRst : public DataHeader
{
	LoginRst()
	{

		dataLth = sizeof(LoginRst);
		cmd = CMD_LOGIN_RST;
		rst = 1;
	}
	int rst;
};


struct Logout : public DataHeader
{
	Logout()
	{

		dataLth = sizeof(Logout);
		cmd = CMD_LOGOUT;
		strcpy(usrName, "Xiaoming");
	}
	char usrName[32];
};

struct LogoutRst : public DataHeader
{
	int rst;
	LogoutRst()
	{

		dataLth = sizeof(LogoutRst);
		cmd = CMD_LOGOUT_RST;
		rst = 1;
	}
};


int main()
{
#ifdef _WIN32
	WORD ver = MAKEWORD(2, 2);
	WSADATA dat;
	WSAStartup(ver, &dat);
#endif
	//1. Setting up sockets
	SOCKET _sock = socket(AF_INET, SOCK_STREAM, 0);
	if (INVALID_SOCKET == _sock)
	{
		printf("Client failed to create socket!!!\n");
	}
	else
	{
		printf("Client creates socket successfully!!!\n");
	}
	//2 Link Server
	sockaddr_in _sin = {};
	_sin.sin_family = AF_INET;
	_sin.sin_port = htons(4567);
#ifdef _WIN32
	_sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
#else
	_sin.sin_addr.s_addr = inet_addr("192.168.27.1");
#endif
	int ret = connect(_sock,(sockaddr*)&_sin,sizeof(sockaddr_in));
	if (SOCKET_ERROR == ret)
	{
		printf("Server connection failed!!!\n");
	}
	else
	{
		printf("Successful server connection!!!\n");
	}

	while (true)
	{
		char scanfBuf[256] = {};
		scanf("%s", scanfBuf);
		if (0 == strcmp(scanfBuf, "exit"))
		{
			printf("Exit client!!!\n");
			break;
		}
		else if (0 == strcmp(scanfBuf, "login"))
		{
			Login login;
			send(_sock, (char*)&login, sizeof(login), 0);
		}
		else if (0 == strcmp(scanfBuf, "logout"))
		{
			Logout logout;
			send(_sock, (char*)&logout, sizeof(logout), 0);
		}
		else if (0 == strcmp(scanfBuf, "loginrst"))
		{
			LoginRst loginrst;
			send(_sock, (char*)&loginrst, sizeof(loginrst), 0);
		}
		else if (0 == strcmp(scanfBuf, "logoutrst"))
		{
			LogoutRst logoutrst;
			send(_sock, (char*)&logoutrst, sizeof(logoutrst), 0);
		}
		else
		{
			printf("Input request is illegal!!!\n");
		}

	}
	
	//7 Close sockets
#ifdef _WIN32
	closesocket(_sock);
	//Clear Windows socket environment
	WSACleanup();
#else
	close(_sock);
#endif
	getchar();
	return 0;
}

3. Stick up the final rendering.

Posted by Chris 96 WS6 on Wed, 09 Oct 2019 13:18:50 -0700