I got the same need some time ago and have created this as a basis:
tcp_server.bas:
tcp_clients.bas:
All client messages are sent to all other clients
tcp_server.bas:
Code: (Select All)
$Console:Only
Dim Shared crlf As String * 2: crlf = Chr$(13) + Chr$(10)
Dim Shared hostConn&, conn&(100), conns%
hostConn& = _OpenHost("TCP/IP:1234")
Print _ConnectionAddress$(hostConn&)
Do
_Limit 10
newConn& = _OpenConnection(hostConn&)
If newConn& <> 0 Then
conns% = conns% + 1: conn&(conns%) = newConn&
Print "connections:"; conns%
End If
If Abs(Timer - tim!) > .1 Then
out$ = Time$ + " "
c% = 1
Do While c% <= conns%
If _Connected(conn&(c%)) = 0 Then
Close conn&(c%)
For i% = c% + 1 To conns%
conn&(i% - 1) = conn&(i%)
Next i%
conns% = conns% - 1
Print "connections:"; conns%
Else
Get #conn&(c%), , in$: If in$ <> "" Then Print c%, in$
Put #conn&(c%), , out$
End If
c% = c% + 1
Loop
tim! = Timer
End If
Loop Until InKey$ <> ""
Close hostConn&
System
tcp_clients.bas:
Code: (Select All)
Do
_Limit 20
Do
m$ = receive$
If m$ <> "" Then Print m$;
Loop Until m$ = ""
Loop Until InKey$ = Chr$(27)
Close connect&
System
Function send& (m$)
c& = connect&
If c& Then Put #c&, , m$
send& = c&
End Function
Function receive$
c& = connect&
If c& Then Get #c&, , m$
receive$ = m$
End Function
Function connect&
Static conn&, hconn&
If conn& Then If _Connected(conn&) Then connect& = conn&: Exit Function
If hconn& Then
conn& = _OpenConnection(hconn&)
Else
conn& = _OpenClient("TCP/IP:1234:localhost")
If Not conn& Then
hconn& = _OpenHost("TCP/IP:1234")
If hconn& Then conn& = _OpenConnection(hconn&)
End If
End If
connect& = conn&
End Function
All client messages are sent to all other clients
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience

