Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / SendSocketByte Question

Author
Message
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 5th Nov 2017 17:55 Edited at: 5th Nov 2017 17:59
Hello everyone,

I have a special question about the SendSocketByte command.

The description says:
Quote: "Will send a single byte in the range -128 to 127 to the remote connection. Note that data is not sent immediately, you must call FlushSocket to send it. Returns 1 if the byte was successfully written to the buffer, 0 if the socket disconnected. "


So a byte consists of 8 bits.
00000000 <= this is the smallest possible byte - aka "0" decimal unsigned
11111111 <= this is the largest possible byte - aka "255" decimal unsigned

So how should I manage the input value of SendSocketByte?
should I input "-128" to get a 00000000 byte send or should I input "0" to get 00000000 send?

Or better: Does the input value range correlate with the sended byte range ?


[/url]
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 5th Nov 2017 20:42 Edited at: 5th Nov 2017 20:45
I guess that depends on the range you need !?
If you only need 128 positive values then send your variables as they are.
If you need 255 positive values then subtract 127 in the send process and add 127 in the receiving process
And I'm pretty sure that 0 (dec) will send 00000000 (bin) and -127(dec) will send 10000001 (bin)
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 5th Nov 2017 20:47 Edited at: 5th Nov 2017 20:53
Quote: "If you need 255 values then subtract 127 in the send process and add 127 in the receiving process"

I can't. I have to add a single byte with the full value 11111111 (bin) to the message

Quote: "And I'm pretty sure that 0 (dec) will send 00000000 (bin)"

I also think this is the case, based on experiments


Quote: "and -127(dec) will send 10000001 (bin)"

This is bad.

But does also 255 (dec) send 11111111 (bin) ? Or is it limited to 127 (dec)?
I am not quite shure based on the command description.

[/url]
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 5th Nov 2017 23:50
There seems to be a limit at 128. Everything above gets turned around to -128-x
And the use of sendnetworkinteger seems also to send signed values.

Paul,
is there a chance to get an unsigned byte into a socket?

[/url]
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 14th Nov 2017 14:30
The value is two's complement, so the value 11111111 corresponds to the value -1

To convert an unsigned byte into a signed byte with the same bits set use
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 14th Nov 2017 15:11 Edited at: 14th Nov 2017 15:15
Thanks Paul!
I figured it out some hours ago by using "Val(Hex(inbyte),16)" in hex and everything was correct, I was quite surprised.
So I don't have to move to T2 ( I learned WinSock2 the other days in order to send/recieve unsigned char arrays).
Your method is even better as it spares the string.

Thank you for the Socket commands - They are very powerful and superior to everything we had before in dbpro times.
Telnet, SSH and even more are actually possible in AGK.

I do not even miss the UDP commands

[/url]
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 15th Nov 2017 09:54
I use SendSocketByte for my current game. I subtract 128 when I add a byte to the socket and then add 128 when I read the received socket byte, then I will be in the range of 0 to 255.

variable_1 = 255

SendSocketByte(socket_connection_id,variable_1 - 128)

variable_2 = GetSocketByte(socket_connection_id) + 128
13/0
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 15th Nov 2017 20:33 Edited at: 15th Nov 2017 20:34
@Cybermind:
You want subtract 127 like i wrote above not 128 because if you want to send 0 but subtract 128 ...well i assume -128 will get wrapped around so you receive 255
You probably didn't run into a problem because you never send 0 !?
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 16th Nov 2017 08:12 Edited at: 16th Nov 2017 08:14
@janbo,
Quote: "Will send a single byte in the range -128 to 127 to the remote connection."
This is from the documentation, if I add 0 then subtract 128 I will get -128, if I add 255 and subtract 128 I will get 127. I could be wrong but I think that this will make me stay in range.
13/0
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 16th Nov 2017 14:22
You are right, I thought signed bytes range from -127 to 128

Login to post a reply

Server time is: 2024-04-16 09:44:23
Your offset time is: 2024-04-16 09:44:23