Solved

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

Posted on 2003-11-06
4
545 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

623 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