Need help with chat-server!!!

edited February 2015 in Questions about Code
         The problem is here under draw :                FULL CODE IS BELOW!!!
         if (serverOn = true) {
         text(myText, 15, 550, width, height);
         Client thisClient = myServer.available();
         if (thisClient != null) {
         if (thisClient.available() > 0) {
         if (msgCount == 13) {
         textLine = 60;
         background(0, 0, 0);
         msgCount = 0;
         }
         fill(0, 200, 0);
         myServer.write(""+thisClient.readString()); <----- When i resend the message to all connected clients it says NULL
         instead of the message!

         text(thisClient.readString(), 15, textLine);
         msgCount++;
         textLine = textLine + 35;

`

      import processing.net.*; 

      Server myServer; 

      Client myClient;
      int Z;
      int textLine = 60;
      boolean serverOn;
      PrintWriter output;
      int year = year();
      int month = month();
      int day = day();
      String y = ""+year;
      String m = ""+month;
      String d = ""+day;
      int msgCount = 0;
      String myText = "";
      boolean debug = true;
      int X, Y;

      void setup() {
      background(0, 0, 0);
      myServer = new Server(this, 5204); 
      //Initialize server
      size(300, 620);
      if (frame != null) {
      frame.setResizable(true);
      }
      }
     void draw() {
     if (debug == true) {
     println(mouseX, mouseY);
     }
     if (serverOn = true) {
     text(myText, 15, 550, width, height);
     Client thisClient = myServer.available();
      if (thisClient != null) {
       if (thisClient.available() > 0) {
         if (msgCount == 13) {
         textLine = 60;
          background(0, 0, 0);
          msgCount = 0;
          }
         fill(0, 200, 0);
        myServer.write(""+thisClient.readStringUntil(1));
        text(thisClient.readString(), 15, textLine);
        msgCount++;
        textLine = textLine + 35;
       }
       }
      } else {
      text("server", 15, 45);
      text("stopped", 15, 65);
     }
     }
     void keyPressed() {
     if (keyCode == BACKSPACE) {
     if (myText.length() > 0) {
    myText = myText.substring(0, myText.length()-1);
    }
    } else if (keyCode == DELETE) {
    myText = "";
   } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
  myText = myText + key;
  } else if (keyCode == ALT) {
 if (myText != "" || myText != " "){
   myServer.write("Server"+" : "+myText); 
 if (msgCount == 13) {
       textLine = 60;
       background(0, 0, 0);
       msgCount = 0;
 }
 text("Server"+" : "+myText, 15, textLine);
 msgCount++;
 textLine = textLine + 35;
 }
 myText = "                                                                                                                                                             ";
 fill(200, 0, 0);
 rect(0, 550, width, height);
 fill(0, 200, 0);
 myText = "";
 }
 }`

Answers

  • Answer ✓

    @oscar124==

    a) println(mouseX, mouseY) means nothing b) why using readStringUntil(1), and not (10) (linefeed ascii table) c) why not using only readString() and creating a var in order to avoid the null?

    try to change your code line 45 with this piece::

          String mess = thisClient.readString();
                text(mess, 15, textLine);
    
               //myServer.write(""+thisClient.readStringUntil(10));
    
               myServer.write(""+mess);
               msgCount++;
    
               textLine = textLine + 35;
    
  • @akenaton

    Thanks! that helped!! I guess i left trails from other thoughts i had erlier but trashed. Would you like to join me in developing this program? You will have full access to all files and i will be hosting the server.

  • @oscar124::

    i have my own job, so helping depends of your project, of its aims & specific technical difficulties; can you explain a little more?

  • @akenaton I made a discussion for people to join. Its all in there!

  • First line I see:

    if (serverOn = true)
    

    is a very common error: you assign true to serverOn, they you test it (it is always true...).
    As I always say, just use the booleans for what they are made, it avoids errors and it reads better:

    if (serverOn)
    
Sign In or Register to comment.