Help client-server

edited November 2013 in Questions about Code

I have a problem with this program. I have to make a connection between server and client. The client, once connected, enter nickname and choose a color, then press the button and I have to save it in a variable (in need) the nickname inserted, but does not work (the name is written on the screen and not saved). How do I?

import processing.net.*;

// Declare a server
Server server;

// Used to indicate a new message has arrived
float newMessageColor = 255;
PFont f;
String incomingMessage = "";
float x = random(0,400);
float y = random(0,200);
String nome;
color c=color(0,0,0);
Utente [] utente;

void setup() {
  size(400,200);  
  // Create the Server on port 5204
  server = new Server(this, 5204); 
  f = createFont("Arial",16,true);
  utente = new Utente [10];
  for(int i = 9; i>=0; i--){
    utente[i] = new Utente(nome,c);
    println("Colore client 0"+utente[i].colore);
    println("Nome client 0"+utente[i].nome);
  }
}

void draw() {
  background(newMessageColor);
  textFont(f);
  textAlign(CENTER);
  fill(255);

  // The most recent incoming message is displayed in the window.
  text(incomingMessage,width/2,height/2); 
  // If a client is available, we will find out
  // If there is no client, it will be"null"
  Client client = server.available();

  // We should only proceed if the client is not null
  if (client!= null) {

    // Receive the message
    // The message is read using readString().
    incomingMessage = client.readString(); 
    // The trim() function is used to remove the extra line break that comes in with the message.
    incomingMessage = incomingMessage.trim();

    // Print to Processing message window
    println( "Client says:" + incomingMessage);

    // Write message back out (note this goes to ALL clients)
    server.write( "How does " + incomingMessage + " make you feel?\n" ); // A reply is sent using write().

    // Reset newMessageColor to black
    newMessageColor = 0;
  }
}

// The serverEvent function is called whenever a new client connects.
void serverEvent(Server server, Client client) {
  incomingMessage = "A new client has connected: " + client.ip();
  println(incomingMessage);
  // Reset newMessageColor to black
  newMessageColor = 0;
}

Client

import processing.net.*;
// Dichiaro un client
Client client;
//
// Stati
final int statoMenu = 0;
final int statoColore = 1;
final int intermedio = 2;
final int play = 3;
int stato;
//----------------------------------

String nome = " ";
PFont font;
color c = color(0, 0, 0);


void setup() {
  size(700, 500);
  client = new Client(this,"127.0.0.1", 5204);
  font = createFont("Helvetica", 18); 

}

void draw() {
    background(255);
    textFont(font,18);
    fill(255,0,0);
    // this adds a blinking cursor after your text, at the expense of redrawing everything every frame
    if (stato == statoMenu || stato == statoColore || stato == intermedio){
      text("Nickname:",40,40);
      text(nome +(frameCount/10 % 2 == 0 ? "_" : ""), 125, 40);
    }
    if (stato == statoColore || stato == intermedio){
      colore();
    }
    if (stato == intermedio){
      rect(100, 100, 50, 20);
    }


}

void keyPressed() {
  switch(stato){
  case statoMenu:
    keyPressedMenu();
    break;
  }
}

void keyPressedMenu(){  
  if (key != CODED) {
    switch(key) {
    case BACKSPACE:
      nome = nome.substring(0,max(0,nome.length()-1));
      stato = statoMenu;
      break;
    case TAB:
      nome += "    ";
      break;
    case ENTER:
      stato=statoColore;
      break;
    /*case RETURN:
      // comment out the following two lines to disable line-breaks
      typedText += "\n";*/
    case ESC:
      exit();
      break;
    default:
      nome += key;
  }
}
}
void colore(){
  //red
  fill(255, 0, 0);
  rect(50, 50, 20, 20);
  //pink
  fill(255, 0, 255);
  rect(90, 50, 20, 20);
  //purple
  fill(130, 0, 255);
  rect(140, 50, 20, 20);
}
void mousePressed(){
 if (stato == statoColore){
  if(mouseX > 50 && mouseX < 70 && mouseY > 50 && mouseY < 70){
    c = color(255, 0, 0);
    stato = intermedio;
  }
  if(mouseX > 90  && mouseX < 110 && mouseY > 50 && mouseY <70){
    c = color(255, 0, 255);
    stato = intermedio;
  }
  if(mouseX > 130 && mouseX < 160 && mouseY > 50 && mouseY <70){
     c = color(130, 0, 255);
     stato = intermedio;
  }
 }
 if (stato == intermedio){
  if(mouseX > 100 && mouseX < 150 && mouseY > 100 && mouseY < 120){
     stato = play;   
    client.write(nome+c);
    }
  }

}

Answers

  • Answer ✓

    Moved because "Using Processing" is about questions about the PDE and the installation.

    "does not work (the name is written on the screen and not saved)"
    On which side? What do you mean by "save in a variable"? Which variable?

  • my goal is to save your Nickname in a variable. The problem is that the client needs to go to the server name (nickname) but do not know how to do

  • edited November 2013

    double-clicking on the client I run the client opens two windows, but on the server recognizes them as if they were the same client and not two separate, why?: (

    //SERVER
    import processing.net.*;
    
    // dichiaro un server
    Server server;
    //-------------Stati--------------------------
    final int statoAttesa = 0;
    final int play = 1;
    final int passaTesoro = 2;
    int stato;
    //--------------------------------------------
    
    // Used to indicate a new message has arrived
    float newMessageColor = 255;
    PFont f;
    String incomingMessage = "";
    String m = "";
    float x = random(0,599);
    float y = random(0,499);
    String nome;
    color c = color(0,0,0);
    color background = color(40, 130, 160);
    Utente [] utente;
    String [] valore;
    String [] nomeClient;
    int [] colore;
    int NClient=0;
    
    //------Variabili per il gioco------
    Posizione pedina, X;
    int random_x,random_y;
    int target;
    //---------------------------------
    
    void setup() {
      size(600,500);
      // Create the Server on port 5204
      server = new Server(this, 5204); 
      f = createFont("Arial",16,true);
      utente = new Utente [10];
      //--------------Setup Gioco--------------
      pedina = new Posizione(x,y, 20);
      random_x = int(random(1,599));
      random_y = int(random(1,499));
      X = new Posizione(random_x,random_y);
      //---------------------------------------
      stato = statoAttesa;
    }
    
    void draw() {
      background(newMessageColor);
      textFont(f);
      textAlign(CENTER);
      fill(255);
    
      // If a client is available, we will find out
      // If there is no client, it will be"null"
      Client client = server.available();
    
      //-----Acquisizione input dell'utente-------
      if (client!= null && stato == statoAttesa) {   
        // Receive the message
        // The message is read using readString().
    
      println(X.x + " "+ X.y);
        incomingMessage = client.readString(); 
        // The trim() function is used to remove the extra line break that comes in with the message.
        incomingMessage = incomingMessage.trim();
        String nome = incomingMessage;
        //inizializzo la variabile nomeClient ad un valore abbastanza grosso
        //supposto di 100 massimi client
        nomeClient = new String [100];
        //inizializzo la variabile colore ad un valore abbastanza grosso
        //supposto di 100 massimi client
        colore = new int [500];
        println("I client sono" + " " + NClient);
        for(int i=0; i < NClient;i++){
          valore = (split(nome,"-"));
          nomeClient [i] = valore[0];
          println(nomeClient[i]);
          //colore [i] = int(valore[1]);
         // println(colore[i]);
         // utente [i]= new Utente(nomeClient[i], colore[i]);
          if(colore[i] == 65536) {
            c = color(255,0,0);
          }
          if(colore[i] == 65281){
            c = color(255, 0, 255);
          }
          if(colore[i] == 8257281){
            c = color(130,0,255);
          } 
        }
        stato = play;
      }
        for(int i=0; i<NClient; i++) {
             server.write(pedina.x + " " + pedina.y + " " + pedina.r + " " + pedina.r + " " + NClient + " *");
             fill(c);
             ellipse(pedina.x, pedina.y, pedina.r, pedina.r);
       }
       if(pedina.x >= X.x-10 && pedina.x <= X.x +10 && pedina.y >= X.y-10 && pedina.y <= X.y + 10) {
              text("Hai vinto",100,100);
       }
    
    
    if (client != null && stato == play){
    
        //---------------- Draw del gioco ----------------
        m = client.readString(); // leggo l'input
    // variabili di input dal client
        String up = "up";
        String down = "down";
        String right = "right";
        String left = "left";
    
    // viene controllato l'input della tastiera dal client, di modo tale da comandare il gioco
    // sul server
        if( up.equals(m) == true){
          pedina.y-=10.;
          println(pedina.x + " " + pedina.y);
        }
        if( down.equals(m) == true){
          pedina.y+=10.;
          println(pedina.x + " " + pedina.y);
        }
        if( right.equals(m) == true){
          pedina.x+=10.;
          println(pedina.x + " " + pedina.y);
        }
        if( left.equals(m) == true){
          pedina.x-=10.;
          println(pedina.x + " " + pedina.y);
        }
        }
      //-----------------------------------------------
    
      }
    
    
    // The serverEvent function is called whenever a new client connects.
    void serverEvent(Server server, Client client) {
      incomingMessage = "A new client has connected: " + client.ip();
      println(incomingMessage);
      NClient++;
      // Reset newMessageColor to black
      newMessageColor = 0;
    }
    
    class Posizione{
      float x, y, r;
    
      Posizione(float a, float b, float s) {
        x = a;
        y = b;
        r = s;
      }
      Posizione(float a, float b){
        x=a;
        y=b;
      }
    
    }
    
    class Utente{
      String nome;
      int colore;
      Utente (String nome1 , int c){
        nome = nome1;
        colore = c;
      }
    }
    

    //CLIENT

        //CLIENT
        import processing.net.*;
        // Dichiaro un client
        Client client;
        //
        // Stati
        final int statoMenu = 0;
        final int statoColore = 1;
        final int intermedio = 2;
        final int play = 3;
        final int go = 4;
        final int play2 = 5;
        final int fine = 6;
        int stato;
        //----------------------------------
    
        String nome = " ";
        String nome2 [];
        PFont font;
        color c = color(0, 0, 0);
    
        //variabili dal server
        String pos ="";
        float posizione[];
        String [] valori; 
        int []posTesoro;
    
        void setup() {
          size(700, 500);
          client = new Client(this,"127.0.0.1", 5204);
          font = createFont("Helvetica", 18); 
    
    
        }
    
        void draw() {
          background(255);
          textFont(font,18);
            fill(255,0,0);
            // this adds a blinking cursor after your text, at the expense of redrawing everything every frame
            if (stato == statoMenu || stato == statoColore || stato == intermedio){
              text("Nickname:",40,40);
              text(nome +(frameCount/10 % 2 == 0 ? "_" : ""), 125, 40);
            }
            if (stato == statoColore || stato == intermedio){
              colore();
            }
            if (stato == intermedio){
              rect(100, 100, 50, 20);
            }
    
            if (client.available() > 0 /*&& stato==play*/) {
                  posizione = new float [5];
                  pos = client.readStringUntil('*');
                  valori = (split(pos," "));
                  posizione [0] = float(valori[0]);
                  posizione [1] = float(valori[1]);
                  posizione [2] = float(valori[2]);
                  posizione [3] = float(valori[3]);
                  posizione [4] = float(valori[4]); //contiene NClient, cioè il numero di client
                 // stato=go;
    
    
            }
            //String valori [] = new String[5];
            if (stato == go || stato ==play2 || stato == fine){    
                  fill(c);
                  stroke(0);
                  ellipse(posizione[0], posizione[1], posizione[2], posizione[3]);
                  stato=play2;
             }
    
    
        }
    
        void keyPressed() {
          switch(stato){
          case statoMenu:
            keyPressedMenu();
            break;
           case play2:
             keyPressedplay();
          }
        }
    
        void keyPressedMenu(){  
          if (key != CODED) {
            switch(key) {
            case BACKSPACE:
              nome = nome.substring(0,max(0,nome.length()-1));
              stato = statoMenu;
              break;
            case TAB:
              nome += "    ";
              break;
            case ENTER:
              stato=statoColore;
              break;
            /*case RETURN:
              // comment out the following two lines to disable line-breaks
              typedText += "\n";*/
            case ESC:
              exit();
              break;
            default:
              nome += key;
          }
        }
        }
    
    
        void colore(){
          //red
          fill(255, 0, 0);
          rect(50, 50, 20, 20);
          //pink
          fill(255, 0, 255);
          rect(90, 50, 20, 20);
          //purple
          fill(130, 0, 255);
          rect(140, 50, 20, 20);
        }
    
        void keyPressedplay(){
          if (key == CODED){
            if (keyCode == UP){
              String up = "up";
              client.write(up);
              //posizione[1] poichè è la y
              posizione[1]-=10;
              println(posizione[0] + " " + posizione[1]);
            }
            if (keyCode == DOWN){
              String down = "down";
              client.write(down);
              //posizione[1] poicè è la y
              posizione[1]+=10;
              println(posizione[0] + " " + posizione[1]);
            }
            if (keyCode == RIGHT){
              String right = "right";
              client.write(right);
              //posizione[0] perchè è la x
              posizione[0]+=10;
              println(posizione[0] + " " + posizione[1]);
            }
            if (keyCode == LEFT){
              String left = "left";
              client.write(left);
              //posizione[0] perchè è la x
              posizione[0]-=10;
              println(posizione[0] + " " + posizione[1]);
            }
          }
        }
        void mousePressed(){
         if (stato == statoColore){
          if(mouseX > 50 && mouseX < 70 && mouseY > 50 && mouseY < 70){
            c = color(255, 0, 0);
            stato = intermedio;
          }
          if(mouseX > 90  && mouseX < 110 && mouseY > 50 && mouseY <70){
            c = color(255, 0, 255);
            stato = intermedio;
          }
          if(mouseX > 130 && mouseX < 160 && mouseY > 50 && mouseY <70){
             c = color(130, 0, 255);
             stato = intermedio;
          }
         }
         if (stato == intermedio){
          if(mouseX > 100 && mouseX < 150 && mouseY > 100 && mouseY < 120){
            //stato = play;   
           for(int i=1;i<=posizione[4];i++){
             nome2 = new String [100];
             nome2[i]=nome;
             client.write(nome2[i]);
        }
             }
    
         }
        }
    
Sign In or Register to comment.