Network communication simulator using WINSOCK (client side)

Posted by ody on Wed, 17 Jun 2020 05:41:41 +0200

Network communication simulator using WINSOCK

(client side)

Recently, in the process of embedded project, I learned winsock network programming. The main content of project is to simulate the whole process through c language before migrating to stm32. The process includes: a client device and a sensor device as a server are connected through TCP protocol, and the sensor terminal is controlled by inputting different instructions from the client terminal
For example, the start command wakes up the sensor;
After wake-up, input any instruction to obtain the temperature value returned by the sensor (here is random number generation);
After waking up, input quit to exit the connection and end the simulation process.

Here are some instructions:
1. As the client, the code is divided into communication module and control module.
2. Considering that the TCP protocol connects through three handshakes, which helps to ensure the stability of the communication between the sensor and the client, we use TCP communication instead of the recommended UDP communication, so the large-scale code in the code part is the communication module.
3. The control module is to identify the returned data of the sensor (in fact, it is to determine the range of the returned value, because the project is relatively simple, only one level is set here)
4. In this code, the temperature value returned by the sensor is only the value between 35.0-42.9 generated by random number, which is the actual value detected by the temperature sensor in the MCU development board version.

Add: the complete source code of the project has been update d to gitee, and subsequent improvements will be carried out gradually. If you are interested, you can have a look gitee connection

Thank you for completing CSDN of Wang classmate in charge of MCU part of the project Personal space , Zhang and Sima in charge of GUI

Article link of sensor:
This part is the source code of client side

#include "stdio.h"
#pragma comment(lib,"WS2_32.lib")
#define BUF_SIZE 64
//Set header file, library file and constant (buffer size)

int _tmain(int argc, _TCHAR* argv[])
	SOCKET sHost;
	SOCKADDR_IN servAddr;
	char buf[BUF_SIZE];
	int retVal;
	//Declare variables required by the program

	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
		printf("WSAStartup failed! \n");
		return 1;
	//Initializing the Socket environment

	if (INVALID_SOCKET == sHost)
		printf("socket failed! \n");
		return -1;
	//Create Socket on client side

	servAddr.sin_family = AF_INET;
	servAddr.sin_addr.S_un.S_addr = inet_addr("");
	servAddr.sin_port = htons(9990);
	int sServerAddlen = sizeof(servAddr);
	//Set server socket address

	retVal = connect(sHost, (LPSOCKADDR)&servAddr, sizeof(servAddr));
	if (retVal == SOCKET_ERROR)
		printf("connect failed!\n");
		return -1;
	//Connect to server

	while (1)
		printf("Please enter the information passed to the server:");
		std::string str; //Send data to server
		std::getline(std::cin, str);
		ZeroMemory(buf, BUF_SIZE);
		strcpy(buf, str.c_str());//Copy the input data to buf area
		retVal = send(sHost, buf, strlen(buf), 0);
		if (SOCKET_ERROR == retVal)
			printf("send failed! \n");
			return -1;
		}//send function debug
		retVal = recv(sHost, buf, sizeof(buf)+1, 0);
		printf("Recv From Server: %s\n", buf);//Receive data from server
		if ((buf[0] == '3' && buf[1] > '6') || buf[0] == '4')
			printf("WARNING! Abnormal temperature, please check\n ");
		//Temperature detection, if more than 37, alarm
		if (strcmp(buf, "quit") == 0)
			printf("quit! \n");
	//Communication between server and client

	return 0;

Production is not easy, thank you for your support!

Topics: socket network simulator Programming