AmziLS / AmziProlog

Amzi! Prolog: complete system to interpret, build, run and debug Prolog programs, but also embed and query them similar to databases.
Other
117 stars 26 forks source link

Socket connection logic server #7

Closed JoostKlep closed 3 years ago

JoostKlep commented 3 years ago

Hello, I am Joost Klep and I am developing a new program for diagnosis of arithmetical problems in primary education. We installed Amzi-Prolog in Eclipse, and we try to start the sserver and sclient from interfaces/samples/internet/sockets/. Unfortunately we cannot find how to establish the connection.

We try this amzi-prolog code for the server side:

:- loadlsx(asock). :- load(asock).

sdefine('SOCK_STREAM', 1). sdefine('INADDR_ANY', 0).

main :- write( main_0 ), nl, wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1 ), nl, sdefine('SOCK_STREAM', SOCK_STREAM), socket(SOCK_STREAM, 0, Sock), write( main_3: Sock), nl, sdefine('INADDR_ANY', INADDR_ANY),

MyPort = 50530,

writeq( main_4: bind(Sock, INADDR_ANY, MyPort)), nl, bind(Sock, INADDR_ANY, MyPort), write( main_5: Sock), nl, listen(Sock, 1), write( main_6:listening), nl, repeat, select([Sock], [Sock], [Sock], 0, 0, ReadList, WriteList, ErrorList), catch(doreads(ReadList), X, endmain(Sock, X, Quit)), Quit == yes.

endmain(Sock, X, yes) :- closesocket(Sock), wsaCleanup.

doreads([]). doreads([Sock | Rest]) :- accept(Sock, RAddr, RPort, NewSock), send(NewSock, $Hello Client$, 0, NumSent), recv(NewSock, Cmd, 4096, 0, NumRecv), write(Cmd), nl, closesocket(NewSock), (Cmd == $shutdown$ -> write($Shutting down server ...\n$), throw(shutdown) ; true ), doreads(Rest).

And for the client side we use:

% NOTE!!! Uncomment the appropriate sdefine for your environment % Linux & Windows :- load( asock).

sdefine('SOCK_STREAM', 1). % Solaris %sdefine('SOCK_STREAM', 2).

main :- wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1), nl, inet_addr($my personal ip addr$, NetAddr), write( main_2:NetAddr), nl, sdefine('SOCK_STREAM', SOCK_STREAM), write( main_3:SOCK_STREAM), nl, socket(SOCK_STREAM, 0, Sock), write( main_4:Sock), nl, connect(Sock, NetAddr, 50501), write($Send what? $), read_string(Str), send(Sock, Str, 0, NumSent), recv(Sock, Buf, 4096, 0, NumRecv), write(Buf), closesocket(Sock), wsaCleanup.

We hope anyone can help us, maybe by giving another example? Question: are there any modules not working propery in windows 10?

Joost

DennisMerritt commented 3 years ago

Hi Joost,

I hope someone can help you with this, there are a few people that have gotten into the code...

Hope all is well with you, —Dennis

Sent from my iPad

On Apr 27, 2021, at 8:43 AM, JoostKlep @.***> wrote:

 Hello, I am Joost Klep and I am developing a new program for diagnosis of arithmetical problems in primary education. We installed Amzi-Prolog in Eclipse, and we try to start the sserver and sclient from interfaces/samples/internet/sockets/. Unfortunately we cannot find how to establish the connection.

We try this amzi-prolog code for the server side:

:- loadlsx(asock). :- load(asock).

sdefine('SOCK_STREAM', 1). sdefine('INADDR_ANY', 0).

main :- write( main_0 ), nl, wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1 ), nl, sdefine('SOCK_STREAM', SOCK_STREAM), socket(SOCK_STREAM, 0, Sock), write( main_3: Sock), nl, sdefine('INADDR_ANY', INADDR_ANY),

MyPort = 50530,

writeq( main_4: bind(Sock, INADDR_ANY, MyPort)), nl, bind(Sock, INADDR_ANY, MyPort), write( main_5: Sock), nl, listen(Sock, 1), write( main_6:listening), nl, repeat, select([Sock], [Sock], [Sock], 0, 0, ReadList, WriteList, ErrorList), catch(doreads(ReadList), X, endmain(Sock, X, Quit)), Quit == yes.

endmain(Sock, X, yes) :- closesocket(Sock), wsaCleanup.

doreads([]). doreads([Sock | Rest]) :- accept(Sock, RAddr, RPort, NewSock), send(NewSock, $Hello Client$, 0, NumSent), recv(NewSock, Cmd, 4096, 0, NumRecv), write(Cmd), nl, closesocket(NewSock), (Cmd == $shutdown$ -> write($Shutting down server ...\n$), throw(shutdown) ; true ), doreads(Rest).

And for the client side we use:

% NOTE!!! Uncomment the appropriate sdefine for your environment % Linux & Windows :- load( asock).

sdefine('SOCK_STREAM', 1). % Solaris %sdefine('SOCK_STREAM', 2).

main :- wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1), nl, inet_addr($my personal ip addr$, NetAddr), write( main_2:NetAddr), nl, sdefine('SOCK_STREAM', SOCK_STREAM), write( main_3:SOCK_STREAM), nl, socket(SOCK_STREAM, 0, Sock), write( main_4:Sock), nl, connect(Sock, NetAddr, 50501), write($Send what? $), read_string(Str), send(Sock, Str, 0, NumSent), recv(Sock, Buf, 4096, 0, NumRecv), write(Buf), closesocket(Sock), wsaCleanup.

We hope anyone can help us, maybe by giving another example? Question: are there any modules not working propery in windows 10?

Joost

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

JoostKlep commented 3 years ago

Hallo Dennis, how are you? I am happy to hear from you. So every now and then I read parts of your personal site. I am working on my old arithmeticus project. One of my sons, Senastiaan is helping me with the business part. That is a problem for me, I am not a communication and commercial person. I am very happy with your amzi-prolog, the site, your educational philosophy, and your customer approach. Most of all I like the clean and clear structure of your product. I hope you are in a good health! We are open for all professional and commercial sparring and I am happy to hear from you. At the moment I am living in Gouda (Netherlands). Joost

Op wo 28 apr. 2021 14:01 schreef Dennis Merritt @.***>:

Hi Joost,

I hope someone can help you with this, there are a few people that have gotten into the code...

Hope all is well with you, —Dennis

Sent from my iPad

On Apr 27, 2021, at 8:43 AM, JoostKlep @.***> wrote:

 Hello, I am Joost Klep and I am developing a new program for diagnosis of arithmetical problems in primary education. We installed Amzi-Prolog in Eclipse, and we try to start the sserver and sclient from interfaces/samples/internet/sockets/. Unfortunately we cannot find how to establish the connection.

We try this amzi-prolog code for the server side:

:- loadlsx(asock). :- load(asock).

sdefine('SOCK_STREAM', 1). sdefine('INADDR_ANY', 0).

main :- write( main_0 ), nl, wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1 ), nl, sdefine('SOCK_STREAM', SOCK_STREAM), socket(SOCK_STREAM, 0, Sock), write( main_3: Sock), nl, sdefine('INADDR_ANY', INADDR_ANY),

MyPort = 50530,

writeq( main_4: bind(Sock, INADDR_ANY, MyPort)), nl, bind(Sock, INADDR_ANY, MyPort), write( main_5: Sock), nl, listen(Sock, 1), write( main_6:listening), nl, repeat, select([Sock], [Sock], [Sock], 0, 0, ReadList, WriteList, ErrorList), catch(doreads(ReadList), X, endmain(Sock, X, Quit)), Quit == yes.

endmain(Sock, X, yes) :- closesocket(Sock), wsaCleanup.

doreads([]). doreads([Sock | Rest]) :- accept(Sock, RAddr, RPort, NewSock), send(NewSock, $Hello Client$, 0, NumSent), recv(NewSock, Cmd, 4096, 0, NumRecv), write(Cmd), nl, closesocket(NewSock), (Cmd == $shutdown$ -> write($Shutting down server ...\n$), throw(shutdown) ; true ), doreads(Rest).

And for the client side we use:

% NOTE!!! Uncomment the appropriate sdefine for your environment % Linux & Windows :- load( asock).

sdefine('SOCK_STREAM', 1). % Solaris %sdefine('SOCK_STREAM', 2).

main :- wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1), nl, inet_addr($my personal ip addr$, NetAddr), write( main_2:NetAddr), nl, sdefine('SOCK_STREAM', SOCK_STREAM), write( main_3:SOCK_STREAM), nl, socket(SOCK_STREAM, 0, Sock), write( main_4:Sock), nl, connect(Sock, NetAddr, 50501), write($Send what? $), read_string(Str), send(Sock, Str, 0, NumSent), recv(Sock, Buf, 4096, 0, NumRecv), write(Buf), closesocket(Sock), wsaCleanup.

We hope anyone can help us, maybe by giving another example? Question: are there any modules not working propery in windows 10?

Joost

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AmziLS/interfaces/issues/9#issuecomment-828396762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGFFLYX6O4IJE6ZD2RTQYE3TK72JJANCNFSM43U5MTYQ .

DennisMerritt commented 3 years ago

Hi Joost,

I’m doing well in my retirement. Covid-19 lockdown was kind of depressing. It’s strange, Nancy, my wife (Mary died a number of years ago) and I both are financially OK, have a nice house, etc. etc. No serious impact on our lives, but still, not seeing grandkids, not sociallizing with our friends, all puts a depressing cloud over life.

Other than that, well there’s our politics which was pretty depressing as well, but other than all that dark and gloomy stuff, life is OK. I’m writing about ukulele chords instead of Prolog, trying to learn to play guitar, growing vegetables like old people do, writing letters to the editor of our local paper, like that. And did get to see the grandkids after a year’s isolation. That was fun.

On money, Amzi! didn't made us rich but did well enough that we didn’t have to get real jobs. One of the real pleasures of the business was our customers all over the world. Prolog was way more popular outside of the United States and the people who were drawn to Prolog all seemed to be independent, open minded sorts of thinkers.

So it is a pleasure for me to see your name pop up now and again, even if it’s been a number of years since I actually looked at any code.

Best wishes, —Dennis

On Apr 29, 2021, at 2:40 AM, JoostKlep @.***> wrote:

Hallo Dennis, how are you? I am happy to hear from you. So every now and then I read parts of your personal site. I am working on my old arithmeticus project. One of my sons, Senastiaan is helping me with the business part. That is a problem for me, I am not a communication and commercial person. I am very happy with your amzi-prolog, the site, your educational philosophy, and your customer approach. Most of all I like the clean and clear structure of your product. I hope you are in a good health! We are open for all professional and commercial sparring and I am happy to hear from you. At the moment I am living in Gouda (Netherlands). Joost

Op wo 28 apr. 2021 14:01 schreef Dennis Merritt @.***>:

Hi Joost,

I hope someone can help you with this, there are a few people that have gotten into the code...

Hope all is well with you, —Dennis

Sent from my iPad

On Apr 27, 2021, at 8:43 AM, JoostKlep @.***> wrote:

 Hello, I am Joost Klep and I am developing a new program for diagnosis of arithmetical problems in primary education. We installed Amzi-Prolog in Eclipse, and we try to start the sserver and sclient from interfaces/samples/internet/sockets/. Unfortunately we cannot find how to establish the connection.

We try this amzi-prolog code for the server side:

:- loadlsx(asock). :- load(asock).

sdefine('SOCK_STREAM', 1). sdefine('INADDR_ANY', 0).

main :- write( main_0 ), nl, wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1 ), nl, sdefine('SOCK_STREAM', SOCK_STREAM), socket(SOCK_STREAM, 0, Sock), write( main_3: Sock), nl, sdefine('INADDR_ANY', INADDR_ANY),

MyPort = 50530,

writeq( main_4: bind(Sock, INADDR_ANY, MyPort)), nl, bind(Sock, INADDR_ANY, MyPort), write( main_5: Sock), nl, listen(Sock, 1), write( main_6:listening), nl, repeat, select([Sock], [Sock], [Sock], 0, 0, ReadList, WriteList, ErrorList), catch(doreads(ReadList), X, endmain(Sock, X, Quit)), Quit == yes.

endmain(Sock, X, yes) :- closesocket(Sock), wsaCleanup.

doreads([]). doreads([Sock | Rest]) :- accept(Sock, RAddr, RPort, NewSock), send(NewSock, $Hello Client$, 0, NumSent), recv(NewSock, Cmd, 4096, 0, NumRecv), write(Cmd), nl, closesocket(NewSock), (Cmd == $shutdown$ -> write($Shutting down server ...\n$), throw(shutdown) ; true ), doreads(Rest).

And for the client side we use:

% NOTE!!! Uncomment the appropriate sdefine for your environment % Linux & Windows :- load( asock).

sdefine('SOCK_STREAM', 1). % Solaris %sdefine('SOCK_STREAM', 2).

main :- wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( main_1), nl, inet_addr($my personal ip addr$, NetAddr), write( main_2:NetAddr), nl, sdefine('SOCK_STREAM', SOCK_STREAM), write( main_3:SOCK_STREAM), nl, socket(SOCK_STREAM, 0, Sock), write( main_4:Sock), nl, connect(Sock, NetAddr, 50501), write($Send what? $), read_string(Str), send(Sock, Str, 0, NumSent), recv(Sock, Buf, 4096, 0, NumRecv), write(Buf), closesocket(Sock), wsaCleanup.

We hope anyone can help us, maybe by giving another example? Question: are there any modules not working propery in windows 10?

Joost

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <https://github.com/AmziLS/interfaces/issues/9#issuecomment-828396762 https://github.com/AmziLS/interfaces/issues/9#issuecomment-828396762>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AGFFLYX6O4IJE6ZD2RTQYE3TK72JJANCNFSM43U5MTYQ https://github.com/notifications/unsubscribe-auth/AGFFLYX6O4IJE6ZD2RTQYE3TK72JJANCNFSM43U5MTYQ> .

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/AmziLS/interfaces/issues/9#issuecomment-828978455, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADS7FIWQE6VWTU63HRFXKNLTLD5MPANCNFSM43U5MTYQ.

maelh commented 3 years ago

The https://github.com/AmziLS/AmziProlog/commit/a6b3d9255aeeeb2dbe3af8b5699703fe0d0e5033 commit should fix the issue. At least the socket examples work again on my system.

There were various pointer and integer truncation issues, and the finger example would also not work since there are almost no servers that support this protocol today.

Fixed the documentation to adapt to this (with suggested test servers), and fixed the issues.

Let me know if it works.

Note: you have to clone the latest master and compile, the release ZIP does not yet contain these fixes.

maelh commented 3 years ago

Latest commit fixes all remaining potential type issues.

JoostKlep commented 2 months ago

Dear Maelh We were able to use the amzi-socket in Win10-64 on our server with only one socket connection at a time. The code is following the amzi socket example:

s_current_port( 50531). % not amzi!, but useful in our application

sdefine( 'SOCK_STREAM', 1 ). sdefine( 'INADDR_ANY', 0 ).

main:- wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg), write( am_ss: main1: 1: b: wsaStartup(0x0101, Ver, HighVer, Desc, Status, MaxSockets, MaxUdpDg) ), nl, sdefine('SOCK_STREAM', SOCK_STREAM), socket(SOCK_STREAM, 0, Sock), sdefine('INADDR_ANY', INADDR_ANY), s_current_port( MainPort ), bind(Sock, INADDR_ANY, MainPort), listen(Sock, 1), write( listening: port: MainPort ), nl, write( socket: Sock ), nl, ... . The output is: am_ss:main1:1:b:wsaStartup(257, 257, 514, WinSock 2.0, Running, 32767, 65467) listening:port:50531 socket:0x000000000000028C

In case of this single server (port 50531) this socket runs good, But: CPU usage is very high (> 85%) until there is a connection with a client. Once there is a connection the CPU usage decreases (< 15%) .

Now we start 3 instances ( .xpl) of this application, each of them with its own portnumber: s_current_port( 50531 ), s_current_port( 60531 ) and s_current_port( 70531).

The output is:

am_ss:main1:1:b:wsaStartup(257, 257, 514, WinSock 2.0, Running, 32767, 65467) listening:port:50531 socket:0x000000000000028C

am_ss:main1:1:b:wsaStartup(257, 257, 514, WinSock 2.0, Running, 32767, 65467) listening:port:60531 socket:0x000000000000028C

am_ss:main1:1:b:wsaStartup(257, 257, 514, WinSock 2.0, Running, 32767, 65467) listening:port:70531 socket:0x000000000000028C

The three together are very slow, they share the 100% CPU.

Strange is that all socket_numbers are 0x000000000000028C.

It seems there is a problem in sockets.c.

Can you please help us?

Greetings, Joost Klep .

On Tue, 18 May 2021 at 00:11, maelh @.***> wrote:

The a6b3d92 https://github.com/AmziLS/AmziProlog/commit/a6b3d9255aeeeb2dbe3af8b5699703fe0d0e5033 commit should fix the issue. At least the socket examples work again on my system.

There were various pointer and integer truncation issues, and the finger example would also not work since there are almost no servers that support this protocol today.

Fixed the documentation to adapt to this (with suggested test servers), and fixed the issues.

Let me know if it works.

Note: you have to clone the latest master and compile, the release ZIP does not yet contain these fixes.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AmziLS/AmziProlog/issues/7#issuecomment-842675412, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGFFLYWIOMBHGQI3E2M4QXDTOGH73ANCNFSM44PTZPNQ .

-- Joost Klep

Emeritus hoogleraar Didactiek van Rekenen en Wiskunde

Geerpolderweg 29 2807 LS Gouda

Telefoon Gouda: 0031 (0)182 78 70 62 Mobiel Nederland: 0031 (0) 6 137 25 906