Platforms to show: All Mac Windows Linux Cross-Platform

Back to RAWSocketMBS class.

RAWSocketMBS.Accept(byref RemoteAddress as String, byref RemotePort as Integer, NewSocket as RAWSocketMBS) as RAWSocketMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 18.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Listens for connections on this socket.

Lasterror is set.

RAWSocketMBS.Bind(DestAddr as Ptr, DestAddrByteSize as Integer) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Binds the socket to the given destination address.

Set ReuseAddress to bind to an address which already a socket is bound to.
Lasterror is set.
Returns true on success and false on failure.
Please specify with port and IP in destAddress where to bind against.
Structure of DestAddress is OS and address family dependent.

See also:

RAWSocketMBS.Bind(Port as Integer, IP as string = "")

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Binds the socket to the given port.

If IP is not empty, you can bind the socket to only the given network interface with that IPv4.
Set ReuseAddress to bind to an address which already a socket is bound to.
Lasterror is set.

See also:

RAWSocketMBS.Close

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 18.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Closes socket.

Same as destructor.

RAWSocketMBS.Connect(Address as String, Port as Integer, byref ErrorMessage as String) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 18.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Convenience method to connect.

Will parse IPv4/IPv6 or query DNS server for IP and connect.
Returns true on success.

See also:

RAWSocketMBS.Connect(DestAddr as Ptr, DestAddrByteSize as Integer) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Connects to destination address.

lasterror is set.
Returns true on success and false on failure.
Please specify with port and IP in destAddress where package is sent to.
Structure of DestAddress is OS and address family dependent.

See also:

RAWSocketMBS.Constructor(AddressFamily as Integer, SocketType as Integer, Protocol as Integer)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The constructor.
Example
dim sock as RAWSocketMBS

// create UDP socket
sock = new RAWSocketMBS(RAWSocketMBS.AddressFamilyINet, RAWSocketMBS.SocketTypeDatagram, RAWSocketMBS.ProtocolUDP)

Please pass valid combination of OS supported sockets.
Raises exception if creation is not possible.
Raw socket requires root access.

RAWSocketMBS.Destructor

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The destructor.

Closes the socket.

RAWSocketMBS.Listen(BackLog as Integer = 2)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 18.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Accepts a new incoming connection.

Sockets uses the accept function to enable incoming connection attempts on a socket.
RemoteAddress and RemotePort are set to address of remote peer.

If you like to pass in your own subclass object for NewSocket, we can use that socket object. Otherwise, we make a new RAWSocketMBS object.

Lasterror is set.

RAWSocketMBS.Poll

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 18.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Polls for activity.

Calls events if needed.
This is called automatically via timer by the plugin.

RAWSocketMBS.Read(ByteSize as Integer, peek as boolean = false) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Read at maximum the given number of bytes in buffer.

Pass true for peek if data should not be removed from receiving buffer.
Lasterror is set.

Be aware that you may not get whole packages here, so data may come in several chunks.
For that, please collect the data and look for whether your package is complete or a required end mark arrived.

RAWSocketMBS.ReadAll(peek as boolean = false) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Read all bytes in buffer.
Example
dim sock as RAWSocketMBS
dim data as MemoryBlock = sock.ReadAll
if data = nil then
// nothing read
else

end if

Pass true for peek if data should not be removed from receiving buffer.
Lasterror is set.

Be aware that you may not get whole packages here, so data may come in several chunks.
For that, please collect the data and look for whether your package is complete or a required end mark arrived.

RAWSocketMBS.ReadDatagram(peek as boolean = false) as DatagramMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Reads a datagram.

If peek is true, the data is not removed from read buffer.
On error returns nil.

Some examples using this method:

RAWSocketMBS.Send(Data as Ptr, DataByteSize as Integer, Flags as Integer = 0) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sends data.

Data is sent to address passed with Connect method.
Returns number of bytes sent.
Lasterror is set.

For TCP sockets and similar, which have a stream of data.

RAWSocketMBS.SendMessage(Data as DatagramMBS) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sends a datagram.

Please specify with port and address of datagram where package is sent to.
Returns number of bytes sent.
Lasterror is set.

For UDP sockets and similar.

See also:

RAWSocketMBS.SendMessage(Data as MemoryBlock, IP as String, Port as Integer) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sends a datagram.

Please specify with port and IP where package is sent to.
Returns number of bytes sent.
Lasterror is set.

For UDP sockets and similar.

See also:

RAWSocketMBS.SendMessage(Data as string, IP as String, Port as Integer) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sends data to target.

Please specify with port and IP where package is sent to.
Returns number of bytes sent.
Lasterror is set.

For UDP sockets and similar.

See also:

RAWSocketMBS.SendTo(Data as Ptr, DataByteSize as Integer, Flags as Integer, DestAddr as Ptr, DestAddrByteSize as Integer) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Network MBS Network Plugin 17.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sends data to dest address.
Example
dim sock as RawSocketMBS
dim Flags as integer
dim r as Integer

// port
dim port as integer = val(iPort.Text)
if port <= 0 or port >= 65535 then
break
MsgBox "Wrong port: "+iPort.Text
Return
end if

// dest address

'/*
'* Socket address, internet style. C declaration for Mac
'*/
'struct sockaddr_in {
'__uint8_t sin_len; // 1 byte, Mac only
'sa_family_t sin_family; // 1 byte
'in_port_t sin_port; // 2 byte in Network byte order
'struct in_addr sin_addr; // 4 byte
'char sin_zero[8]; // 8 bytes filler

'};

// convert IPv4 from text to numeric value
dim IP as Uint32 = RAWSocketMBS.inet_addr(iIP.Text)

// create destination address. This is for Mac! Windows and Linux have different structures
dim dest as new MemoryBlock(16)
dest.UInt8Value(0) = dest.size
dest.UInt8Value(1) = RAWSocketMBS.AddressFamilyINet
dest.UInt16Value(2) = RAWSocketMBS.htons(port)
dest.UInt32Value(4) = IP

// message
dim m as string = ConvertEncoding(iMessage.Text, encodings.UTF8)
dim data as MemoryBlock = m

// send
r = sock.SendTo(data, data.size, Flags, dest, dest.size)

Please specify with port and IP in destAddress where package is sent to.
Returns number of bytes sent. Structure of DestAddress is OS and address family dependent.
Lasterror is set.

For UDP sockets and similar.

Some examples using this method:

The items on this page are in the following plugins: MBS Network Plugin.


The biggest plugin in space...