Topic:   [RB] Networking for Beginners   (Read 37365 times)


0 Members and 1 Guest are viewing this topic.

Telstar5


  • GMG-er

  • **


  • Posts: 371

  • The sun is up, the sky is blue...
[RB] Networking for Beginners
« on: May 30, 2009, 07:59:27 PM »
Here's my contribution to the Code Exchange. For this, I'm going to tell you how you can make RealBasic do fun stuff with networking.

I'm hoping for this to be the most extensive and easiest-to-understand networking tutorial out there, so enjoy reading it.

Introduction notes
I know what you're thinking; "I haven't used RealBasic since I was 12. RealBasic guys aren't real programmers, dude!".

This goes against the whole idea of technology. Technology is supposed to be an ever moving high speed train; it only goes forward. You could be sitting there writing the exact same program we're going to make in C and thinking you're cool because it's what software engineers use, but RealBasic (and most OOP people) can make applications in a quarter of the time it takes traditional interprelites (my name for those who like to write code in one continuous scroll) to get the very foundations of their programs fully set.

RealBasic is also good for compiling cross platform. For example, we could write a text editor in C, compile it for Mac, and then you'd probably have to restructure the same application to make it work on Windows. RealBasic handles that for you, so you can spend more time working on the actual application and adding features and capabilities rather than buggering about with funny system calls.

Java is the exception, but unless you don't want your code to be easily viewed by absolutely everyone in the world, I would advise keeping your good ideas in a language that you know is difficult to reverse engineer or crack into.

This is just an introductory note for those who are wondering why the hell I'm making a post for RealBasic, as I know there are a lot of interprelites out there who will make any justification to call RealBasic a piece of crap.

You will need...
A pen and a large amount of paper. Make sure you are alone in an empty, comfortable environment. Preferably, you'd have a dry-wipe board, a shirt, a tie and, of course, multi-coloured pens.

You'll also need some Red Bull, Pepsi, coffee - take your pick. Snacks are fine as well, just don't choose chocolate or anything overly salty. Cigarettes are also a recommendation. I shouldn't advocate smoking and I'm sure it must be fun being smoke-free, but I swear to god, if you want to survive network programming without self harming, you're going to require twenty Lucky Strikes and a Zippo.

Rootle through your CD collection and find some good tunes. Something you can just slap on as a backing track for thinking; I personally recommend the B-52s or Lemon Jelly.

Put on your CD of choice, open up that tin of soda and prepare yourself for the most uncomfortable day of your life.

Finally, open up your probably pirated copy of RealBasic, and we can REALLY begin.

So, what now?

Well, we're going to make an instant messaging program. Sounds hard; but thankfully, RealBasic makes it easy.

Now, let's just talk about sockets. No, not the ones in your head; SOCKETS. Those lovely little things that make the internet possible and the thing that makes the sun rise in the morning and set in the evening.

There are two kinds of protocols we care about; TCP and UDP.

TCP is what REALLY makes the sun rise in the morning. It's reliable, it powers practically every computer's networking capabilities, and it's an absolute asshole to work with. It's a connection orientated protocol, so we have to connect to the other side before we can send any data. It requires more preparation than UDP, but you get reliability and stability that data is received securely.

UDP stands for User Datagram Protocol. It's a connectionless protocol which requires next to no preparation; you just enter in an IP, and the data gets sent. It doesn't do any confirmation nor any reliability tricks, which is useful for sending little bits of data; however, this notoriety has earned UDP the nickname Unreliable Datagram Protocol. Over a local network, it's fine. Internet? I wouldn't.

So, what will we use? We'll use the easiest object to make our IM program work; the AutoDiscovery socket. Open up Window1, and give it the title 'Chatroom', or whatever you want. Drag in a base object and rename the Super as "AutoDiscovery" without quotes. You'll want to rename it to "ChatroomConnector" so we can keep track of what the object's name is.

Next, choose a port to send to. In the properties of this new control, make sure 'SendToSelf' is ticked, and choose a lovely open and free port to use (for example, you could choose port 8080).

Now, drag in two edit fields and a push button. Rename your large edit field to "ChatLog" and the other, preferably smaller edit field to "ChatString". Oh, you'll need to change the caption of that push button to "Send" and the name of that button to "SendMessage". Make sure the larger editfield has the MultiLine property set to true (hint: it's that little checkbox in the properties section!)

To give it a little bit of tidyness, we'll want to change the font size of the larger ChatLog to 10, and maybe change the font.

Now, let's take a look at what we have here.



Sweet! We do indeed have the makings of an IM application. Go and reward yourself; have a cigarette, make a coffee, do whatever. We are not at the complicated bit. Yet.
« Last Edit: May 30, 2009, 10:46:39 PM by Telstar5 »


Telstar5


  • GMG-er

  • **


  • Posts: 371

  • The sun is up, the sky is blue...
Re: [RB] Networking for Beginners part 2
« Reply #1 on: May 30, 2009, 08:00:01 PM »
Now, double click on the AutoDiscovery socket. In the code editor, select the MemberJoined event and type in...

Code: [Select]
ChatLog.AppendText(ip + " has entered the chatroom." + EndOfLine)

and in the MemberLeft event...

Code: [Select]
ChatLog.AppendText(ip + "has left the chatroom." + EndOfLine)

Great! So whenever a user joins or leaves, it alerts everyone else. That's cool, right?

We ain't got to the hard part yet, buddy. Better get your pens; jot down "Command 10 is a message (broadcast)". This will be your reminder, because we're going to expand on this program as we develop it.

In the ReceivedMessage event, put this code in;

Code: [Select]
  if command = 10 then
 Â   ChatLog.AppendText(fromIP + ": " + data + EndOfLine)
 Â else
 Â end if

So, what we've done here is written half a protocol. The fromIP property is a string which contains the origin IP address, data contains the message they want to send (which is also a string). We're telling our program to take the IP and the data and append it to any other text in our chat log.

Now, let's write the other half. Take a peek at the Action method of our push button. Write in this code;

Code: [Select]
ChatroomConnection.SendMessageToGroup(10, ChatString.Text)

Yay! So, what did we just do here? We've told our socket to send a message to everyone, the command is 10 and that the data we want to send is the text in the ChatString edit field.

Now I know what you're thinking; you're just itching to try it out. No sir, we need to make sure the socket will connect and register as a member of our little network. Under the Window1 Open event, put the following code in:

Code: [Select]
  ChatroomConnection.Bind(6311)
 Â ChatroomConnection.Register("PublicChat")

This registers you as a member. We want this to be a public chatroom, so we're just going to leave this code as-is for now.
« Last Edit: May 30, 2009, 08:06:13 PM by Telstar5 »


Telstar5


  • GMG-er

  • **


  • Posts: 371

  • The sun is up, the sky is blue...
Re: [RB] Networking for Beginners part 3
« Reply #2 on: May 30, 2009, 08:00:58 PM »
Now then, let's take it for a spin. Run our new application, type in some crap and hit send.

Yay, it works! Now, how about we clean it up? Make the send button the default button and add the following line of code to it's action method.

Code: [Select]
ChatString.Text=""

That code clears off what you typed in last. To make it sound a bit better, we want some sounds to put into it; so get yourself a little blip or bloop sound that we can hear each time we get a message.

Under the RecievedMessage event, change the code to:

Code: [Select]
  if command = 10 then
 Â   ChatLog.AppendText(fromIP + ": " + data + EndOfLine)
 Â   <INSERT SOUND NAME HERE>.Play
 Â else
 Â end if

Now, test out the app. If all goes well, your text should clear out automatically and a sound should play each time you receive a message.



Well, was that easy enough for you? Beware, underestimating network programming is easy to do. It seems fun, but once you start having commands set booleans and get those booleans to trigger sends whose IP address is stored under a property which is set by a command, you start using an awful lot of paper and wishing death to all those who might interrupt you.

In my next post, I'll tell you how we can take this to the next level by having things like usernames, private messages and crap like that. Maybe. Stay tuned.

EDIT: Sweet, I managed to make a network program in under 15 lines of code!
« Last Edit: May 30, 2009, 09:19:22 PM by Telstar5 »


Telstar5


  • GMG-er

  • **


  • Posts: 371

  • The sun is up, the sky is blue...
Re: [RB] Networking for Beginners
« Reply #3 on: May 30, 2009, 09:46:04 PM »
[size=16]Networking for Beginners II: With A Vengeance[/size]

So, we've got our little IM application; now what?

We can get it to play sounds when we get a new message, we have buttons and stuff.. but these chatlogs aren't very good, are they? No, and that is why we are going to make them much better by no longer having those horrible IP addresses show up every time.

I must note, if you are following this tutorial, by the end of it, we will have a fully featured app with all sorts of good shit, such as the program remembering your username, ability to save chatlogs, etc.. But for now, we're going to replace the IP address with a handle.

Go ahead and make two new things; a new window, and a new global property under App (this property will be UserName as String; you want this to be a global property accessible anywhere).

Call your window Preferences, you don't really need a title, but put Preferences as both name and title anyway. It won't take much effort; add a single line edit field and a button with an Okay caption. Size your window appropriately.

Now, in the Preferences window's open event...

Code: [Select]
EditField1.Text=(app.UserName)

..and in the button's action code...

Code: [Select]
app.UserName=EditField1.Text

Now, this will require a bit of reworking of that old code we wrote about an hour ago. In the SendMessage button, you'll want to replace all you wrote with...

Code: [Select]
ChatroomConnection.SendMessageToGroup(10, (app.username) + ": " + ChatString.Text)
 Â ChatString.Text=""

This basically just appends the user name to the string we want to send to everyone else; it won't actually attach your user name to an IP address and send it out to everyone. There's a reason for this: life is too fucking short to bother with two-dimensional arrays and indexes, especially when it comes to network stuff. So, here's the REAL lesson here: ALWAYS CUT CORNERS AT EVERY OPPORTUNITY!!

So, now that's over, we can re-write this bit of code under our socket's RecievedMessage event. While we're here, we'll add another command! Make this one command 20; it tells the other user that we've entered the chat room and we really DO have a handle.

Code: [Select]
  if command = 10 then
 Â   ChatLog.AppendText(data + EndOfLine)
 Â   Udo.Play
 Â elseif command = 20 then
 Â   ChatLog.AppendText(fromIP + " is now known as " + data + endofline)
 Â else
 Â end if

and then add this under the Open event of the main window...

Code: [Select]
ChatroomConnection.SendMessageToGroup(20, (app.UserName))

This sends out our username to the rest of the group. We'll be borrowing this line of code to put into the preferences window under the OK button.

Code: [Select]
Window1.ChatroomConnection.SendMessageToGroup(20, (app.UserName))

Finally, add a preferences button in the main window, and paste in the following;

Code: [Select]
Preferences.ShowModalWithin(Window1)

Go ahead, run your app and give it a go. You should see usernames pop up now whenever you enter something into the chatroom.
« Last Edit: May 30, 2009, 10:00:35 PM by Telstar5 »


Gnome


  • GMG Extraordinaire

  • ***


  • Posts: 1073
Re: [RB] Networking for Beginners
« Reply #4 on: May 30, 2009, 11:00:57 PM »
one gnome died in the reading of this tutorial
This Cannot be, NOOOOOOOO!!!!

-Gnomes Cry when the McRib was discontinued again.

Tireas Dragon


  • GMG Extraordinaire

  • ***


  • Posts: 1626

  • Trying to recover from my shattered screen.
Re: [RB] Networking for Beginners
« Reply #5 on: May 31, 2009, 12:42:42 AM »
Quote
one gnome died in the reading of this tutorial
lol
I must be dreaming (wake up me wake up) How could this have happened. Tireas' cry when he found his computer fallen over in his chair with it's screen shattered.