Link to home
Start Free TrialLog in
Avatar of Larn0s
Larn0s

asked on

Socket recieve buffer problem. Some things have FFFFFF...

I've created a program that sends and receives things using Winsock.
But I've got this problem when receiving.
Although I do get the packet, it's kind of corrupt... So to speak.
When for example, the packet should be something like: 13 00 43
It's actually FFFFFF13 00 43...
If I used a packet sniffer on the program while it's running, it shows that it recives 13 00 43, but in the program itself, it shows it with the FFFFFF.
At first I though it was just with small packets, as slightly larger ones were ok. But when I started using much larger packets, the FFFFFF appeared all over the place, almost appearing randomly.
Something that should be: 45 00 23 56 34 11 06 34 76 27 35 89 23 00 45 22 F4 C2 23 3B
Would be something weird like: FFFFFF45 00 F3 56 FFFFFF34 11 06 FFFFFF34 76 27 35 89 23 00 FFFFFF45 22 F4 C2 23 FFFFFF3B
If I tried something like recvBuffer[0], it would be -123 or some weird number like that...

Below is a sample program that does the above, but from what I can tell, it should work properly... But then again, I haven’t really used sockets that much. Any help is greatly appreciated.

#include <iostream>
#include <winsock.h>
#include <string>

#pragma comment(lib, "wsock32.lib")

#define MAX_PACKET_SIZE 65525

using namespace std;

int main(void) {

      WSAData wsa;
      WSAStartup(MAKEWORD(1, 1), &wsa);
      SOCKET sock;
      sockaddr_in LocalSin, RemoteSin;

      char szIP[] = "255.255.255.255";
      int port = 80;

      LocalSin.sin_family = AF_INET;
      LocalSin.sin_port = 0;
      RemoteSin.sin_family = AF_INET;
      RemoteSin.sin_port = htons(port);

      RemoteSin.sin_addr.S_un.S_addr = inet_addr(szIP);
      sock = socket(AF_INET, SOCK_STREAM, 0);
      bind(sock, (sockaddr *)&LocalSin, sizeof(sockaddr_in));
      connect(sock, (sockaddr *)&RemoteSin, sizeof(sockaddr_in));

      char* szDataString = "blahblah something";

      send(sock, szDataString, strlen(szDataString), 0);

      int i = 0;
      char* recvBuffer = (char *)malloc(MAX_PACKET_SIZE+1);
      int bytesRecv;
      do {
            memset(recvBuffer, 0, MAX_PACKET_SIZE+1);
            bytesRecv = recv(sock, recvBuffer, MAX_PACKET_SIZE, 0);

            if (bytesRecv > 0) {
                  for(int i = 0; i < bytesRecv; i++) {
                        printf("%0.2X ", recvBuffer[i]);
                  }
            }
      } while(bytesRecv > 0);

      closesocket(sock);
      WSACleanup();

      return 0;
}
Avatar of GloomyFriar
GloomyFriar

it looks like printf problem - it shows signed data.
try to do so:

unsigned char* recvBuffer = (char *)malloc(MAX_PACKET_SIZE+1);
ASKER CERTIFIED SOLUTION
Avatar of GloomyFriar
GloomyFriar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Larn0s

ASKER

I couldn't do this:
unsigned char* recvBuffer = (char *)malloc(MAX_PACKET_SIZE+1);
as the recv() method doesn't accept unsigned char*, only char*, just casting it worked though like in the second method:
printf("%0.2X ", (unsigned char)recvBuffer[i]);
So thanks for your help. :)