Network Library Questions
in
Core Library Questions
•
3 years ago
The Reference for some of the Network library methods (i.e. available(), read(), etc) refer to a "buffer" - I do not understand what this is...and I believe my ignorance of it may be leading to the trouble I am having with my code that follows.
I have written a server and client whiteboard program, and I keep getting "nullpointexceptions" after the program runs fine for a while - it appears almost random when the program fails, sometimes it happens to the server, sometimes it happens to a client(s). I open the server first, then I run the clients. the clients are duplicates,I just saved them under different names.
This is the Server:
I have highlighted the line that gets the "nullpointerexception"
- //EXC 19-4 SERVER white board with colors
- //import net library
- import processing.net.*;
- //Declare a server and client object
- Server server;
- //tracks the number of clients connected
- int clientCount = 0;
- String incomingMessage = "";
- PFont f;
- void setup(){
- size(300, 200);
- background(255,0,0);
- //initialze server
- server = new Server(this, 5204);
- f = createFont("Chalkduster-48.vlw", 20, true);
- incomingMessage = "";
- }//close setup
- void draw(){
- background(255);
- Client client = server.available();
- if(client != null){
- incomingMessage = client.readStringUntil('*');
- server.write(incomingMessage);
- //println(incomingMessage);
- }//close if
- textFont(f);
- textAlign(CENTER);
- fill(0);
- text(incomingMessage, width/2, height/2 + 50);
- }//close draw
- void serverEvent(Server server, Client client){
- //incomingMessage = "A new client has connected" + client.ip();
- println("A new client has connected at IP: " + client.ip());
- //server.write(clientCount);
- clientCount += 1;
- }//close serverEvent
This is the client...
I have highlighted the line that gets the "nullpointerexception"
- //EXC 19-4 client white board with colors
- import processing.net.*;
- Client client;
- color r;
- color g;
- color b;
- color lineColor;
- void setup(){
- lineColor = color(0);
- frame.setTitle("My super-awesome application!!!");
- size(200, 200);
- background(255);
- client = new Client(this, "127.0.0.1", 5204);
- r = color(255,0,0);
- g = color(0,255,0);
- b = color(0,0,255);
- }//close setup
- void draw(){
- if(client.available() > 10){
- String recieve = client.readStringUntil('*');
- println("receiving data = " + recieve);
- int[] values = int( splitTokens( recieve, ",*" ) );
- //render ellipses
- noStroke();
- fill(values[2]);
- ellipse(values[0], values[1], 10, 10);
- }//close if
- }//
- void mouseDragged(){
- String send = mouseX + "," + mouseY + "," + lineColor + "*";
- client.write(send);
- println("sending data = " + send);
- }//close if
- void keyPressed(){
- if(key == 'c'){
- background(255);
- }
- else if(key == 'r'){
- lineColor = r;
- } else if(key == 'g'){
- lineColor = g;
- } else if(key == 'b'){
- lineColor = b;
- }//close if
- }//close mousepressed
1