How can i send a file with server.write?

hi,i want to try to host a website with processing but i can’t send file

import processing.net.*;
Server myServer;
PrintWriter test;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, 5204);
  println("ip: " + Server.ip() + ":5204");
  test = createWriter("untest.html");
  test.println("<html><head></head><body>lolol</body></html>");
  test.close();
}

void draw() {
  Client client = myServer.available();
  if(client != null)
  {
    client.write(test);
  }
}

You could use loadStrings() to convert the file into an array of strings, combine all the parts of the array into one String, and then send that string.

first i try like

import processing.net.*;
Server myServer;
int port = 5204;
//PrintWriter test;

void setup() {
  size(200, 200);
  myServer = new Server(this, port);
  println("ip: " + Server.ip() + ":"+port);
//  test = createWriter("/data/index.html");
//  test.println("<html><head></head><body>lolol</body></html>");
//  test.close();
}

void draw() {
  Client client = myServer.available();
  if(client != null)
  {
    client.write(":-)");
    myServer.disconnect(client);
  }
}

and “inspect” the result,
as all i write is wrapped in a html page and inside

<pre>:-)</pre>

i could not send any further “html code”

i can’t open it with chrome

I know that works if you just use processing, but i don’t know how you would do that with chrome.

here same,
firefox works, chrome not

how can i send uft-8 string?

i put “println(client.read());”
and i find this

GET / HTTP/1.1
Host: 192.168.0.20:5204
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7

wow now it’s a blank pagescreen

yay this code work :slight_smile:

import processing.net.*;
Server myServer;
PrintWriter test;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, 5204);
  println("ip: " + Server.ip() + ":5204");
}

void draw() {
  Client client = myServer.available();
  if(client != null)
  {
    println(client.readString());
    println("-------------------------------");
    println(client.ip());
    client.write("HTTP/1.1 200 OK\r\n");
    client.write("Content-Length: 124\r\n");
    client.write("Content-Type: text/html\r\n\r\n");
    client.write("<html><head><title>a title</title></head><body><h2>Hi!</h2></body></html>");
    client.stop();
  }
}

should not the 124 in

client.write("Content-Length: 124\r\n");

match the chars in

client.write("<html><head><title>a title</title></head><body><h2>Hi!</h2></body></html>");

? CHROME / inspect / console / ERROR

so i use this
not only ( make and ) send a /data/index.html file,
also can send a /data/favicon.ico file