Author Topic: TCP/IP Messenger not Working?  (Read 5742 times)

0 Members and 1 Guest are viewing this topic.

Offline Calloway

  • Newbie
  • Posts: 18
  • I still have Laser Disc...
    • View Profile
    • Calloway Sutton
TCP/IP Messenger not Working?
« on: July 23, 2017, 02:20:22 am »
Can anyone help me find the problem as to why TCP/IP will connect but won't send messages?
Here's the code

Code: QB64: [Select]
  1.  
  2. _TITLE "Messenger"
  3.  
  4.  
  5. PRINT "Welcome to Messenger"
  6. LOCATE , , 1
  7. client = _OPENCLIENT("TCP/IP:23456:96.37.234.146")
  8. IF client THEN
  9.     PRINT "[connected to host]"
  10.     _TITLE "Client"
  11.     INPUT "Enter your name: ", myname$
  12.     PRINT #client, myname$ + " connected!"
  13.     PRINT "To Exit, press the Escape key"
  14.     DO
  15.         GetMessage client
  16.         SendMessage myname$, mymessage$, client
  17.         _DELAY 0.01
  18.     LOOP
  19.     PRINT "Server wasn't found, hosting on LAN"
  20.     host = _OPENHOST("TCP/IP:23456")
  21.     IF host THEN
  22.         _TITLE "Host"
  23.         PRINT "[Beginning new chat session!]"
  24.         DIM Users(1 TO 10000)
  25.         numclients = 1
  26.         client = _OPENCLIENT("TCP/IP:23456:96.37.234.146")
  27.         IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!"
  28.         INPUT "Enter your name:", myname$
  29.         PRINT #client, myname$ + " connected!"
  30.         PRINT "To Exit, press the Escape key"
  31.         PRINT "[Chat session active!]"
  32.         DO ' host main loop
  33.             newclient = _OPENCONNECTION(host)
  34.             IF newclient THEN
  35.                 numclients = numclients + 1
  36.                 Users(numclients) = newclient
  37.                 PRINT #Users(numclients), "Welcome!"
  38.             END IF
  39.             FOR i = 1 TO numclients
  40.                 IF Users(i) THEN
  41.                     INPUT #Users(i), message$
  42.                     IF message$ <> "" THEN
  43.                         FOR p = 1 TO numclients
  44.                             IF Users(p) THEN PRINT #Users(p), message$
  45.                         NEXT p
  46.                     END IF
  47.                 END IF
  48.             NEXT i
  49.             GetMessage client
  50.             SendMessage myname$, mymessage$, client
  51.             _DELAY 0.01
  52.         LOOP
  53.     END IF
  54.     PRINT "ERROR: Could not begin new host!"
  55.  
  56.  
  57. 'Frontend below
  58.  
  59.  
  60.  
  61.  
  62. SUB GetMessage (client)
  63. INPUT #client, newmessage$
  64. IF newmessage$ <> "" THEN
  65.     VIEW PRINT 1 TO 23
  66.     LOCATE 23, 1
  67.     PRINT newmessage$
  68.     VIEW PRINT 1 TO 24
  69.  
  70.  
  71. SUB SendMessage (myname$, mymessage$, client)
  72. k$ = INKEY$
  73. IF LEN(k$) THEN
  74.     IF k$ = CHR$(8) AND LEN(mymessage$) <> 0 THEN
  75.         mymessage$ = LEFT$(mymessage$, LEN(mymessage$) - 1)
  76.     ELSE
  77.         IF LEN(k$) = 1 AND ASC(k$) >= 32 THEN mymessage$ = mymessage$ + k$
  78.     END IF
  79. LOCATE 24, 1: PRINT SPACE$(80);
  80. LOCATE 24, 1: PRINT myname$ + ": "; mymessage$;
  81. IF k$ = CHR$(13) THEN ' [Enter] sends the message
  82.     IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program
  83.     PRINT #client, myname$ + ": " + mymessage$
  84.     mymessage$ = ""
  85. IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends program
  86.  


I know that $CHECKING is off but when it's on it doesn't display any errors
« Last Edit: July 23, 2017, 02:22:15 am by Calloway »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: TCP/IP Messenger not Working?
« Reply #1 on: July 23, 2017, 03:24:04 pm »
Hi. Try insert "SHARED mymessage$" to first position in SUB SendMessage. I think, its return as empty string from DO LOOP. Please write us if this help or not.

FellippeHeitor

  • Guest
Re: TCP/IP Messenger not Working?
« Reply #2 on: July 23, 2017, 09:25:31 pm »
Forget about PRINT and INPUT for TCP/IP. They are buggy in the current version.

Use PUT and GET instead.

Read this article for extensive reference and ideal methods: https://qb64.org/articles/articletcpip.txt

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: TCP/IP Messenger not Working?
« Reply #3 on: July 24, 2017, 12:15:02 am »
yeah, print # and input # work great in the SDL version of QB but not the GL.
so if your using the current build change your Print # to Put # and your Input # to Get #.

and make sure that variables are all shared between SUBs or returned by Functions.(get message would be better as a function in my option but I guess it depends on where in the program the message gets displayed)
Granted after becoming radioactive I only have a half-life!