16
Network / 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():
And my readbyte():
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():
Here's what the server writes:
Any idea what's wrong?
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;
}
}
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;
}
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);
writebyte(&socket, c);
Here's what the server writes:
Any idea what's wrong?