Author |
Topic: Server / Client (Read 397 times) |
|
dfeugey
Guest
|
 |
Re: Server / Client
« Reply #6 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 
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).
|
| « Last Edit: Jul 1st, 2015, 11:23pm by dfeugey » |
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Server / Client
« Reply #7 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.
|
|
Logged
|
|
|
|
dfeugey
Guest
|
 |
Re: Server / Client
« Reply #8 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.
|
| « Last Edit: Jul 2nd, 2015, 06:13am by dfeugey » |
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Server / Client
« Reply #9 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.
|
|
Logged
|
|
|
|
dfeugey
Guest
|
 |
Re: Server / Client
« Reply #10 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.
|
| « Last Edit: Jul 2nd, 2015, 5:01pm by dfeugey » |
Logged
|
|
|
|
dfeugey
Guest
|
 |
Re: Server / Client
« Reply #11 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.
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Server / Client
« Reply #12 on: Jul 2nd, 2015, 7:10pm » |
|
on Jul 2nd, 2015, 4:55pm, dfeugey wrote: 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.
|
|
Logged
|
|
|
|
dfeugey
Guest
|
 |
Re: Server / Client
« Reply #13 on: Jul 2nd, 2015, 8:53pm » |
|
Ha ha, very good  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.
|
| « Last Edit: Jul 2nd, 2015, 8:56pm by dfeugey » |
Logged
|
|
|
|
|