好吧,我正在尝试用 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;
}
}
}
}