I've been experimenting with tcp clients in c++ and I really wanted to get some feedback on my code since I know its bad. I would really appreciate some feedback on the class structure I have and the ways I can improve it. I also would love some feedback on any other simple bugs I have (or things in general I can fix).
//FOR CODE REVIEW PLEASE GIVE FEEDBACK
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#undef UNICODE
#pragma comment (lib, "Ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#define port 0001;
std::vector<std::string> split(std::string mystring, std::string delimiter)
{
std::vector<std::string> subStringList;
std::string token;
while (true)
{
size_t findfirst = mystring.find_first_of(delimiter);
if (findfirst == std::string::npos) //find_first_of returns npos if it couldn't find the delimiter anymore
{
subStringList.push_back(mystring); //push back the final piece of mystring
return subStringList;
}
token = mystring.substr(0, mystring.find_first_of(delimiter));
mystring = mystring.substr(mystring.find_first_of(delimiter) + 1);
subStringList.push_back(token);
}
return subStringList;
}
class client {
WSADATA wsaData;
//SOCKET cSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
char recvBuf[1024];
PCWSTR ip = L"192.168.86.36";
public:
void startWinsock() {
std::cout << "[+] - Starting our client.\n";
int rCode = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rCode != NO_ERROR) {
std::cout << "[-] - Error while trying to start winsock.\n";
exit(0);
}
}
/*
void prepSocket() {
SOCKET cSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (cSocket == INVALID_SOCKET) {
std::cout << "[+] - Error: " << WSAGetLastError() << std::endl;
WSACleanup();
exit(1);
}
}
*/
void connectClient(SOCKET cSocket) {
sockaddr_in addrServer;
addrServer.sin_family = AF_INET;
InetPton(AF_INET, ip, &addrServer.sin_addr.s_addr);
addrServer.sin_port = htons(0001);
memset(&(addrServer.sin_zero), '\0', 8);
std::cout << "[+] - Connecting to our socket.\n";
int iResult = connect(cSocket, (SOCKADDR*)&addrServer, sizeof(addrServer));
if (iResult == SOCKET_ERROR) {
closesocket(cSocket);
std::cout << "[-] - Connection Failed: " << WSAGetLastError() << std::endl;
WSACleanup();
}
}
void sendMessage(SOCKET cSocket,char messageContent[1024]) {
//char demoMessage[1024] = "[+] - Demo message\n";
int message = send(cSocket, messageContent, strlen(messageContent), 0);
if (message == SOCKET_ERROR) {
std::cout << "[-] - Unable to send demo message: " << WSAGetLastError() << std::endl;
closesocket(cSocket);
WSACleanup();
}
std::cout << "[+] - Successfully sent our demo message.\n";
}
void recieveMessage(SOCKET cSocket) {
std::cout << "[+] - Awaiting server response" << std::endl;
int iResult = recv(cSocket, recvBuf, 1024, 0);
if (iResult > 0) {
std::string content;
for (int i = 0; i < iResult; i++) {
content += recvBuf[i];
}
if (strstr(content.c_str(), "cmd")) {
std::string command = split(content, (std::string)"cmd ")[1];
std::cout << "COMMAND SHIT NOT IMPLEMENTED\n";
}
else if (strstr(content.c_str(), "exit")) {
exit(0);
}
std::cout << "> " << content;
}
else if (iResult == 0) {
std::cout << "[-] - Connection closed\n" << std::endl;
}
else {
std::cout << "[-] - An error occured: " << WSAGetLastError() << std::endl;
}
}
void killSocket(SOCKET cSocket) {
closesocket(cSocket);
WSACleanup();
}
};
int main() {
client client;
client.startWinsock();
//client.prepSocket();
SOCKET cSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (cSocket == INVALID_SOCKET) {
std::cout << "[+] - Error: " << WSAGetLastError() << std::endl;
WSACleanup();
exit(1);
}
client.connectClient(cSocket);
client.sendMessage(cSocket,(char *)"works kek :happy_pepe:\n");
client.recieveMessage(cSocket);
client.killSocket(cSocket);
}
If you would rather have a pastebin link heres one: https://pastebin.com/E7AvddwP