Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2003-11-06
4
532 Views
Last Modified: 2008-01-16
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;
}
0
Comment
Question by:Larn0s
  • 3
4 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9693716
it looks like printf problem - it shows signed data.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9693722
try to do so:

unsigned char* recvBuffer = (char *)malloc(MAX_PACKET_SIZE+1);
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 50 total points
ID: 9693743
another way:
printf("%0.2X ", (unsigned char)recvBuffer[i]);
0
 

Author Comment

by:Larn0s
ID: 9698429
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. :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question