源码如下:
CommonHeader.h
#ifndef TESTAPP_COMMONHEADER_H #define TESTAPP_COMMONHEADER_H #include <array> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <iostream> #include <algorithm> #include <numeric> #include <climits> #include <cstdarg> using namespace std; static constexpr int INVALID = -1; static constexpr int RECV_BUFFER_SIZE = 10240; static const int RET_FAILURE = -1; static const int RET_SUCCESS = 0; static const int BUFF_SIZE_1024 = 1024; static void Print(const char *str, ...) { char buff[BUFF_SIZE_1024] = {0}; va_list values; va_start(values, str); vsnprintf(buff, BUFF_SIZE_1024 - 1, str, values); va_end(values); cout << buff << endl; } #endif
Client.h
#ifndef TESTAPP_CLIENT_H #define TESTAPP_CLIENT_H #include "CommonHeader.h" class Client { public: Client(string peerIp, int peerPort); ~Client(); void ResetPeer(string peerIp, int peerPort); int Connect(); int ReConnect(); int Send(const string &msg); int Recv(); private: int m_socket = INVALID; string m_peerIp; int m_peerPort { INVALID }; char m_recvBuffer[RECV_BUFFER_SIZE] {}; }; #endif //TESTAPP_CLIENT_H
Client.cpp
#include <cstdio> #include <cstring> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include "Client.h" Client::Client(string peerIp, int peerPort) : m_peerIp(peerIp), m_peerPort(peerPort) { } Client::~Client() { if (m_socket != INVALID) { close(m_socket); } } void Client::ResetPeer(string peerIp, int peerPort) { m_peerIp = peerIp; m_peerPort = peerPort; } int Client::Connect() { if (m_socket != INVALID) { close(m_socket); } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(m_peerPort); addr.sin_addr.s_addr = inet_addr(m_peerIp.c_str()); m_socket = socket(AF_INET,SOCK_STREAM,0); if (connect(m_socket, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) != 0) { perror("connect"); return RET_FAILURE; } return RET_SUCCESS; } int Client::Send(const string &msg) { if (m_socket == INVALID) { return RET_FAILURE; } int ret = send(m_socket, msg.c_str(), msg.length(), 0); if (ret == RET_FAILURE) { perror("send"); return RET_FAILURE; } Print("Send %d bytes to peer [%s:%d]", ret, m_peerIp.c_str(), m_peerPort); return RET_SUCCESS; } int Client::Recv() { if (m_socket == INVALID) { return RET_FAILURE; } memset(m_recvBuffer, 0, sizeof(m_recvBuffer)); int ret = recv(m_socket, m_recvBuffer, sizeof(m_recvBuffer), 0); if (ret < 0) { perror("recv"); return RET_FAILURE; } Print("Recv %d bytes [%s] from peer [%s:%d]", ret, m_recvBuffer, m_peerIp.c_str(), m_peerPort); return RET_SUCCESS; }
main.cpp
#include <iostream> #include <fstream> #include <unordered_map> #include <vector> #include <csignal> #include "Client.h" using namespace std; unordered_map<int, char> g_asciiMap = {}; string packet = "474554202f20485454502f312e310d0a486f73743a203130332e38352e38342e3134313a38390d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a43616368652d436f6e74726f6c3a206d61782d6167653d300d0a557067726164652d496e7365637572652d52657175657374733a20310d0a557365722d4167656e743a204d6f7a696c6c612f352e30202857696e646f7773204e542031302e303b2057696e36343b2078363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f3132302e302e302e30205361666172692f3533372e3336204564672f3132302e302e302e300d0a4163636570743a20746578742f68746d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c6170706c69636174696f6e2f786d6c3b713d302e392c696d6167652f776562702c696d6167652f61706e672c2a2f2a3b713d302e382c6170706c69636174696f6e2f7369676e65642d65786368616e67653b763d62333b713d302e370d0a4163636570742d456e636f64696e673a20677a69702c206465666c6174650d0a4163636570742d4c616e67756167653a207a682d434e2c7a683b713d302e392c656e3b713d302e382c656e2d47423b713d302e372c656e2d55533b713d302e360d0a436f6f6b69653a204a53455353494f4e49443d44453335413542373541323338444238344543333630464533413233304242380d0a49662d4d6f6469666965642d53696e63653a205361742c2032362041756720323032332030323a34323a313020474d540d0a0d0a"; int GetDecimalVal(char c) { if (c >= '0' && c <= '9') { return c - '0'; } if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } Print("Get decimal val [%s] failed", c); return RET_FAILURE; } int TransalatePacket(string packet, string &output) { if (packet.length() % 2 != 0) { Print("Packet length [%lu] invalid", packet.length()); return RET_FAILURE; } for (int i = 0; i < packet.length(); i += 2) { int high = GetDecimalVal(packet[i]); if (high == RET_FAILURE) { return RET_FAILURE; } int low = GetDecimalVal(packet[i + 1]); if (low == RET_FAILURE) { return RET_FAILURE; } auto it = g_asciiMap.find(high * 16 + low); if (it == g_asciiMap.end()) { Print("[%d] not exist in asiic map", it->first); return RET_FAILURE; } output += it->second; } return RET_SUCCESS; } int main() { char space = ' '; for (int i = 0; i < 127; ++i) { g_asciiMap[i] = space + (i - 32); } string msg; TransalatePacket(packet, msg); Client client("192.168.18.129", 38000); if (client.Connect() == RET_FAILURE) { return RET_FAILURE; } client.Send(msg); client.Recv(); sleep(10); return RET_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.22) project(TestApp) set(CMAKE_CXX_STANDARD 17) aux_source_directory(. SOURCES) include_directories(./) add_executable(TestApp ${SOURCES})
标签:码流,peerIp,return,int,RET,Client,include,tcpdump,wireshark From: https://www.cnblogs.com/pond-flower/p/17944201