BlitzMax

BlitzMax

  • Downloads
  • Docs
  • API
  • Resources
  • About

›Tutorials

Setup

  • Getting Started
  • Win32
  • Linux
  • macOS
  • Android
  • iOS
  • Raspberry Pi
  • NX (Switch Homebrew)
  • Custom Settings

Tutorials

  • Beginners Guide
  • OOP Tutorial
  • Network Programming
  • TCP Socket Programming

Language

  • Arrays
  • Basic Compatibility
  • Collections
  • Comments
  • Conditional Compiling
  • Constants
  • Data Types
  • Debugging
  • Enums
  • Exceptions
  • Expressions
  • Functions
  • Identifiers
  • Literals
  • Modules
  • Objects
  • Program Flow
  • Slices
  • Strings
  • User Defined Types
  • Variables
  • Advanced Topics

    • Interfacing With C
    • Memory Management
    • Pointers
    • Custom Pre/Post Compilation Scripts
    • Creating DLLs

Tools

  • MaxIDE
  • BlitzMax Make (bmk)
  • BlitzMax Compiler (bcc)
Edit

Introduction to Network Programming in BlitzMax

Sockets

Sockets are the fundamental "things" behind the network communication performed by your computer. For example, when your web browser displays the page for "www.google.com", it opens a socket and connects to google.com in order to fetch everything.

Sockets are the end-point of a two-way communication link, where each socket listens for incoming messages, and replies with appropriate responses, thereby establishing bidirectional communication.

There are several different kinds of sockets, but for this guide we are going to focus on the two most common kinds you'll come across in BlitzMax, stream (TCP) sockets and datagram (UDP) sockets :

Creating a Socket

Before we create a socket, we define a TAddrInfo object to provide hints for the kind of socket we want to create. This is the modern way to handle sockets, and it simplifies a lot of the coding we'll need to do when dealing with them.

Local hints:TAddrInfo = New TAddrInfo(AF_INET_, SOCK_STREAM_)

The first argument AF_INET_ requests that the socket use the IPv4 protocol. For IPv6 you can use AF_INET6_, and if you don't care which protocol to use, you can use AF_UNSPEC_, which will allow you to use either.

The second argument specifies the kind of socket we want to create. For UDP we could also use SOCK_DGRAM_.

To create our stream socket, we use the TSocket.Create function, passing in our hints as shown in the example below:

' create an AF_INET (IPv4), STREAM socket (TCP)
Local hints:TAddrInfo = New TAddrInfo(AF_INET_, SOCK_STREAM_)

Local socket:TSocket = TSocket.Create(hints)

Print "Socket Created"

The TSocket.Create function creates a socket and returns a TSocket object. You use this object for all of your subsequent socket communications.

Handling Errors

If there was a problem getting a socket, TSocket.Create will return Null, which you should really handle before trying to use your socket object:

Local hints:TAddrInfo = New TAddrInfo(AF_INET_, SOCK_STREAM_)

Local socket:TSocket = TSocket.Create(hints)

If Not socket Then
    Print "Unable to create socket."
    End
End If

Print "Socket Created"
← OOP TutorialTCP Socket Programming →
  • Sockets
  • Creating a Socket
  • Handling Errors
BlitzMax
Docs
Getting StartedDownloadsAbout
Community
ResourcesSyntaxBomb Forums
More
GitHubStarChat on Discord
Copyright © 2023 Bruce A Henderson