Length of readline argument is intenger.
I have to specify the number of characters to read.
For example, if these two files are randomly sent from java,I try to send from java to prolog.
client_send.txt
SWI-prolog
SWI
prolog
Server
Client
client_send2.txt
SWI-prolog
SWI
prolog
Server
ClientSWI-prolog
SWI
prolog
Server
Client
In case only client_send.txt, read_string(In,35,String) is perfect,
But in case sending client_send2.txt, read_string(In,35,String) is not perfect.
It’s reading only half of client_send2.txt.
I want to be able to read all the characters even if the character streams from both files are sent.
Also, even if the file (client_send.txt or client_send2.txt) is sent multiple times in succession,
I want to be able to read everything using Repeat etc.
In other words, I want to read all characters even if the file is sent many times from java after connecting once with tcp.
not json
Read the contents of the file specified by java with Fileinputstream,
It is the flow to send with the stream by executing (getOutputStream.write) to swi-prolog
If you use a free variable in the second argument, it will be unified with the length of the string in the third argument. If you don’t care about the length, you can just use an anonymous variable.
Read at most Length characters from Stream and return them in the string String. If Length is unbound, Stream is read to the end and Length is unified with the number of characters read.
So, if you do read_string(Stream, StringLength, String) you’ll read everything into String and StringLength will unify with the length of that string (assuming StringLength is unbound; otherwise see the next paragraph). You can also do read_string(Stream, _, String) to get the same result (if you want the length, use string_length/2).
If you do read_string(Stream, 1000, String), then at most 1000 characters will be read into String. You can also do MaxRead=1000, read_string(Stream, MaxRead, String), to get the same result.
Sorry if I am not getting your point. It still seems that you are trying to put an integer in the second argument of read_string/3?
The point is, do not put an integer. Put a free variable if you need to know the length of the input, or an anonymous variable if you don’t care about it.
I took your client_send.txt and put it in a file on my computer, looks like this:
$ cat client_send.txt
SWI-prolog
SWI
prolog
Server
Client
Now, I will read this file from standard input and echo it to standard output. I don’t care about the length of the input so I am putting an anonymous variable in the second argument of read_string/3.
$ < client_send.txt swipl -g 'read_string(current_input, _, Str), format("~s", [Str])' -t halt
SWI-prolog
SWI
prolog
Server
Client
In the next example, I only want to write to standard output the length of the input.
$ < client_send.txt swipl -g 'read_string(current_input, N, _), format("~d~n", [N])' -t halt
36
Am I still misunderstanding your question? There is definitely some confusion somewhere.
If you save and execute this code as it is, it will be using an anonymous variable
This way of writing should work.
Somehow it doesn’t work ,in case of stream
?- server(2011).
Nothing is displayed,
Client_side no error.
But forced termination the running client side using ^c,↓ is showing.
?- server(2011).
SWI-prolog
SWI
prolog
Server
Client
I hope representing result whithout forcing termination the running client side using ^c
It is this point that I want you to pay attention to.
When you execute it, 36 will automatically be entered in the anonymous variable.
But In case of instream read_string(In,36,String), ?- server(2011).
It’s no disply of format.
Client_side no error.
I think,Probably Because the prolog is waiting for the 36th character to be sent.
But forced termination the running client side using ^c,↓ is showing.
?- server(2011).
SWI-prolog
SWI
prolog
Server
Client
I feel that the behavior is different when opening and reading a file and when reading from an in-stream.
in case of opening and reading a file ,with read_string(current_input, _, Str)
SWI-prolog
SWI
prolog
Server
Client
It’s perfect.
But in case of reading a instream ,with read_string(In, _, String)
?- server(2011).
Nothing is displayed.
But forced termination the running client side using ^c,↓ is showing.
?- server(2011).
SWI-prolog
SWI
prolog
Server
Client
Because read_line/3 is waiting for the end-of-stream, which it sees only if the client is closed. If you want to see input before end-of-stream, then specify a length, but you have to deal with figuring out how much you want to read – you can do character at a time, if you wish. Or, try read_string/5.
You might want to try using nc -vv localhost 2011 instead of your Java client (might need options -N and -q 1)
In general, network programming is tricky. That’s why it’s best to not use raw sockets but to use something higher level. I haven’t programmed in Java for years, but my understanding is that HTTPClient and HTTPRequest provide similar functionality to JavaScript’s fetch; and on the Prolog side, you can use http_server. (I’m in the process of writing a minimal client-server example, but haven’t finished it yet.)
For testing, utilities such as netcat (ncat, nc) are very useful, because they’ve been used for years and have had their bugs and idiosyncrasies worked out. You can also look at their source code; the code is fairly simple once you get past all the options. (I would also recommend doing your first development on a Linux or Unix system; Windows adds some additional complications.) If you really can’t figure out what’s going on, use Wireshark or tcpdump to look at the packets and flows – it’s quite easy to use and is very instructive.