Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9282297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:20:21+00:00 2026-06-18T18:20:21+00:00

Alright I’m trying to make a simple multiplayer game in Java. I’ve written the

  • 0

Alright I’m trying to make a simple multiplayer game in Java. I’ve written the server and client. I can connect to the server successfully but after a second I get a error that disconnects me.

Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 1769152617
    at Client.updateCordinates(Client.java:49)
    at Input.run(Client.java:143)
    at java.lang.Thread.run(Unknown Source)

The above is the error I receive from the client.
No error is displayed with the server.

Client side code:

import java.applet.*;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

@SuppressWarnings("unused")
public class Client extends Applet implements Runnable, KeyListener{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static Socket socket;
    static DataInputStream in;
    static DataOutputStream out;
    int playerid;

    int[] x = new int [10];
    int[] y = new int [10];

    boolean left,down,right,up;

    int playerx;
    int playery;
    public void init(){
        setSize(100,100);
        addKeyListener(this);
        try{
        System.out.println("Connecting...");
        socket = new Socket("localhost",7777);
        System.out.println("Connection successful.");
        in = new DataInputStream(socket.getInputStream());
        playerid = in.readInt();
        out = new DataOutputStream(socket.getOutputStream());
        Input input = new Input(in,this);
        Thread thread = new Thread(input);
        thread.start();
        Thread thread2 = new Thread(this);
        thread2.start();
    }catch(Exception e){
        System.out.println("Unable to start client");
    }
    }
    public void updateCordinates(int pid, int x2, int y2){
        this.x[pid] = x2;
        this.y[pid] = y2;

    }
    public void paint(Graphics g){
        for (int i=0; i <10;i++){
        g.drawOval(x[i], y[i], 5, 5);
    }

    }
    public void run(){
            while(true){
                if(right == true){
                    playerx += 10;

                }
                if(left == true){
                    playerx -= 10;

                }
                if(up == true){
                    playery += 10;

                }
                if(down == true){
                    playery -= 10;

                }
                if(right||left||up||down){
                    try{
                    out.writeInt(playerid);
                    out.writeInt(playerx);
                    out.writeInt(playery);
                }catch(Exception e){System.out.println("Error sending cordinates");

                }
                }
                repaint();
                try{
                Thread.sleep(400);
            }catch(InterruptedException e){
                e.printStackTrace();

            }
            }
        }
    public void keyPressed(KeyEvent e){
            if (e.getKeyCode() == 37){
                left = true;
            }
            if (e.getKeyCode() == 38){
                up = true;
            }
            if (e.getKeyCode() == 39){
                right = true;
            }
            if (e.getKeyCode() == 40){
                down = true;
            }

        }
    public void keyReleased(KeyEvent e){
             if (e.getKeyCode() == 37){
                left = false;
            }
            if (e.getKeyCode() == 38){
                up = false;
            }
            if (e.getKeyCode() == 39){
                right = false;
            }
            if (e.getKeyCode() == 40){
                down = false;
            }

        }
    public void keyTyped(KeyEvent e){

        }
}

class Input implements Runnable{
    DataInputStream in;
    Client client;
    public Input(DataInputStream in, Client c){
        this.in = in;
        this.client = c;
    }
    public void run(){
        while(true){
            try {
                int playerid = in.readInt();
                int x = in.readInt();
                int y = in.readInt();
                client.updateCordinates(playerid, x, y);
        }catch(IOException e){
            e.printStackTrace();
        }
        }

    }
}

Server Side Code:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

   static ServerSocket serverSocket;
   static Socket socket;
   static DataOutputStream out;
   static Users[] user = new Users[10];
   static DataInputStream in;
   public static void main(String[]args)throws Exception{
       System.out.println("Starting server");
       serverSocket = new ServerSocket(7777);
       System.out.println("Server Started");
       socket = serverSocket.accept();
       for (int i=0; i <10;i++){
       System.out.println("Connection from:"+socket.getInetAddress());
       out = new DataOutputStream(socket.getOutputStream());
       out.writeUTF("This is a of Java sockets");
       System.out.println("Data has been sent.");
       in = new DataInputStream(socket.getInputStream());
       if(user[i]==null){
           user[i] = new Users(out,in,user,i);
       Thread th = new Thread(user[i]);
       th.start();
       break;

    }
    }
    }
}
class Users implements Runnable{
    DataOutputStream out;
    DataInputStream in;
    Users[]user=new Users[10];
    String name;
    int playerid;
    int playeridin;
    int xin;
    int yin;
    public Users(DataOutputStream out, DataInputStream in, Users[] user, int pid){
       this.out = out;
       this.in = in;
       this.user = user;
       this.playerid = pid;

}
public void run(){
    try {
        out.writeInt(playerid);
    }catch (IOException e1){
        System.out.println("Failed to send PlayerID");
    }
    while(true){
        try{
            playeridin = in.readInt();
            xin = in.readInt();
            yin = in.readInt();
            for(int i=0;i<10;i++){
                if (user[i] !=null){
                    user[i].out.writeInt(playeridin);
                    user[i].out.writeInt(xin);
                    user[i].out.writeInt(yin);
        }
    }
}
    catch(IOException e){
        user[playerid] = null;
    }
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:20:24+00:00Added an answer on June 18, 2026 at 6:20 pm

    You have

     int[] x = new int [10];
    

    means the size of x[] is 10
    when you refer this.x[pid] what is the value of pid?

    If it is 10 or >10 you’ll get the exception because the indexes starts from 0
    In your case it is from 0 to 9

    Also

    In the server side inside the for loop you do this out.writeUTF("This is a of Java sockets"); while on the client side you do this playerid = in.readInt(); since it is not getting any integer it is reading a random value which is >10

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, here I am again trying to write code from scratch and I can't
Alright, guys, I'm trying to get a basic hang on Java and I figured
Alright guys, i'm trying to use the variable in the getIndividualMap function but i'm
Alright, I'm trying to use the basic document.getElementsByTagName function, but each time I do,
Alright I am trying to set up an OAuth Provider in PHP, but I
Alright, I'm trying to figure out why I can't understand how to do this
Alright I don't see why this isnt working. It seems pretty simple. Here is
Alright, this is driving me nuts because my regex is working on Rubular, but
Alright so I have no idea how to even begin doing this But basically
Alright, I'll try and make this brief as possible. I wanted to a UIToolbar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.