BBC BASIC for Windows
Programming >> Communication and Input/Output >> Server / Client
http://bb4w.conforums.com/index.cgi?board=communication&action=display&num=1434014560

Server / Client
Post by dfeugey on Jun 11th, 2015, 09:22am

I tried this : http://rosettacode.org/wiki/Echo_server#BBC_BASIC
And this (with same port) : http://rosettacode.org/wiki/Sockets#BBC_BASIC
... and I get no result.

The first program does not seem to receive any request, even from a telnet client. Firewall is OK. Computer is a Win7 Pro 64bit one. The second program gives me a prompt, and nothing else.

I suspect some changes to the SocketLib.
Do you have example of working code?

I try to make a link between two computers, with encryption, and possibly on the fly audio catching/sending.

Re: Server / Client
Post by sveinioslo on Jun 29th, 2015, 10:57pm

The Echo_server program is working ok.
Start telnet, use your local ip.
Telnet> o 192.168.0.31 12321
You get an echo of what you typed in when you hit enter.

Quote:
The first program does not seem to receive any request

It may appear so, there is no print statement in the code, only 'echo' to the sender.

You might find it easier to understand or work with these two examples.

The sender:
Code:
      REM socksend.bbc
      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets

      ON ERROR ON ERROR OFF:PROC_exitsockets:PRINT 'REPORT$:END
      ON CLOSE PROC_exitsockets:QUIT

      host$ = FN_gethostname : REM if sockreceive is running on the same computer as socksend
      REM host$= "1.2.3.4" : REM remote IPaddr if sockreceive is running on another computer
      port$ = "12321"

      connect_socket% = FN_tcpconnect(host$,port$)
      IF connect_socket% <= 0 ERROR 100, "Failed to make connection"

      FOR test% = 1 TO 10
        WAIT 25
        test$ = "The quick brown fox "+STR$test%+CHR$13+CHR$10
        bytes_written% = FN_writesocket(connect_socket%, !^test$, LEN(test$))
        IF bytes_written% < LEN(test$) ERROR 100, "Failed to write entire packet"
        PRINT "Wrote ";test$
      NEXT test%
      test$=CHR$13+CHR$10
      bytes_written% = FN_writesocket(connect_socket%, !^test$, LEN(test$))

      PROC_closesocket(connect_socket%)
      PROC_exitsockets
      PRINT "Finished"
      END
 


And the receiver:
Code:
      REM sockreceive.bbc
      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets

      ON ERROR ON ERROR OFF:PROC_exitsockets:PRINT REPORT$" at line ";ERL:END
      ON CLOSE PROC_exitsockets:QUIT

      host$ = FN_gethostname
      port$ = "12321"

      REPEAT
        listen_socket% = FN_tcplisten(host$,port$)
        IF listen_socket% <= 0 THEN
          PRINT "Failed to create listen_socket"
        ELSE
          PRINT '"Listening on port ";port$
          REPEAT
            WAIT 1
            accept_socket% = FN_check_connection(listen_socket%)
          UNTIL accept_socket%
    
          REPEAT
            IF FN_readlinesocket(accept_socket%, 6000, head$)
            PRINT head$
          UNTIL head$=""
        ENDIF
  
        PROC_closesocket(accept_socket%)
      UNTIL FALSE
 


Svein

Re: Server / Client
Post by dfeugey on Jun 30th, 2015, 12:01pm

Thanks Svein.

Echo server works, but not with localhost (as name or IP).
Rosetta code to send data does not work.

All the code you submitted works very well, and will be very useful!
Thanks smiley

Re: Server / Client
Post by rtr2 on Jun 30th, 2015, 12:52pm

on Jun 11th, 2015, 09:22am, dfeugey wrote:
http://rosettacode.org/wiki/Sockets#BBC_BASIC
... and I get no result.
The second program gives me a prompt, and nothing else.

You say that it "gives me a prompt, and nothing else", but the correct behaviour of the program is to attempt to send a packet to 'localhost' (and ignore any errors) and then exit to the normal immediate-mode prompt. So your description seems to fit the expected behaviour. What do you believe is not working correctly?

I use SOCKLIB extensively and am not encountering any problems. You will find several examples of socket-based programs at the Yahoo group file repository, for example socksend.bbc, sockrecv.bbc, EchoServer.bbc and EchoClient.bbc:

https://groups.yahoo.com/neo/groups/bb4w/files/Miscellaneous/

Richard.

Re: Server / Client
Post by dfeugey on Jul 1st, 2015, 12:37pm

In fact the localhost part (or 127.0.0.1) is one of the two problems. If I replace it with FN_gethostname, it's OK. Bug?

msg$ = "hello socket world" is not received. Classic, since server make a readline. My fault here. So I add CHR$13+CHR$10. Then I forget to send an empty CHR$13+CHR$10 and the server made strange things.

The Rosetta Code server part is interesting, but with no client or possible interaction with Telnet, it's difficult to play with it as is.

Your code is a bit more useful here smiley
Thanks. It gives me a lot of ideas.

Re: Server / Client
Post by rtr2 on Jul 1st, 2015, 4:07pm

on Jul 1st, 2015, 12:37pm, dfeugey wrote:
In fact the localhost part (or 127.0.0.1) is one of the two problems. If I replace it with FN_gethostname, it's OK. Bug?

It doesn't sound like a bug to me, and anyway it's clearly nothing to do with BBC BASIC or SOCKLIB since neither of those knows anything about specific IP addresses.

Remainder of message deleted by request. Apparently its tone was "completely unacceptable".
Re: Server / Client
Post by dfeugey on Jul 1st, 2015, 11:20pm

Hum, OK. But why having this FN_tcpconnect("localhost", "256") on Rosetta code if it does not work with modern version of BBC Basic or Windows or SocketLib or whatever?

Both localhost and 127.0.0.1 do not work here. I can confirm that under Xojo, it works normally (perhaps with an internal trick). It's not a big deal, but examples could be modified... or not smiley

I appreciate the bunch of examples on Wiki / Yahoo / Rosetta. But there are possibilities to go even further. Examples are essentials. That's your Midi code that convince me to adopt BBC4Win.

Now with working Socket examples, I can almost forget Xojo (I use it a lot for network projects).
Re: Server / Client
Post by rtr2 on Jul 2nd, 2015, 04:39am

on Jul 1st, 2015, 11:20pm, dfeugey wrote:
Hum, OK. But why having this FN_tcpconnect("localhost", "256") on Rosetta code if it does not work with modern version of BBC Basic or Windows or SocketLib or whatever?

But it does work! If the FN_tcpconnect failed the program would display the message "Failed to open socket" but (here, at least, on Windows 8.1) no such message is displayed - the program proceeds to completion and the immediate-mode prompt is printed. So it must have successfully opened a socket to 'localhost' on port 256.

Are you saying that the subsequent send fails, even if there is a recipient listening on port 256, or what?

Richard.
Re: Server / Client
Post by dfeugey on Jul 2nd, 2015, 06:11am

Nope. If I replace host$ = FN_gethostname by host$ = "localhost" or "127.0.0.1" I have a "Failed to make connection" in Svein program.
With this code (from Rosetta) I get no error... but there is in fact an error in the code, as socket%<0. So no connection.

Code:
      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets

      socket% = FN_tcpconnect("localhost", "12321")
      IF socket% = 0 ERROR 100, "Failed to open socket"

      REM Don't use FN_writesocket since an error is expected
      msg$ = "hello socket world"+CHR$13+CHR$10
      SYS `send`, socket%, !^msg$, LEN(msg$), 0 TO result%

      msg$ = CHR$13+CHR$10
      SYS `send`, socket%, !^msg$, LEN(msg$), 0 TO result%

      PROC_closesocket(socket%)
      PROC_exitsockets
 

IF socket% = 0 ERROR 100, "Failed to open socket"
Should be...
IF socket% <= 0 ERROR 100, "Failed to open socket"

Hope it'll help.

I suspect some security issue with ports > to a certain number, or with Windows 7 Pro policies. Nota: I have a stock Win7 Pro with clean and unchanged config. Of course, server is authorized in the firewall.
Re: Server / Client
Post by rtr2 on Jul 2nd, 2015, 3:36pm

on Jul 2nd, 2015, 06:11am, dfeugey wrote:
IF socket% = 0 ERROR 100, "Failed to open socket"
Should be...
IF socket% <= 0 ERROR 100, "Failed to open socket"

Right, it seems that you have found a weakness in that Rosetta Code submission - so please correct it (that particular submission is not one of mine)!

Nevertheless, I still cannot reproduce the behaviour you describe. Here, if I run the Rosetta Code program exactly as published (Windows 8.1 64-bit, BB4W 6.00a, SOCKLIB v1.4) I get a positive number returned in socket%, not an error return - so long as a recipient program is listening on port 256 of course.

I don't understand why you are getting a different result. All my tests confirm that you can successfully open a TCP connection to 'localhost' on port 256. Are you certain that your 'listening' program is working as it should?

Richard.
Re: Server / Client
Post by dfeugey on Jul 2nd, 2015, 4:55pm

With the same receiver :

Code:
      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets

      socket% = FN_tcpconnect(FN_gethostname, "12321")
      IF socket% <=0 ERROR 100, "Failed to open socket"

      REM Don't use FN_writesocket since an error is expected
      msg$ = "hello socket world"+CHR$13+CHR$10
      SYS `send`, socket%, !^msg$, LEN(msg$), 0 TO result%

      msg$ = CHR$13+CHR$10
      SYS `send`, socket%, !^msg$, LEN(msg$), 0 TO result%

      PROC_closesocket(socket%)
      PROC_exitsockets 

Works...

Code:
      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets

      socket% = FN_tcpconnect("127.0.0.1", "12321")
      IF socket% <=0 ERROR 100, "Failed to open socket"

      REM Don't use FN_writesocket since an error is expected
      msg$ = "hello socket world"+CHR$13+CHR$10
      SYS `send`, socket%, !^msg$, LEN(msg$), 0 TO result%

      msg$ = CHR$13+CHR$10
      SYS `send`, socket%, !^msg$, LEN(msg$), 0 TO result%

      PROC_closesocket(socket%)
      PROC_exitsockets
 

Does not.

I'm not sure it's not linked to the fact that the DNS resolves localhost (and not the hosts file) or that my config is IPv6 or that I have a network bridge. Nota: ping localhost, or ping 127.0.0.1 do work.

Conclusion: don't rely on localhost under Windows. Many people seem to have similar problems.
Re: Server / Client
Post by dfeugey on Jul 2nd, 2015, 5:08pm

Quote:
So please correct it (that particular submission is not one of mine)!

It's not mine either. But it's now corrected.
Re: Server / Client
Post by rtr2 on Jul 2nd, 2015, 7:10pm

on Jul 2nd, 2015, 4:55pm, dfeugey wrote:
With the same receiver

Do you literally mean "the same"? You need to change both ends to 'localhost' - you cannot establish a connection between localhost (which doesn't go through your firewall) and your host's actual name (which does). If you have been changing only one end that would explain the 'problem'.

For reference, this is the 'listener' program I have been using to test the Rosetta Code example:

Code:
      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets
      ON CLOSE PROC_exitsockets : QUIT
      ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 0 : PROC_exitsockets : QUIT

      listen% = FN_tcplisten("localhost", "256")
      IF listen%<0 ERROR 100, "Couldn't open listening socket"
      REPEAT
        WAIT 1
        socket% = FN_check_connection(listen%)
      UNTIL socket%
      PRINT "Connection made"

      buffer$ = STRING$(256, CHR$(0))
      REPEAT
        WAIT 1
        result% = FN_readsocket(socket%, !^buffer$, LEN(buffer$) - 1)
        IF result% > 0 PRINT LEFT$(buffer$, result%)
      UNTIL result%=-1
      PRINT "Disconnected"

      PROC_closesocket(socket%)
      PROC_exitsockets
      END 

Richard.

Re: Server / Client
Post by dfeugey on Jul 2nd, 2015, 8:53pm

Ha ha, very good smiley
That was this, yes.

I never had the problem before, since Xojo use DNS, that supports/translates localhost. BBC4Win is definitively more bare metal. Not a bad thing.

This dual way is a very good way to secure accesses... as localhost is local only and gethostbyname a 'network-carded' version. But it will be a problem for a tool that must work both local and distant... Not a big deal, but good to know.

Thanks again for the tip.