Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP/IP. data from one program to another
#7
I got the same need some time ago and have created this as  a basis:

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
Reply


Messages In This Thread
RE: TCP/IP. data from one program to another - by mdijkens - 02-16-2025, 08:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  where can I get more info about TCP/IP in QB64? TempodiBasic 4 1,138 08-02-2024, 08:51 AM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: 1 Guest(s)