Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-06
4
Medium Priority
?
553 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

704 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