11-29-2024, 03:14 PM
here is my modified code:
Code: (Select All)
Rem Host
Randomize Timer
Rem Project to simulate a server with multiple clients TCP/IP
Rem the first program is the server side of the network
Dim bgColor As _Unsigned Long
bgColor = _RGBA32(254, 0, 255, 255)
Screen _NewImage(480, 240, 32) 'Sets screenmode
Cls , bgColor
_ScreenMove (_DesktopWidth / 2) - (_Width / 2), (_DesktopHeight / 2) - (_Height / 2)
_PrintString (10, 100), "Searching for host..."
Dim Server As Long
Server = _OpenClient("TCP/IP:65535:localhost")
If Server Then
_PrintString (10, 130), "Host found. Press any key to exit."
Sleep
System
Else
_PrintString (10, 130), "Host not found. Initialization beginning."
Server = _OpenHost("TCP/IP:65535")
If Not Server Then
_PrintString (10, 200), "Host initialization failed."
Else
_PrintString (10, 200), " Host initialization successful."
End If
End If
Rem Initiate client here (optional)
Rem here Host searches connections with clients and if activated it reads data
Dim Client(0 To 65534) As Long, ID As _Unsigned Integer, Instruction As String
ID = 1
Do
ID = Int(Rnd * 65535)
If ID = 65535 Then ID = 0
If Client(ID) = 0 Then Client(ID) = _OpenConnection(Server) ' Is cli(ID) connected?
Instruction = "" ' it deletes previous command read by client
If Client(ID) <> 0 Then
If Not EOF(Client(ID)) Then Get #Client(ID), , Instruction ' has Client(ID) sent data?
End If
' data executor
Select Case Instruction
Case "x"
Cls , bgColor
_PrintString (10, 100), "Client" + Str$(ID) + " quits"
Case "c"
bgColor = _RGBA32((Rnd * (255)) + 1, (Rnd * (255)) + 1, (Rnd * (255)) + 1, 255)
Cls , bgColor
_PrintString (10, 100), "Client" + Str$(ID) + " changes background color"
' it changes color
Case "s"
Play "cdc"
Cls , bgColor
_PrintString (10, 100), "Client" + Str$(ID) + " makes noise"
End Select
_PrintString (10, 200), " Press q to quit"
Loop Until InKey$ = "q"
Close
System
Code: (Select All)
Rem Client
Dim bgColor As _Unsigned Long
bgColor = _RGBA32(128, 200, 67, 255)
Screen _NewImage(400, 320, 32)
Cls , bgColor
_ScreenMove (_DesktopWidth / 2) - (_Width / 2), (_DesktopHeight / 2) - (_Height / 2)
_PrintString (10, 100), "Connecting to Host..."
Dim Server As Long
Server = _OpenClient("TCP/IP:65535:localhost")
If Server Then
_PrintString (10, 130), "Connected."
Else
_PrintString (10, 130), "Connection failed. Retrying..."
Server = _OpenClient("TCP/IP:65535:localhost") 'Try one more time
If Not Server Then
_PrintString (10, 260), "Connection failed."
Sleep
System
Else
_PrintString (10, 260), " Connected."
End If
End If
_Title Str$(Server)
Rem Client waits for input and transmits to Host
Dim Instruction As String
Do
_Delay 1
Cls , bgColor
_PrintString (10, 100), "Client is connected to host."
_PrintString (10, 130), "Press c to change host background color"
_PrintString (10, 160), "Press s to make noise "
_PrintString (10, 190), "Press x to quit"
Instruction = InKey$
Instruction = LTrim$(RTrim$(LCase$(Instruction)))
If Len(Instruction) Then Put #Server, , Instruction ' If new instruction, then send to Host
Select Case Instruction
Case "x":
Cls , bgColor
_PrintString (10, 100), "Disconnecting from Host..."
Sleep 1
_PrintString (10, 130), "Terminating Client..."
Sleep 1
_PrintString (10, 160), "Bye!"
Sleep 1
System
Case "c"
_PrintString (10, 230), "Changing color on Host..."
Case "s"
_PrintString (10, 230), "Playing sound on Host..."
End Select
Loop Until Instruction = "x"
Close
System