Network Client Always Null

I am trying to make pong online, but the client on the server side is always null. What can I do to fix this?

import processing.net.*;

Server pong;

int bounceTimer = 0;
int startTimer = 165;
int score1 = 0;
int score2 = 0;
int timeDisplay;
int clientNum = 0;

String input;
int data[];
int id = 0;
int move = 0;

Ball ball = new Ball(300, 200);
Paddle paddle1 = new Paddle(30, 200);
Paddle paddle2 = new Paddle(570, 200);

void setup() {
  size(600, 400);
  frameRate(55);
  pong = new Server(this, 5204);
}

void draw() {
  input = "";
  timeDisplay = (startTimer/55) + 1;
  if (startTimer < 0) {
    timeDisplay = -1;
  }
  bounceTimer++;
  background(0); 
  Client client = pong.available();
  if (client != null) {
    println("true");
    input = client.readString();
    switch (input) {
    case "UP1":
      paddle1.y -= 5;
      break;
    case "DOWN1":
      paddle1.y += 5;
      break;
    case "UP2":
      paddle2.y -= 5;
      break;
    case "DOWN2":
      paddle2.y += 5;
      break;
    default:
      break;
    }
  } else {
    println("false");
  }
  if (startTimer > -1 && clientNum >= 2) {
    startTimer--; 
    textSize(50);
    text(timeDisplay, width/2 -15, height/2 -100);
  }
  ball.act();
  paddle1.act();
  paddle2.act();
  textSize(23);
  text(score1, 5, 25);
  text(score2, width-20, 25);
  pong.write(
    (int)paddle1.y + " " + 
    (int)paddle2.y + " " + 
    (int)ball.x + " " + 
    (int)ball.y + " " + 
    (int)score1 + " " + 
    (int)score2 + " " + 
    (int)timeDisplay + " " +
    (int)clientNum);
}

void serverEvent(Server server, Client client) {
  clientNum++;
}

Provide full code or a runanble version.

Kf

Here is the server and classes. I believe the issue is before the classes.

import processing.net.*;

Server pong;

int bounceTimer = 0;
int startTimer = 165;
int score1 = 0;
int score2 = 0;
int timeDisplay;
int clientNum = 0;

String input;
int data[];
int id = 0;
int move = 0;

Ball ball = new Ball(300, 200);
Paddle paddle1 = new Paddle(30, 200);
Paddle paddle2 = new Paddle(570, 200);

void setup() {
  size(600, 400);
  frameRate(55);
  pong = new Server(this, 5204);
}

void draw() {
  input = "";
  timeDisplay = (startTimer/55) + 1;
  if (startTimer < 0) {
    timeDisplay = -1;
  }
  bounceTimer++;
  background(0); 
  Client client = pong.available();
  if (client != null) {
    println("true");
    input = client.readString();
    switch (input) {
    case "UP1":
      paddle1.y -= 5;
      break;
    case "DOWN1":
      paddle1.y += 5;
      break;
    case "UP2":
      paddle2.y -= 5;
      break;
    case "DOWN2":
      paddle2.y += 5;
      break;
    }
  } else {
    println("false");
  }
  if (startTimer > -1 && clientNum >= 2) {
    startTimer--; 
    textSize(50);
    text(timeDisplay, width/2 -15, height/2 -100);
  }
  ball.act();
  paddle1.act();
  paddle2.act();
  textSize(23);
  text(score1, 5, 25);
  text(score2, width-20, 25);
  pong.write(
    (int)paddle1.y + " " + 
    (int)paddle2.y + " " + 
    (int)ball.x + " " + 
    (int)ball.y + " " + 
    (int)score1 + " " + 
    (int)score2 + " " + 
    (int)timeDisplay + " " +
    (int)clientNum);
}

void serverEvent(Server server, Client client) {
  clientNum++;
}
class Movement {
  float x, y;
  float dx = 0;
  float dy = 0;
  float velocity = 0;
  float course = 0;
  float radius = 0;
  float wid = 0;
  float hei = 0;
  float xc = 0;
  float yc = 0;

  public Movement() {
  }

  public Movement(float newX, float newY) {
    x = newX;
    y = newY;
  }

  public void move() {
    computeVector();
    x += dx;
    y += dy;
  }

  public void computeVector() {
    dx = velocity * cos(radians(course));
    dy = velocity * sin(radians(course));
  }

  public void drawCircle() {
    ellipse(x, y, radius, radius);
    hei = radius;
    wid = radius;
  }
  public void drawRect() {
    rectMode(CENTER);
    rect(x, y, wid, hei);
  }
  public boolean collusion(float x1, float y1, float x2, float y2, float dis1, float dis2) {
    if (dist(x1, y1, x2, y2) < dis1 + dis2) {
      return true;
    } else {
      return false;
    }
  }
  public void yBounceCir() {
    if (y < 0 || y > height) {
      course = course  + (0 - course)* 2;
    }
  }
  public void yBounce() {
    course = course  + (0 - course)* 2;
  }
  public void xBounce() {
    if (bounceTimer > 10)
      course = course  + (90 - course)* 2;
    bounceTimer = 0;
  }
    public void xBounceRandom() {
    if (bounceTimer > 10)
      course = random((course  + (90 - course)* 2)-8,(course  + (90 - course)* 2)+8);
    bounceTimer = 0;
  }
  public void arrowMover(float newVel) {
    if (keyPressed) {
      if (keyCode == UP) {
        course =  270;
        velocity = newVel;
        move();
      } else if (keyCode == DOWN) {
        course = 90;
        velocity = newVel;
        move();
      } else {
        velocity = 0;
      }
    }
  }
  public void yLock(float dis) {
    if (y < (0+dis)) {
      y = dis;
    } else if (y > (height - dis)) {
      y = height - dis;
    }
  }
  public void yLock(float dis, float dis2) {
    if (y < (0+dis)) {
      y = dis;
    } else if (y > (height - dis2)) {
      y = height - dis2;
    }
  }
  public boolean rectBox(Movement a) {
    boolean top = yc + hei > a.yc;
    boolean bottom = yc < a.yc +a.hei;
    boolean left = xc + wid > a.xc;
    boolean right = xc < a.xc + a.wid;
    if (top&&bottom&&left&&right) {
      return true;
    } else {
      return false;
    }
  }
  
  public void setPos(int newX, int newY) {
   x = newX;
   y = newY;
  }
}
class Ball extends Movement {
  public Ball(float x, float y) {
    super(x, y);
    xc = x - radius/2;
    yc = y - radius/2;
    radius = 20;
    velocity = 4;
    course = (int)random(0, 360);
    if (course >= 225 && course <= 315) {
      course = 215;
    } else if (course >= 45 && course <= 135) {
      course = 35;
    }
  }

  void act() {
    if (startTimer > 0) {
      velocity = 0;
    }
    if (startTimer == 0) {
      velocity = 4;
    }
    drawCircle();
    move();
    yBounceCir();
    if (rectBox(paddle1)) {
      if (x > paddle1.x) {
        xBounceRandom();
        if (velocity < 9) {
          velocity += random(0, 0.2);
        }
      }
    }
    if (rectBox(paddle2)) {
      if (x < paddle2.x + paddle2.wid) {
        xBounceRandom();
        if (velocity < 9) {
          velocity += random(0, 0.2);
        }
      }
    }
    xc = x - radius/2;
    yc = y - radius/2;
    if (x < 0 - wid) {
      startTimer = 165;
      score2++;
      setPos(width/2, height/2);
    }
    if (x > width + wid) {
      startTimer = 165;
      score1++;
      setPos(width/2, height/2);
    }
  }
}
class Paddle extends Movement {
  public Paddle(float x, float y) {
    super(x, y); 
    xc = x - wid/2;
    yc = y - hei/2;
    wid = 15;
    hei = 90;
    velocity = 0;
  }

  void act() { 
    fill(255);
    drawRect();
    arrowMover(5);
    yLock(hei/2);
    xc = x - wid/2;
    yc = y - hei/2;
  }
}

Here is the client.

import processing.net.*;

Client player;

String input;
int data[];
int paddle1, paddle2, ballx, bally, score1, score2, startTime, num;
int id = -1;
int output = 0;
String out;

void setup() {
  size(600, 400);
  player = new Client(this, "127.0.0.1", 5204);
}

void draw() {
  background(0);
  if (player.available() > 0) {
    input = player.readString();
    data = int(split(input, ' '));
    paddle1 = data[0];
    paddle2 = data[1];
    ballx = data[2];
    bally = data[3];
    score1 = data[4];
    score2 = data[5];
    startTime = data[6];
    num = data[7];
    if (id < 0) {
      id = num;
    }
  }
  rectMode(CENTER);
  fill(255);
  rect(30, paddle1, 15, 90);
  rect(570, paddle2, 15, 90);
  ellipse(ballx, bally, 20, 20);
  textSize(23);
  text(score1, 5, 25);
  text(score2, width-20, 25);
  if (startTime < 4 && startTime > 0) {
    textSize(50);
    text(startTime, width/2 -15, height/2 -100);
  }
  if (output != 0) {
    player.write(out);
  }
  if (keyPressed) {
    if (id == 1200) {
      if (keyCode == UP) {
        out = "UP1";
      }
      if (keyCode == DOWN) {
        out = "DOWN1";
      }
    } else if (id == 2200) {
      if (keyCode == UP) {
        out = "UP2";
      }
      if (keyCode == DOWN) {
        out  = "DOWN2";
      }
      output = 1;
    } else {
      output = 0;
    }
    println(id);
  }
}

For this, it is better to use a smaller program to see if your network is working properly, or your network libraries are proper, or Processing is up to date. After you test it and if it works, then it is something in your code.

I have tested the server-client in the code below. You need to update the your ip address. For the code below, you start the server first followed by the client. Yo press any key on the client’s sketch. Check the client’s console in the PDE to see what you send and what you received. What happens is that the server receives the data and it sends it back to the client. I do a check in the client’s side to make sure what I receive is the same as what I sent.

Kf

SERVER

import processing.net.*;

final String WHOAMI="SERVER";

Server server;
String message=WHOAMI;

void setup() {
  size(1400, 800);
  server = new Server(this, 5204);
  fill(255);
  textAlign(CENTER, CENTER);


  println("Starting server");
}

void draw() {
  background(0);
  translate(width/2, height/2);
  text(message, 0, 0);

  checkForIncoming();
}


void checkForIncoming() {
  Client nextClient = server.available();
  if (nextClient != null) {
    println("Client is available, reading bytes");

    byte[] byteBuffer = nextClient.readBytes();

    if (byteBuffer.length > 0) {
      println("Time:", millis(), "Received data. Trying to decode.", byteBuffer.length);

      printArray(byteBuffer);
      for (int i=0; i<byteBuffer.length; i++)
        server.write(byteBuffer[i]-0);

      int valTx=0;
      for (int i=0; i<byteBuffer.length; i++) {
        //Converts byte to digit and multiplies by 10^j, where j is position in number 
        valTx += ((byteBuffer[i]-'0') & 0xff) * (pow(10, byteBuffer.length-1-i));
      }

      message=WHOAMI;
      message="Server relays  " + valTx;
    } else {
      println("Byte amount not above 0");
    }
  }
}

CLIENT

import processing.net.*;

final String WHOAMI="CLIENT";

Client client;
int valOut=-1;
String message=WHOAMI;


void setup() {

  size(600, 100);
  fill(0);
  textAlign(CENTER, CENTER);

  // String server = "192.168.1.15";
  String server = "192.168.1.67";
  client = new Client(this, server, 5204);

  background(255);
  println("Starting client");
}

void draw() {
  background(255);
  translate(width/2, height/2);
  text(message,0,0);

  if (client.available()>0) {


    byte[] byteBuffer = client.readBytes();
    if (byteBuffer.length > 0) {

      int valRx=0;
      for (int i=0; i<byteBuffer.length; i++) {
        //Converts byte to digit and multiplies by 10^j, where j is position in number 
        valRx += ((byteBuffer[i]-'0') & 0xff) * (pow(10, byteBuffer.length-1-i));
      }

      println("Time:", millis(), byteBuffer.length, "Client receives. Trying to decode.", valRx);
      println("Data transfer between client and server and back to client was a ", (valRx-valOut)==0?"SUCCESS":"FAILURE");
      printArray(byteBuffer);  
      
      message=WHOAMI;
      message="Client last data transfer " + valOut;

      //Reset fields 
      valOut = -1;
    }
  }
}

void keyTyped() {


  valOut=frameCount%10000;
  String strout=str(valOut);  //Limits to 4 digits

  println("Time:", millis(), "Client sends ", strout);
  client.write(strout.getBytes());
}

Keyword: kf_keyword server client network net

I tried running these, and they both started, but they did not connect. I changed the IP, and my processing and network library are up to date. I did press a key on the client’s sketch, and I started the server first. What else could be going wrong?

Double verify the ip. Try another port, maybe 11000? What OS are you using?

Kf

Changing the port worked. I am using windows 10. Thank you so much!

Good to know and thxs for updating us on your status. Weird it happens… Good luck with your game.

Kf

Thanks for your help!