login
March 29, 2024, 02:44:06 pm

Author Topic: ASCII in sockets  (Read 5253 times)

0 Members and 1 Guest are viewing this topic.

Skirmant

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 681
  • Country: il
  • Ignorance is bliss.
    • View Profile
ASCII in sockets
« on: May 09, 2012, 06:42:19 am »
Not sure what board I should post this in. But I guess it's more of a showroom topic than support.
This idea was floating in my head for a while since I began using sockets a few years ago. And I'd like to hear your thoughts on this.

Probably the first thing you notice when you decide to learn how to send shit through the internet is that it MUST be in the form of a string. Many people find this highly frustrating because the majority of server's data (including VCMP) consists of numbers. Which means you're forced to find creative ways import them into strings.

But this is not what I'm addressing. The second thing you realize is that when you send data, every single integer in your messages must be turned into a byte. Which is completely different when you move the numerical data trough a computer. Because it allows you to store far larger numbers than 0-9 to a single byte.

Now what's a byte? A byte is a memory unit that consists of 8 bits. Each bit can have two different states - true & false.
Which means a byte can have 256 different combinations. 256 different symbols you can store in a single byte.
Symbols that form the ASCII standard:



That means if you assign a predetermined numerical value for each symbol you can hold 256  different numerical values (Probably 0-255).
By now you probably got the point.

How do you use this method?

Let's say we a have a message that a server wants to send to it's client.
And it was "169" - This message would take 3 bytes of data. Now if you made an algorithm for converting it...
It could be turned to "☺" and wolah! You just saved 3x of space!

But what about values that go beyond 255? Like 256.

No problem. In that case we just convert "25" and leave "6" be. At the end of the day we are guaranteed to save at least 2x of bandwidth.

What about more complex stuff?

Let's say that the message contains "15499145" it could be converted to: "SAY" Got it?
I converted 154 to 'S', 99 to 'A' and 145 to 'Y'. So we turned a 8 byte message to 3 bytes.

What about text?

This method can be used on text too! VCMP let's you chat with less than a 100 difirent symbols.
Which means you can shorten your text strings at least 2x.

But won't this make my server need more computer power?
 
If you actually got to the point when you want to ask this, I have only one response: Go kill yourself.
Seriously? These aren't the late 80's and early 90's. I'm sure your server/client can handle constant string manipulation.

To finish the post I'd like to say that this seems like a very useful method for people that want to cut bandwidth for their applications.
It's flexible and it can be used for many different approaches and combinations.
« Last Edit: May 09, 2012, 07:08:06 am by Skirmant »
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

"C++ is a horrible language" - Linus Torvalds, creator of Linux

Quote
<SLC> nope. changed my mind. squirrel still sux dix
<SLC> it's just a piece of sh!t
* SLC has quit (Connection closed)

Thijn

  • Forum Administrator
  • Crazy Man
  • *
  • *
  • *
  • Posts: 2946
  • Country: nl
    • View Profile
Re: ASCII in sockets
« Reply #1 on: May 09, 2012, 08:41:02 am »
This same method is used on almost every query protocol.
It sends an ascii value that says the length of the string that follows. And it also sends the player count and max players in ascii values.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

I'm not totally useless,
I can be used as an bad example ;)

"Never do Today, What you can do Tomorrow"


Skirmant

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 681
  • Country: il
  • Ignorance is bliss.
    • View Profile
Re: ASCII in sockets
« Reply #2 on: May 09, 2012, 09:12:03 am »
This same method is used on almost every query protocol.
It sends an ascii value that says the length of the string that follows. And it also sends the player count and max players in ascii values.

To be honest. Before you responded I had no idea there were things like query protocols. I guess you learn something new everyday :laugh: +1

The post doesn't loose it's purpose as it still gives insight to someone that wants to build his own protocols for his own specific needs.
And building your own shit is far better than using some third party code.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

"C++ is a horrible language" - Linus Torvalds, creator of Linux

Quote
<SLC> nope. changed my mind. squirrel still sux dix
<SLC> it's just a piece of sh!t
* SLC has quit (Connection closed)

aXXo

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • *
  • *
  • *
  • Posts: 2722
  • Country: in
    • View Profile
Re: ASCII in sockets
« Reply #3 on: May 09, 2012, 09:19:00 am »
I converted 154 to 'S', 99 to 'A' and 145 to 'Y'. So we turned a 8 byte message to 3 bytes.
How did you convert 154 to an 'S'?
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor


Skirmant

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 681
  • Country: il
  • Ignorance is bliss.
    • View Profile
Re: ASCII in sockets
« Reply #4 on: May 09, 2012, 09:35:56 am »
I converted 154 to 'S', 99 to 'A' and 145 to 'Y'. So we turned a 8 byte message to 3 bytes.
How did you convert 154 to an 'S'?

It doesn't have to follow the same ID's of the ASCII table. The point is that you can treat each symbol as a numerical combo of your choice.
The function that translates the data could recognize "S" as 456789 if you wanted it to. It depends on what kind of algorithm you make. Get it?

« Last Edit: May 09, 2012, 09:38:34 am by Skirmant »
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

"C++ is a horrible language" - Linus Torvalds, creator of Linux

Quote
<SLC> nope. changed my mind. squirrel still sux dix
<SLC> it's just a piece of sh!t
* SLC has quit (Connection closed)

stormeus

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 1755
  • Country: us
  • VC:MP Developer
    • View Profile
    • GTA VICE CITY Respective owner
Re: ASCII in sockets
« Reply #5 on: May 09, 2012, 08:59:22 pm »
Nothing new there, but should help people who might want/need something like this. Something similarly related:

Code: [Select]
function onScriptLoad()
{
local string = "\x1074\x0043";
local seg1   = string.slice( 0, 1 );
local seg2   = string.slice( 1, 2 );

print( seg1 + " / " + seg2 );
if( seg1 == "\x1074" )
print( "TEST1 PASS" );

if( seg2 == "\x0043" )
print( "TEST2 PASS" );

print( "\x0117" );
print( string.len().tostring() );
}

Each \x#### segment is one byte. Quick and dirty, though.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

<krystianoo> stormeus do good job
<krystianoo> with recent update
<krystianoo> if not agree; jeb yourself in head
<Avenger> yesterday you said death to stormeus
<karan> double standard krystianoo
<karan> he called him fake prophet too
<krystianoo> sure fake prophet
<krystianoo> but with recent updates real