Mini Messenger
#2
And, here's a version posted by Zeppelin which combines both utilities into one:

Code: (Select All)
DIM SHARED Users(1 TO 1000) ' array to hold other client info
DIM SHARED NumClients
DIM SHARED OUT$


PRINT "[Steve's Mini Messenger]"
host = _OPENHOST("TCP/IP:7319") ' no host found, so begin new host
IF host THEN
    PRINT "[Beginning new host chat session!]"
    NumClients = 0
    client = _OPENCLIENT("TCP/IP:7319:localhost")
    IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!"
    INPUT "Enter your name:", myname$
    'PRINT #client, myname$ + " connected!"
    PRINT "[Chat session active!]"
ELSE
    PRINT "ERROR: Could not begin new host!"
    PRINT "JOINING as CLIENT"

    'Join as client
    client = _OPENCLIENT("TCP/IP:7319:localhost") ' Attempt to connect to local host as a client
END IF ' host

IF host THEN
    _TITLE "HOST"
    DO ' host main loop
        newclient = _OPENCONNECTION(host) ' receive any new connection
        IF newclient THEN
            NumClients = NumClients + 1
            Users(NumClients) = newclient
            PRINT "Welcome to Steve's Mini Messenger!"
        END IF
        FOR i = 1 TO NumClients
            GetMessage Users(i) 'check all clients for a message
            IF OUT$ <> "" THEN
                l = LEN(OUT$)
                FOR j = 1 TO NumClients ' distribute incoming messages to all clients
                    PUT #Users(j), , l
                    PUT #Users(j), , OUT$
                NEXT
            END IF
        NEXT i

        SendMessage myname$, mymessage$, client
        _LIMIT 30
    LOOP
END IF

IF client THEN
    INPUT "Enter your name: ", myname$
    OUT$ = myname$ + " connected!"
    l = LEN(OUT$)
    PUT #client, , l
    PUT #client, , OUT$

    _TITLE "CLIENT"
    DO
        GetMessage client
        SendMessage myname$, mymessage$, client ' display current input on screen
        _LIMIT 30
    LOOP
END IF


SUB GetMessage (client) ' get & display any new message
    GET #client, , l
    IF l > 0 THEN
        OUT$ = SPACE$(l)
        GET #client, , OUT$
        VIEW PRINT 1 TO 20
        LOCATE 20, 1
        PRINT OUT$
        VIEW PRINT 1 TO 24
    ELSE
        OUT$ = ""
    END IF
END SUB

SUB SendMessage (myname$, mymessage$, client) ' simple input handler
    k$ = INKEY$
    IF LEN(k$) THEN
        IF k$ = CHR$(8) AND LEN(mymessage$) <> 0 THEN
            mymessage$ = LEFT$(mymessage$, LEN(mymessage$) - 1)
        ELSE
            IF LEN(k$) = 1 AND ASC(k$) >= 32 THEN mymessage$ = mymessage$ + k$
        END IF
    END IF
    VIEW PRINT 1 TO 24
    LOCATE 22, 1: PRINT SPACE$(80); ' erase previous message displayed
    LOCATE 22, 1: PRINT myname$ + ": "; mymessage$;
    IF k$ = CHR$(13) THEN ' [Enter] sends the message
        IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program
        mymessage$ = myname$ + ":" + mymessage$
        l = LEN(mymessage$)
        PUT #client, , l
        PUT #client, , mymessage$
        mymessage$ = ""
    END IF
    IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends program
END SUB
Reply


Messages In This Thread
Mini Messenger - by SMcNeill - 05-01-2022, 10:31 PM
RE: Mini Messenger - by SMcNeill - 05-01-2022, 10:33 PM
RE: Mini Messenger - by Ikerkaz - 12-08-2022, 05:42 PM
RE: Mini Messenger - by SMcNeill - 12-08-2022, 06:10 PM
RE: Mini Messenger - by Ikerkaz - 12-09-2022, 03:55 PM
RE: Mini Messenger - by Coolman - 05-02-2022, 09:49 AM
RE: Mini Messenger - by James D Jarvis - 05-02-2022, 01:08 PM
RE: Mini Messenger - by RUBIDIUM - 07-05-2022, 03:52 AM
RE: Mini Messenger - by Pete - 12-08-2022, 08:05 PM



Users browsing this thread: 4 Guest(s)