Java Socket连接 - >写入Server时出现奇怪的崩溃

时间:2017-06-06 07:17:16

标签: java sockets networking server

我最近制作了一个2D平台游戏并试图实现一些在线2玩家功能。有一个服务器程序,当然还有客户端。它的工作方式如下:您的客户端可以在ID 1和ID 2之间进行选择,并且ID会直接发送到服务器,因此服务器可以决定接收哪些数据以及要将数据发回给您(客户端1需要客户端2的坐标)对于前。)。

现在的问题是发送的数据以某种方式被操纵并且存在错误的值。另外,如果我发送了一个真正的布尔值,我的客户端会在我发送布尔值的行中停止,并且不会继续运行代码!

我重建了" connect"新客户端程序中的方法1比1,与我的gameclient存在相同的问题!

感谢您的建议!经过长时间的谷歌搜索似乎没有同样的问题: - (

来自(重建)客户的代码:

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

public class main {

    public int x=5,y=10,id=2;
    public int rX=0,rY=0;
    public boolean receivedAttacked = false;
    public boolean attacked=false;

    public static void main(String[] args) {
        System.out.println("Connecting...");
        new main();
    }

    public main() {
        try {
            Socket socket = new Socket("localhost",6789);
            DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
            DataInputStream din = new DataInputStream(socket.getInputStream());

            dout.writeByte(1);
            dout.writeInt(id);
            dout.flush();

            dout.writeByte(2);
            dout.writeInt(x);
            System.out.print("X: " + x + " / ");
            dout.flush();

            dout.writeByte(3);
            dout.writeInt(y);
            System.out.print("Y: " + y + " / ");
            dout.flush();

            dout.writeByte(4);
            System.out.print("Attacked: " + attacked + " / " + "\n");
            dout.writeBoolean(attacked);
            dout.flush();

            int byteIndex = 3;
            byte byteRead;
            for(int i = 0; i < byteIndex; i++) {
                byteRead = din.readByte();

                switch(byteRead) {
                    case 1:
                        rX = din.readInt();
                        System.out.print("RECEIVED:" +  "x: " + rX + " / ");
                    case 2:
                        rY = din.readInt();
                        System.out.print("Y: " + rY + " / ");
                    case 3:
                        receivedAttacked = din.readBoolean();
                        System.out.print("rAttacked: " + receivedAttacked + " / " + "\n");
                    default:
                }
            }
            System.out.println("PAUSE");
            dout.close();
            din.close();
            socket.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

服务器代码(只是处理所有输入和输出的线程):

package me.server.lel;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JFrame;

public class ServerThread extends Canvas implements Runnable {

    Socket incoming; //the client socket so to say

    public int id;
    public  int x1 = 0,y1 = 0,x2 = 0,y2 = 0;
    public  boolean attacked1 = false, attacked2 = false;

    public ServerThread(Socket incoming, int id) {
        this.incoming = incoming;
        this.id = id;
    }

    @Override
    public void run() {

        try {
            //Reading the input and saving it in global variables
            DataInputStream in  = new DataInputStream(incoming.getInputStream());

            byte index;
            int byteAmount = 4;

            for(int i = 0; i < byteAmount; i++ ){
                index = in.readByte();
                switch(index) {
                    case 1:
                        id = in.readInt();
                        break;
                    case 2:
                        if(id == 1) {
                        x1 = in.readInt(); 
                        } else if(id == 2) {
                            x2 = in.readInt(); ;
                        }
                        break;
                    case 3:
                        if(id == 1) {
                            y1 = in.readInt(); 
                        } else if(id == 2) {
                            y2 = in.readInt(); 
                        }
                    case 4:
                        if(id == 1) {
                            attacked1 = in.readBoolean();
                        } else if(id == 2) {
                            attacked2 = in.readBoolean();
                        }
                    default:
                }
            }

            //Writing back the values of the opposite player
            DataOutputStream out = new DataOutputStream(incoming.getOutputStream());

            if(id == 1) {
                out.writeByte(1);
                out.writeInt(x2);
                out.flush();

                out.writeByte(2);
                out.writeInt(y2);
                out.flush();

                out.writeByte(3);
                out.writeBoolean(attacked2);
                out.flush();

            } else if(id == 2) {
                out.writeByte(1);
                out.writeInt(x1);
                out.flush();

                out.writeByte(2);
                out.writeInt(y1);
                out.flush();

                out.writeByte(3);
                out.writeBoolean(attacked1);
                out.flush();
            }
        } catch(SocketException e) {
            System.out.println("User Disconnected: " + incoming.getLocalAddress());
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }   
    }
}

因为您可以看到我在我的客户端代码中执行了一些控制台输出。因此,如果我按原样运行程序,那就是控制台输出:

Connecting...
X: 5 / Y: 10 / Attacked: false / 
RECEIVED:x: 0 / Y: 33554432 / rAttacked: false / 
rAttacked: false / 

所以你可以看到收到的每个值都是错误的,除了布尔值。现在,如果我进入我的客户端代码并设置&#34;攻击&#34; boolean为true,输出就像这样运行:

Connecting...
X: 5 / Y: 10 / Attacked: true / 

所以我假设代码停止了并且没有继续代码(发生在我的客户端,甚至停止滴答)

对不起,很长的帖子,我希望我的代码足以让你们查一下!非常感谢你!

1 个答案:

答案 0 :(得分:1)

您忘记了自己的break在您的客户cases以及服务器中的case 3case 4。例如,在这里,当您的客户端发送第三个字节时,服务器会读取它,但也会通过处理布尔值的case 4。 这可能不是奇怪数字值的来源,但我很确定这是布尔错误的原因。

尝试添加所有break语句,这样就不会在执行时使用字节操作做一些无意识的疯狂事情。