Solved

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

Posted on 2003-11-06
4
526 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
Comment Utility
it looks like printf problem - it shows signed data.
0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
try to do so:

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

Accepted Solution

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

Author Comment

by:Larn0s
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now