Writing server-output to file
in
Core Library Questions
•
1 year ago
Hi,
I've modified the chatserver-example to write incoming data to a textfile but as easy as that sounds it doesn't really return anything. The weird thing is that the incoming messages are displayed in the draw-window as "text"-objects but i can't get the strings to print in either the outputfile or the console, it always seems to returns null. Any clue?
Thanks,
FRid
The client
The server:
I've modified the chatserver-example to write incoming data to a textfile but as easy as that sounds it doesn't really return anything. The weird thing is that the incoming messages are displayed in the draw-window as "text"-objects but i can't get the strings to print in either the outputfile or the console, it always seems to returns null. Any clue?
Thanks,
FRid
The client
- import processing.net.*;
Client c;
String data;
void setup() {
size(200, 200);
background(50);
fill(200);
c = new Client(this, "127.0.0.1", 8888); // Connect to server on port 80
c.write("GET http://localhost:8888/foursquare?code=CODE/ HTTP/1.1\n"); // Use the HTTP "GET" command to ask for a Web page
c.write("Hello, anybody"); // Be polite and say who we are
c.write("out there?");
}
void draw() {
if (c.available() > 0) { // If there's incoming data from the client...
data = c.readString(); // ...then grab it and print it
//println(data);
}
}
The server:
- import processing.net.*;
int port = 8888;
boolean myServerRunning = true;
int bgColor = 0;
int direction = 1;
int textLine = 60;
Server myServer;
ArrayList strings;
PrintWriter output;
void setup()
{
size(1000, 500);
textFont(createFont("SanSerif", 16));
strings = new ArrayList();
myServer = new Server(this, port); // Starts a myServer on port 10002
output = createWriter("output.txt");
background(0);
}
void mousePressed()
{
// If the mouse clicked the myServer stops
for (int i=0;i<strings.size();i++) {
String s = (String) strings.get(i);
output.println(s);
}
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
myServer.stop();
myServerRunning = false;
exit(); // Stops the program
}
void draw()
{
if (myServerRunning == true)
{
text("server", 15, 45);
Client thisClient = myServer.available();
if (thisClient != null) {
if (thisClient.available() > 0) {
text("mesage from: " + thisClient.ip() + " : " + thisClient.readString(), 15, textLine);
String temp = thisClient.readString();
strings.add(temp);
println(thisClient.readString());
textLine = textLine + 35;
}
}
}
else
{
text("server", 15, 45);
text("stopped", 15, 65);
}
}
1