Servers and Clients
in
Core Library Questions
•
1 year ago
I am trying to create an Real-Time-Strategy game with processing and to do this i am trying to program a server and client. Both my server and the client that I have created keeps getting the error: "StringOutOfBoundsException: String index out of range: -1".
Here is the relevant code:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This is the code for the server
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
import processing.net.*; //Imports processing.net.* Library -- for serverServer gameServer; //Server -- This program is the serverClient gameClient; //ClientString input; //Stores data recieved from Clientint[] data = new int[3]; //Separates data stored in input
void setup(){gameServer = new Server(this, 12345); //Starts server}
void ServerSetup(){//This loops and sends coordinate information for each of your teams units to the Clientfor(int i = 0; i < NumberOfUnits; i++){//Send Team Coordinates -- i = array index, TeamOne[i] = and array of a class called Units//UnitXCoordinate and UnitYCoordinate = the unit's X and Y coordinates
gameServer.write(i + " " + TeamOne[i].UnitXCoordinate + " " + TeamOne[i].UnitYCoordinate + "\n");}//Receive data from clientgameClient = gameServer.available();if (gameClient != null) {//input is a string -- stores the information recieved from client
input = gameClient.readString();
//This is where the error in the program is, from what I understand, input.substring separates the value in input into //an array of strings
input = input.substring(0, input.indexOf("\n")); //Only up to the newline//data is an integer array
data = int(split(input, ' ')); // Split array index and X and Y values into an array
//TeamTwoXY[ ][ ] is an array used for displaying the X and Y coordinates
TeamTwoXY[data[0]][0] = data[1]; //data[0] is the array index sent from the clientTeamTwoXY[data[0]][1] = data[2]; //data[1] is the X coordinate, data[2] is the Y coordinate//Function for creating the image "picture"
image(picture,TeamTwoXY[data[0]][0],TeamTwoXY[data[0]][1],40,80); // Unit}}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This code exists in separate program -- this is the client for the server
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
import processing.net.*;
Client gameClient;String input;int[] data = new int[3];
void setup(){gameClient = new Client(this, "000.0.0.0",12345); //The number 000.0.0.0 is where the server's IP address goes}
void ClientSetup(){for(int i = 0; i < NumberOfUnits; i++){
gameClient.write(i + " " + TeamOne[i].UnitXCoordinate + " " + TeamOne[i].UnitYCoordinate + "\n");}
// Receive data from serverif (gameClient.available() > 0) {input = gameClient.readString();//This is the line where the error occurs
input = input.substring(0, input.indexOf("\n")); // Only up to the newlinedata = int(split(input, ' ')); // Split values into an arrayTeamTwoXY[data[0]][0] = data[1];TeamTwoXY[data[0]][1] = data[2];image(picture,TeamTwoXY[data[0]][0],TeamTwoXY[data[0]][1],40,80); // Unit}}
If anyone has a solution I would be very grateful.
1