Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: writebyte() implementation  (Read 2517 times)

0 Members and 1 Guest are viewing this topic.

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
writebyte() implementation
« on: October 02, 2012, 11:08:24 pm »
I'm attempting to write a: writebyte() function in which the client
specified to write a byte of data to the server, and the server will read this data as a byte

Here's my writebyte():
void writebyte(sf::UdpSocket *socket, unsigned char b){
        if (socket->send(&b, sizeof(b), "127.0.0.1", 4567) != sf::Socket::Done){
                //Error
                cout << "message was not sent" << endl;
        }
}
 

And my readbyte():
unsigned char readbyte(UdpSocket *socket){
        unsigned char byte;
        std::size_t received;
        sf::IpAddress sender;
        unsigned short port;

        int rec = socket->receive(&byte, sizeof(byte), received, sender, port);
        if(rec != sf::Socket::Done){
                //Error
        }
        if(rec == sf::Socket::Disconnected)
                cout << "D/C" << endl;
        return byte;
}
 

I've so far gotten a few functions to work:
writeint()
writefloat() and
writeshort()

The server receives the data correctly.
However for writebyte() and readbyte(), the data is not read correctly.
For example, I've sent the following data using writebyte():
                                unsigned char c = 253;
                                writebyte(&socket, c);
 

Here's what the server writes:


Any idea what's wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: writebyte() implementation
« Reply #1 on: October 02, 2012, 11:14:09 pm »
An [unsigned] char is written in the console as a character. You need to cast it to an int to see its value.
Laurent Gomila - SFML developer

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: writebyte() implementation
« Reply #2 on: October 03, 2012, 12:31:47 am »
Thanks got it!