#include "pch.h" #include "SokcetClient.h" #include <iostream> #include <thread> #include <Ws2tcpip.h> #include "StringHelper.h" #include "HeartBeatResponse.h" #include "x2struct/x2struct.hpp" #include <string.h> #include <stdio.h> SokcetClient::SokcetClient() { m_nLocalSocket = -1; WSADATA wsaData; if(WSAStartup (MAKEWORD ( 2 , 2 !), & WSADATA) = 0 ) STD :: COUT << " the Socket version Load Failure " << STD :: endl; } SokcetClient :: ~ SokcetClient () { closesocket (); } void :: closesocket SokcetClient () { IF (m_nLocalSocket = -! . 1 ) the closesocket (m_nLocalSocket); // Close the socket connection m_nLocalSocket = - . 1 ; WSACleanup (); // terminate the use of ws2_32.lib } //Create a Socket BOOL SokcetClient the createSocket :: () { IF (m_nLocalSocket == - . 1 ) { / * int iMode =. 1; WSADATA WSD; // initialize Socket environment if (WSAStartup (MAKEWORD (2, 2), & wsd) =! 0) { outputMessage ( "WSAStartup failed \ n-!"); } * / m_nLocalSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP go); IF ! (m_nLocalSocket = INVALID_SOCKET is) { outputMessage (The StringHelper :: GBKToUTF8 ( " Service server socket is created successfully ") .c_str ()); } / * // call the ioctlsocket () to set it to non-blocking mode int = retVal the ioctlsocket (m_nLocalSocket, the FIONBIO, (u_long FAR *) & iMode); IF (retVal of SOCKET_ERROR is ==) { outputMessage ( "the ioctlsocket failed!"); WSACleanup (); } * / } return to false ; } BOOL SokcetClient :: myconnect ( const char * IP, const unsigned Short Prot) { int nRet = of SOCKET_ERROR is; IF ! (m_nLocalSocket = - . 1 ) { The sockaddr_in m_nServeraddr; Memset ( & m_nServeraddr, 0 , the sizeof (m_nServeraddr)); m_nServeraddr.sin_family = AF_INET; m_nServeraddr.sin_port = the htons (Prot); m_nServeraddr.sin_addr.s_addr = the inet_addr (IP); nRet = Connect (m_nLocalSocket, (the sockaddr *) & m_nServeraddr, sizeof (m_nServeraddr)); // return 0 if successful. Otherwise of SOCKET_ERROR is IF (nRet == of SOCKET_ERROR is) { outputMessage ( " server connection failed! " ); return false; } outputMessage("服务器连接成功!"); //std::string data ="{\"bizCode\":\"B1001\",\"parkingNo\":\"1000000184\",\"clientNo\":\"1\",\"reqNo\":\"201909291613278736\",\"clientName\":\"大门岗亭\",\"sign\":\"57DCE7C04A3EF22BF2305281A98A57B2\"}\n\0"; //std::string strTemp = StringHelper::GBKToUTF8(data); //outputMessage(data.c_str()); //Mysend(strTemp.c_str()); //Myrecv(); return true; } return false; } void SokcetClient::Myrecv() { if (m_nLocalSocket != -1) { int rs = -1; int resultRecv = -1; fd_set rfds; while (true) { int size = sizeof(m_message); memset(m_message, '\0', size); FD_ZERO(&rfds); FD_SET(m_nLocalSocket, &rfds); struct timeval tv; tv.tv_sec = 2; tv.tv_usec = 0; rs = select(m_nLocalSocket, &rfds, NULL, NULL, 0); if (rs > 0) { resultRecv = recv(m_nLocalSocket, m_message, size, 0); if (resultRecv > 0) { HeartBeatResponse heartBeatResponse; x2struct::X::loadjson(m_message, heartBeatResponse, false); string json =:: :: X-x2struct (heartBeatResponse) toJSON; // output message outputMessage (json.c_str ()); Memset (m_message, ' \ 0 ' , size); } the else { // these types of error codes, that is connected to normal, continues to receive IF ((resultRecv < 0 ) && (errno == EAGAIN || || EWOULDBLOCK in errno errno == == EINTR)) { continue ; // continue to receive data } // outputMessage ( "server connection is broken ! "); // BREAK; // out of receiving cycle } } } /* #define MAXBUF 1024 int len; char buffer[MAXBUF]; char heartbeat[2048] = "{\"bizCode\":\"B1001\",\"parkingNo\":\"1000000184\",\"clientNo\":\"1\",\"reqNo\":\"201909291613278736\",\"clientName\":\"大门岗亭\",\"sign\":\"57DCE7C04A3EF22BF2305281A98A57B2\"}\n"; fd_set rfds; struct timeval tv; int retval, maxfd = -1; while (1) { FD_ZERO(&rfds); FD_SET(0, &rfds); maxfd = 0; FD_SET(m_nLocalSocket, &rfds); if (m_nLocalSocket > maxfd) maxfd = m_nLocalSocket; tv.tv_sec = 2; tv.tv_usec = 0; retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); if (retval == -1) { printf("Will exit and the select is error! %s", strerror(errno)); break; } else if (retval == 0) { //printf("No message comes, no buttons, continue to wait ...\n"); len = send(m_nLocalSocket, heartbeat, strlen(heartbeat), 0); if (len < 0) { printf("Message '%s' failed to send ! \ The error code is %d, error message '%s'\n", heartbeat, errno, strerror(errno)); break; } else { printf("News: %s \t send, sent a total of %d bytes!\n", heartbeat, len); } continue; } else { if (FD_ISSET(m_nLocalSocket, &rfds)) { memset(buffer, 0,MAXBUF + 1); len = recv(m_nLocalSocket, buffer, MAXBUF, 0); if (len > 0) { printf("Successfully received the message: '%s',%d bytes of data\n", buffer, len); } else { if (len < 0) printf("Failed to receive the message! \ The error code is %d, error message is '%s'\n", errno, strerror(errno)); else printf("Chat to terminate!\n"); break; } } if (FD_ISSET(0, &rfds)) { memset(buffer,0, MAXBUF + 1); fgets(buffer, MAXBUF, stdin); if (!strncmp(buffer, "quit", 4)) { printf("Own request to terminate the chat!\n"); break; } len = send(m_nLocalSocket, buffer, strlen(buffer) - 1, 0); if (len < 0) { printf("Message '%s' failed to send ! \ The error code is %d, error message '%s'\n", Buffer, errno, the strerror (errno)); break; } the else { the printf ( "News:!% S \ T Send, Sent A Total of% D bytes \ n-", Buffer, len); } } } } * / } the else { outputMessage ( " not currently connected to the server! " ); } } void SokcetClient :: Mysend ( const char * Buffer) { IF (m_nLocalSocket = -! . 1 ) { int size = strlen (Buffer); Send (m_nLocalSocket, Buffer, size, 0 ); } the else { outputMessage ( " not currently connected to the server " ); } } void SokcetClient :: outputMessage ( const char * outStr) { STD :: COUT << outStr < < STD :: endl; }
#pragma once #ifndef _SOCKETCLIENT_H_ #define _SOCKETCLIENT_H_ #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll class SokcetClient { public: SokcetClient(); ~SokcetClient(); bool createSocket(); void closeSocket(); bool Myconnect(const char* ip, const unsigned short prot); void Mysend(const char* buffer); void Myrecv(); void outputMessage(const char* outstr); private: SOCKET m_nLocalSocket; char m_message[1024]; }; #endif _SOCKETCLIENT_H_