Hello everyone,
I am dealing with this problem that I receive a buffer from a client, and when I try to output what the client sends to my program, it only shows everything till the first zero character.
#include <cstdio>
#include <winsock2.h>
#include <iostream>
#include <string>
using namespace std;
struct incSockets
{
SOCKET sv;
SOCKET cl;
};
void filter(char* packet)
{
while(*packet != 0)
{
cout << "(" << int(*packet) << ")";
packet++;
}
cout << endl;
}
int main(int argc, char** argv) {
struct incSockets incs;
const int iReqWinsockVer = 2;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
{
if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
{
incs.cl = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
size_t bytesReceived;
if (incs.cl==INVALID_SOCKET)
{
//error
}
sockaddr_in clSockAddr;
memset(&clSockAddr, 0, sizeof(clSockAddr));
clSockAddr.sin_family = AF_INET;
clSockAddr.sin_port = htons(1339);
clSockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(incs.cl, (sockaddr*)(&clSockAddr), sizeof(clSockAddr))!=0)
{
//error
}
char buffer[1024];
int fromlen = 10240;
while(true) {
bytesReceived = recvfrom(incs.cl, buffer, 1024, 0, (struct sockaddr *)&clSockAddr, &fromlen);
if(bytesReceived < 0) {
//error
}
//When I do this, it shows me way more.
//cout << int(buffer[0]) << int(buffer[1]) << int(buffer[2]) << int(buffer[3]) << int(buffer[4]) << int(buffer[5]) << int(buffer[6]) << int(buffer[7]) << endl;
//Now it only shows me the first 5 characters. The sixth is a 0.
filter(buffer);
}
closesocket(incs.cl);
}
else
{
// Required version not available
}
// Cleanup winsock
if (WSACleanup()!=0)
{
// cleanup failed
}
}
else
{
// startup failed
}
return 0;
}
I hope someone can help me solve this problem.
Thanks in advance.