Author Topic: TCP/IP Experiments in QB64  (Read 4300 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
TCP/IP Experiments in QB64
« on: February 23, 2020, 03:05:35 pm »
The samples in this thread demonstrate how to use a local TCP/IP connection to exchange data between two running QB64 programs.

The most updated version of this post can be found at https://github.com/FellippeHeitor/tcpip-experiments

Get a zip of all the code for the samples below at https://github.com/FellippeHeitor/tcpip-experiments/archive/master.zip

Contents
All of the samples below require you to "unblock" them at the first run if you are on Windows, as the OS requests your permission for them to open a port for local communication.


- simple-host-client
drawing.png

This single-module experiment works both as host and client, so you will have to run the executable twice to get to see it working.

On startup, it attempts to connect to an existing host. If that fails, it becomes the host itself and starts listening for connections on port 60710 (no particular reason for the port number - could be anything).

When a client connects, the host sends a "HELLO" message and starts listening for commands. At this point, the user can draw in the client window and see the result in the host window, as mouse data is being transfered.

If the user hits SPACE in the client window, a prompt will be presented and a command can be typed. The available commands are:

- TIME - requests the current time from the host
- DATE - requests the current date from the host
- WISDOM - requests a smart-aleck sentence from the host.
- MSG - sends a message to the host. The format is MSG>your message.
- CLS - instructs the host to clear its window.
- BYE - disconnected from the host and ends the CLIENT execution.

The "protocol" for communication follows the pattern COMMAND>contents<END>, which gets transfered via SUB Send().

Closing either side will also send a BYE message to the other party, so that the connection can be properly closed.


- inform-host-client
inform.png

These are two separate modules written using InForm for QB64. The host waits for a client to connect and then sends commands to render graphics on the client's window.

Launch both and click the "offline" text in the client, so a connection can take place.

After connected, click the buttons in the host to add a "New rectangle" or a "New circle" to the client's PictureBox control. As soon as you create either shape, you can change their color to red with the "Make it Red" button or to a random color with the appropriate button. You can only change the color of the last created shape.

If you click the picture in the client's window, the image will be sent over to the host, which will display it.

The "protocol" for communication is slightly different here, with each message being marked by a "signal" at the start (using the return of the MKI$() function of an arbitraty value), followed by the data to be sent and the <END> marker, which is parsed for.


- windowing-host-client
windowing.png

These two modules are the most ambitious of the trio. With regards to how they communicate, they still work the same as the samples before. The message protocol here is more similar to the first simple-host-client sample above, but the contents being shared are what make this one different.

Run the windowingHost first, and it will begin listening for connections through arbitrarily numbered port 63450.

Then run the windowingClient module. This program will create a window that just displays a random number at the top and paints the window with a random color and places random dots all over.

But you will not see a new program launch this time. Instead, the client will send its rendered contents over TCP/IP to the host, which will display the window inside its own window.

You should launch more instances of the windowingClient to see this sample really shine. Each new instance of the client will get its own window inside the host. These are draggable and you can even click inside them. The clicks will be sent to each client and you will see them react accordingly, by drawing a circle on the indicated coordinates.
« Last Edit: February 23, 2020, 03:16:29 pm by FellippeHeitor »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: TCP/IP Experiments in QB64
« Reply #1 on: February 23, 2020, 03:22:41 pm »
Ah, finally that windows-within-window demo re-surfaces as a polished work! I love it. Maybe I'll use it to join the GUI race. XD
You're not done when it works, you're done when it's right.

Marked as best answer by on October 07, 2024, 06:12:25 pm

FellippeHeitor

  • Guest
Re: TCP/IP Experiments in QB64
« Reply #2 on: February 23, 2020, 03:30:12 pm »
  • Undo Best Answer
  • Ah, finally that windows-within-window demo re-surfaces as a polished work

    Don't be fooled, it's still the same demo. It may (and very likely will) crash if too many clients are launched... depending on how inner windows are manipulated, it may crash even without that many clients connected.

    But it is indeed a working prototype, and I'm glad you enjoy it <3

    Offline Cobalt

    • QB64 Developer
    • Forum Resident
    • Posts: 878
    • At 60 I become highly radioactive!
      • View Profile
    Re: TCP/IP Experiments in QB64
    « Reply #3 on: February 24, 2020, 10:27:16 am »
    How is this different than the Networking of my Battleship game? Beyond that being based in a 2 player game so only host and 1 client.

    Or is this just a different implementation of the same basics?
    Granted after becoming radioactive I only have a half-life!

    FellippeHeitor

    • Guest
    Re: TCP/IP Experiments in QB64
    « Reply #4 on: February 24, 2020, 10:30:52 am »
    You tell me, how is it different? :-)
    « Last Edit: February 24, 2020, 10:31:59 am by FellippeHeitor »

    Offline Cobalt

    • QB64 Developer
    • Forum Resident
    • Posts: 878
    • At 60 I become highly radioactive!
      • View Profile
    Re: TCP/IP Experiments in QB64
    « Reply #5 on: February 24, 2020, 10:40:01 am »
    Ooohhh.. okay. <:-D
    Granted after becoming radioactive I only have a half-life!