I am trying to make a Winsock Chat. I want to send packets which sit between 2 "tags". kind of like "^^^TAG^^^ packet data ^^^TAG^^^"
Problem is, the Client Apps I am using, including my own Client app I wrote either send the message wrong or my Server app is receiving the data wrong
Here is what I mean:
I am aware why it is split, that's what my Tag idea is for, but if you read what I send and what I got you will see there's added and replaced letters. At a certain point I even got the words that I sent followed by "================================" then other random unicode characters, but I could not get it again to screenshot.
Due to the fact most of the TCP clients I got off of the internet didn't work, I assume the problem is with how I receive the packets rather than how I and the other programs are sending them
My code:
heres a rewritten simple version of my code
struct client_info
{
SOCKET sock;
const char* ip;
int port;
};
struct server_info
{
SOCKET sock;
const char* ip;
int port;
std::vector<client_info> clients;
int client_count;
HANDLE connection_handler;
HANDLE recv_handler;
};
struct param_info
{
void* server_info_pointer;
};
class my_server
{
public:
my_server(const char* ip, int port)
{
this->m_info.ip = ip;
this->m_info.port = port;
this->start();
this->client_handler();
this->recv_packet();
}
~my_server(void)
{
}
private:
server_info m_info;
bool start(void)
{
WSADATA lpWsaData = decltype(lpWsaData){};
WSAStartup(MAKEWORD(2, 2), &lpWsaData);
this->m_info.sock = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in lpAddr = decltype(lpAddr){};
lpAddr.sin_family = AF_INET;
lpAddr.sin_addr.S_un.S_addr = inet_addr(this->m_info.ip);
lpAddr.sin_port = htons(this->m_info.port);
char chOption = 1;
setsockopt(this->m_info.sock, SOL_SOCKET, SO_REUSEADDR, &chOption, sizeof(chOption));
setsockopt(this->m_info.sock, IPPROTO_TCP, TCP_NODELAY, &chOption, sizeof(chOption));
if (!bind(this->m_info.sock, reinterpret_cast<sockaddr*>(&lpAddr), sizeof(lpAddr)))
{
return true;
}
closesocket(this->m_info.sock);
WSACleanup();
return false;
}
bool client_handler(void)
{
param_info pi = param_info{};
pi.server_info_pointer = &this->m_info;
if (this->m_info.connection_handler = CreateThread(nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>
(this->client_handler_internal), &pi, 0, nullptr))
{
return true;
}
return false;
}
static void client_handler_internal(void* param)
{
auto pi = reinterpret_cast<param_info*>(param);
if (!listen(reinterpret_cast<server_info*>(pi->server_info_pointer)->sock, SOMAXCONN))
{
client_info ci = client_info{};
sockaddr_in lpAddr;
int dAddrSize = sizeof(lpAddr);
while (ci.sock = accept(reinterpret_cast<server_info*>(pi->server_info_pointer)->sock, reinterpret_cast<sockaddr*>(&lpAddr), &dAddrSize))
{
ci.ip = inet_ntoa(lpAddr.sin_addr);
ci.port = htons(lpAddr.sin_port);
printf("%s:%d joined!\n", ci.ip, ci.port);
reinterpret_cast<server_info*>(pi->server_info_pointer)->clients.push_back(ci);
memset(&ci, 0, sizeof(ci));
Sleep(100);
}
}
return;
}
auto __forceinline recv_packet(void) -> bool
{
param_info pi = param_info{};
pi.server_info_pointer = &this->m_info;
if (this->m_info.recv_handler = CreateThread(nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>
(this->recv_packet_internal), &pi, 0, nullptr))
{
return true;
}
return false;
}
static void recv_packet_internal(void* param)
{
auto pi = reinterpret_cast<param_info*>(param);
for (;;)
{
for (int i = 0; i < reinterpret_cast<server_info*>(pi->server_info_pointer)->clients.size(); ++i)
{
char * lpBuffer = new char[64];
if (0 < recv(reinterpret_cast<server_info*>(pi->server_info_pointer)->clients.at(i).sock, lpBuffer, sizeof(lpBuffer), 0))
{
std::string lpNewBuffer = lpBuffer;
printf("%s\n", lpNewBuffer.c_str());
}
memset(lpBuffer, 0, sizeof(lpBuffer));
}
Sleep(50);
}
return;
}
};
if (0 < recv(reinterpret_cast<server_info*>(pi->server_info_pointer)->clients.at(i).sock, lpBuffer, sizeof(lpBuffer), 0))
You ignore the return value of recv
, so your code has no idea how many bytes it received. Also, see below for why sizeof(lpBuffer)
is wrong here.
memset(lpBuffer, 0, sizeof(lpBuffer));
Since lpBuffer
is a char *
, this zeroes sizeof(char *)
bytes, which is not right. Only use sizeof
when you need the size of a type. Also, why are you zeroing a buffer you already used and will never use again?
std::string lpNewBuffer = lpBuffer;
You should have used the return value from recv
here to know how many bytes lpNewBuffer
should be.
Don't treat things as strings if they're not strings. Store the return value of recv
so you know how many bytes you received.