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

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]];

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.

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.