Help with TCP/IP problem

by Zack (no login)

Hi, I am using a slightly modified version of the sample code from the website for a little program I decided to make.

This is it:
---
client = _OPENCLIENT("TCP/IP:7319:192.168.2.4")
IF client THEN
_TITLE "Client - Messenger"
INPUT "Enter your name: ", myname$
PRINT #client, myname$ + " connected!"
LOCATE 24, 1
PRINT myname$; ":";
DO
GetMessage client
SendMessage myname$, mymessage$, client
_DELAY 0.01
LOOP
ELSE
host = _OPENHOST("TCP/IP:7319")
IF host THEN
_TITLE "Host - Messenger"
DIM Users(1 TO 10000000)
numclients = 0
client = _OPENCLIENT("TCP/IP:7319:localhost")
INPUT "Enter your name: ", myname$
PRINT #client, myname$ + " connected!"
LOCATE 24, 1
PRINT myname$; ":";
DO
newclient = _OPENCONNECTION(host)
IF newclient THEN
numclients = numclients + 1
Users(numclients) = newclient
PRINT #Users(numclients), "Welcome!"
END IF
FOR i = 1 TO numclients
IF Users(i) THEN
INPUT #Users(i), message$
IF message$ <> "" THEN
FOR p = 1 TO numclients
IF Users(p) THEN PRINT #Users(p), message$
NEXT p
END IF
END IF
NEXT i
GetMessage client
SendMessage myname$, mymessage$, client
_DELAY 0.01
LOOP
END IF
END IF
SLEEP
SYSTEM

SUB GetMessage (client)
INPUT #client, newmessage$
IF newmessage$ <> "" THEN
VIEW PRINT 1 TO 23
LOCATE 23, 1
PRINT newmessage$
VIEW PRINT 1 TO 24
END IF
END SUB

SUB SendMessage (myname$, mymessage$, client)
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
LOCATE 24, 1
PRINT SPACE$(80);
LOCATE 24, 1
PRINT myname$ + ": "; mymessage$;
IF k$ = CHR$(13) THEN
IF LEN(mymessage$) > 0 THEN
PRINT #client, myname$ + ": " + mymessage$
mymessage$ = ""
LOCATE 24, 1
PRINT SPACE$(80);
LOCATE 24, 1
PRINT myname$ + ": ";
END IF
END IF
END IF
END SUB
---

This is where i got it from:
http://www.qb64.net/wiki/index.php?title=OPENHOST


It works fine until I close one of the clients. Then it will start printing two of whatever anyone says. I think this is because the host sends the message that someone said out to everyone else. Everytime a new user enters numclients increases by one, so there is one extra FOR/NEXT loop even after that user leaves.

I tried making a FOR/NEXT loop at the beginning of the hosts DO/LOOP that checked each connect_handle, and if it returned a number larger than 0 then it would subtract one from numclients. This didn't work however because the person who left might have been the first person to join. Then when it subtracted one, that users number was left blank and the newest person to join was no longer part of numclients.

---
newclient = _OPENCONNECTION(host)
...
numclients = numclients + 1
Users(numclients) = newclient
---

I need a way to constantly tell me how many clients are running at one time. I tried a bit, but could not figure out a way to do it.

Any help will be greatly appreciated

Posted on Apr 13, 2011, 3:33 PM

Respond to this message   

Return to Index


Response TitleAuthor and Date
Re: Help with TCP/IP problem on Apr 13
 Thanks :DZack on Apr 14