BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Tiny TCP chat"
, by thechange
Demonstration of the TCP commands using a tiny program.
Code
; The program below, a one-on-one 'multi-chat' program, can be tested ; by launching two of one kind on the same computer. ; Using the CommandLine (also accessible from the Blitz IDE ; pulldown menu 'Program') to specify which is the "Host" ; or by default, the client. ; ; Connection sequence - ; Server creates a server: ; ; ServerHandle = CreateTCPServer ( LocalPort ) ; ; Client connects to server: ; ; StreamToServer = OpenTCPStream ( ServerIP$ , ServerPort ) ; ; Server accepts incoming connection by listening: ; ; StreamToClient = AcceptTCPStream ( ServerHandle ) ; Graphics 800 , 400 , 0 , 2 ; Windowed Const ServerPort = 2012 ; Port to use (of server) ; If you do not have multiple computers to test this out ; on your network, you can start 2 instances of Blitz ; and let them connect to each other on the same computer. ; Localhost, or "127.0.0.1". Const ServerIP$ = "127.0.0.1" ; Use the command line feature of Blitz to control ; whether this program should act as either host or client. ; Specify "Host" to serve, in all other cases client. ; This way you can also create an executable ; and launch it from the command line with parameters. If CommandLine () = "Host" ;= Master/Host AppTitle "Server" ;- Open serverside TCP port ServerHandle = CreateTCPServer ( ServerPort ) ;- Make sure the server was created succesfully If Not ServerHandle Then RuntimeError "Unable to host on port " + ServerPort ;- Listen for incoming connection (from client) Print "Waiting for connection..." Repeat Stream = AcceptTCPStream ( ServerHandle ) Until Stream ; Non-zero means succesful Else ;= Slave/Client AppTitle "Client" ;- Attempt to connect to host Print "Trying to connect..." Repeat Stream = OpenTCPStream ( ServerIP , ServerPort ) Until Stream ; Connection established End If Print "Connected" ;= Interact/chat Repeat Value = GetKey () ; Check for keystrokes If Value ; Key pressed ;- Send and display keystrokes WriteByte Stream , Value ; Send value of key to remote side ;- Support for linefeeds (local linefeed when pressed return/enter key) If Value = 13 Then Print Else Write Chr ( Value ) End If If ReadAvail ( Stream ) ; We got incoming ;- Display keystrokes when received RemoteValue = ReadByte ( Stream ) ; Value of remote key pressed ;- Support for linefeeds (local linefeed on remote return/enter key) If RemoteValue = 13 Then Print Else Write Chr ( RemoteValue ) End If Until Value = 27 ; Escape ;= Disconnect ;- Close stream to remote side (both host/client) CloseTCPStream Stream ;- Close server (stop hosting) If CommandLine () = "Host" CloseTCPServer ServerHandle End If End ;,,,, ; Yes, it can be smaller. ;---- ; ; Graphics 800 , 400 , 0 , 2 ; ; If CommandLine () = "Host" ; ServerTCP = CreateTCPServer ( 8080 ) ; Create server on primary IP on TCP port ; Repeat ; ConnectTCP = AcceptTCPStream ( ServerTCP ) ; Wait for incoming connection (1) ; Until ConnectTCP ; Else ; Client ; Repeat ; ConnectTCP = OpenTCPStream ( "127.0.0.1" , 8080 ) ; Connect to (local)host IP on TCP port ; Until ConnectTCP ; End If ; ; Repeat ; Value = GetKey () ; If Value ; Send and display keystroke ; WriteByte ConnectTCP , Value ; Print Chr ( Value ) ; End If ; If ReadAvail ( ConnectTCP ) ; Display incoming keystroke ; Print Chr ( ReadByte ( ConnectTCP ) ) ; End If ; Until Value = 27 ; Escape ; ; If CommandLine () = "Host" ; CloseTCPServer ServerTCP ; Close server ; End If ; CloseTCPStream ConnectTCP ; Free TCP port ; ; End ; ;,,,, ; ;----
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations