11
$\begingroup$

I am trying to open connection with socket. I am using this:

Needs["SocketLink`"];
port = 8888;
socket = CreateServerSocket[port];
{in, out} = OpenSocketStreams[socket];

If I am writing to output stream then the message is printed:

BynaryWrite[out, {1, 1, 1, 1, 1, 1, 1}]

BinaryWrite::errfile: "Could not access file "File write failed"".

Also, If I am trying to read data from input stream then the mathematica kernel stops.

How can I read and write data to streams, but not use the function CreateAsynchronousServer?. How can I wait for a request to the server? I want to use for solving this problem ScheduledTasks.

I want to create a simple http-server. I know how create a server using CreateAsynchronousServer. For example:

CreateAsynchronousServer[CreateServerSocket[8888], Handler]

Handler[{in_InputStream, out_OutputStream}] :=
    Module[{responsestring = "HTTP/1.0 200 OK\r\nContent-Length: 4\r\n\r\ntest"},
        While[True, TimeConstrained[BinaryRead[in], 0.01, (Close[in]; Break[])]];
        BinaryWrite[out, ToCharacterCode[responsestring]];
        Close[out];
    ];

Result is a server that is handles the request from http://localhost:8888 using the function Handler. Within handler I can read and write data to output stream related with a socket. But I don't know how to do it without using CreateAsynchronousServer.

$\endgroup$
  • $\begingroup$ Can you discuss a bit abut what you want to do with this functionality? $\endgroup$ – Jagra Jul 21 '16 at 22:12
  • $\begingroup$ Yes! I updated my question. $\endgroup$ – Kirill Belov Jul 22 '16 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.