Number Discrepency

Started by
5 comments, last by Unconquerable 17 years, 9 months ago
Hi. I have a client that connects to a server and attempts to send the number 400 to the server, that goes well.

int Send(SOCKET socket,int _buffer )
{
    
    
    
    send( socket, reinterpret_cast< char * >( &_buffer ), sizeof( _buffer ) , 0 );
    Logger.LogInt(_buffer); // Log to make sure i sent the right number
    if (nret == SOCKET_ERROR) {
    return NETWORK_ERROR;
    }
    
    else{}
}


However, instead of 400, the server is receving 249, i checked this after multiple tests via my log file. Here is the servers recv function:

  int Recv(SOCKET socket)
  {
     char buffer[256];
     int _buffer;


     nret = recv( socket, reinterpret_cast< char * >( &_buffer ), sizeof( _buffer ) , 0 );
     
     _buffer = iShiptwo_x;
     Logger.LogInt(iShiptwo_x); // log what i recieved!
    
     if (nret == SOCKET_ERROR) {
	// Get a specific code
	// Handle accordingly
	return NETWORK_ERROR;
     } else {

     }

}


If any one has an idea on whats going on, that'd be great, thanks.
Advertisement
Why are you sending sizof(_buffer) + 1, but receiving sizeof(_buffer)?
Quote:Original post by SiCrane
Why are you sending sizof(_buffer) + 1, but receiving sizeof(_buffer)?


Changed that and i still have the same problem.
Are your client and server on different operating systems ?
This might be a problem with byte ordering, some OS are big endian others are small endian.

Try using htonl(long x) to convert and integer before sending.
And ntohl(long x) to convert the integer after recieving.
They are on the same OS. And when trying to use htonl, i get :expected primary-expression before "int"
Instead of "_buffer = iShiptwo_x;" maybe you mean "iShiptwo_x = _buffer;"?
Quote:Original post by Anonymous Poster
Instead of "_buffer = iShiptwo_x;" maybe you mean "iShiptwo_x = _buffer;"?


That fixed it, thanks.

This topic is closed to new replies.

Advertisement