0

好吧,我正在尝试用 Java 制作一个简单的多人游戏。我已经编写了服务器和客户端。我可以成功连接到服务器,但一秒钟后我收到一个错误,导致我断开连接。

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)

以上是我从客户端收到的错误。服务器没有显示错误。

客户端代码:

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();
        }
        }

    }
}

服务器端代码:

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;
    }
    }
}
}
4

2 回答 2

2

你有

 int[] x = new int [10];

表示当您引用 this.x[pid] 时 x[] 的大小为 10 pid的值是多少?

如果它是10 或 >10你会得到异常,因为索引从 0 开始在你的情况下它是从0 到 9

在for循环内的服务器端执行此操作out.writeUTF("This is a of Java sockets");,而在客户端执行此操作,playerid = in.readInt();因为它没有获得任何整数,它正在读取> 10的随机值

于 2013-02-07T06:57:12.947 回答
0

诺亚,你能给出你输入的样本输入值,它触发了上述异常。因为它自己说的异常,当有一个数组索引值超出时,它在 updatecoordinated(....) 函数中崩溃绑定(在您的情况下可能超出索引 9,因为 x[] 和 y[] 的数组大小为 10)

于 2013-02-07T07:02:11.660 回答