Java 客户端到服务器未知来源 [英] Java Client to Server Unknown Source

查看:77
本文介绍了Java 客户端到服务器未知来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的乒乓球游戏,需要通过网络工作,服务器将使用球的位置和 2 个球棒创建一个游戏,当客户端连接到服务器时,服务器将创建一个名为 PongPlayerThread 的新类它将处理客户端到服务器的输入和输出流,

I have a simple pong game that needs to work over a network, the server will create a game with the positions of the ball and 2 bats, when a client connects to the server, the server will create a new class known PongPlayerThread which will deal with the input and output streams of the client to server,

我的服务器在没有任何数据从客户端到服务器的情况下工作 100% 正常,服务器可以毫无问题地向客户端发送信息,但是我有一个奇怪的问题,但首先这是我的代码,所以你可以看到什么我有.

My server works 100% fine without any data from the client to the server, the server can send information to the client without any problems, but I have a strange problem, But first here is my code, so you can see what I have.

PongServer

try
{
    serverSocket = new ServerSocket(port);
    listen = true;
    System.out.println("Server was setup and will try to create a socket");
}
catch(IOException e)
{
    System.err.println("Could not listen on port:" + port);
    System.exit(1);
}

while(listen)
{
    players[idPlayer] = new PongPlayerThread(serverSocket.accept(), idPlayer, rtnInfo());
    players[idPlayer].start();
    System.out.println("Client Connected with ID:" + idPlayer);
    players[0].passData(rtnInfo());
    idPlayer++;     
    if(idPlayer > 1)
    {
        listen = false;
        playing = true;
    }
}

while(playing)
{
    players[0].passData(rtnInfo());
    players[0].sleep(25);
    players[1].passData(rtnInfo());
    players[1].sleep(25);
}

....//more, but not important

这是我的 PongClient

Here is my PongClient

try
{
    socket = new Socket(host, port);
    serverOut = new PrintWriter(socket.getOutputStream(), true);
    serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException e)
{
    System.err.println("Couold not connect to host:" + host);
    System.exit(1);
}
catch (IOException e)
{
    System.err.println("Could not get Input/Output from server");
    System.exit(1);
}

...

while ((pos = serverInput.readLine()) != null) 
{
    String text = "nothing";
    serverOut.println(text);
    String[] posValues = pos.split(":");
        model.getBall().setX(Double.parseDouble(posValues[0]));
        model.getBall().setY(Double.parseDouble(posValues[1]));


    /*if(PongController.moveUp == true)
    {
        System.out.println("Up");
        serverOut.println("up");
        PongController.moveUp = false;
    }
    else
    {
        serverOut.println("nothing");
    }*/

}

这是我的 PongPlayerThread

Here is my PongPlayerThread

try
{
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
        new InputStreamReader(
        socket.getInputStream()));

    String text = "hhh";

    System.out.println(in.toString());
    //System.out.println(text = in.readLine());
    System.out.println("Checking readLine value");

    String line; 
    if ((line = in.readLine()) == null) 
    { 
        System.out.println("A ok"); 
    } 
    else 
    { 
        System.out.println(":" + line); 
    }

    while(send)
    {
        //String temp = in.readLine();
        //if(temp.equals("up"))
        //{
        //        System.out.println("Up you say");
        //}
        out.println(pongData);
    }

    out.close();
    in.close();
    socket.close();
}

catch (IOException e) 
{
    e.printStackTrace();
}

现在,当我运行我的服务器时一切正常,然后我连接一个客户端,当一个客户端连接时,乒乓球应该静止不动,同时等待另一个玩家,但球会在不从服务器获取数据的情况下自行更新,一旦我关闭客户端程序,我的服务器就会出现这个错误

Now when I run my server it is fine, I then connect a client, when a client connects the pong ball should sit still while it waits for another player, but the ball will just update itself without getting data from the server, once I close the clients program, my server will come up with this error

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Pong.PongPlayerThread.run(PongPlayerThread.java:42)

PongPlayerThread 中的第 42 行是这个

The line 42 in PongPlayerThread is this

if ((line = in.readLine()) == null) 

我这几天一直在努力解决这个问题,但我仍然没有找到解决方案,我觉得inputStream无法连接到客户端的outputStream,我尝试过使用wireShark但这是一个局域网程序,所以它不起作用,并且wireShark中不会显示任何内容.如果有人能对此有所启发,将不胜感激.

I have been trying to fix this for days, but I have still not found the solution, I feel like the inputStream cannot connect to the outputStream of the client, I have tried to use wireShark but this is a LAN program, so it won't work and nothing will show up in wireShark. If anyone could shine some light onto this, It would be much appreciated.

画布

iTech 更新好的,我在这里使用了您的代码,现在是我的 PongPlayerThread 中的代码

iTech update Ok I have used your code here is what is in my PongPlayerThread now

public void run()
{
    try
    {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    socket.getInputStream()));

        String text = "hhh";

        System.out.println(in.toString());
        //System.out.println(text = in.readLine());
         System.out.println("Checking readLine value");

         String line = null; 
         if ((line = in.readLine()) != null) // why you check if it is null !?
         { 
             System.out.println("Client sent: "+line); 
         } 

        while(send)
        {
            out.println(pongData);
        }

         out.close();
         in.close();
         socket.close();
    }

这将在控制台中显示客户端发送:您好",但我的客户端不会停止并继续从服务器接收数据,

this will say in console "Client sent: Hello", but my client will not stop and keep taking in data from the server,

如果我将您给我的 if 语句放入具有 out.println(pongData) 的 while 语句中,则它可以工作,但是一旦客户端连接和断开连接,我就会收到错误消息,或者如果两个客户端连接,然后它们就会收到错误消息两者都让我再次收到此错误:(

If i put the if statement you gave me into the while statement which has out.println(pongData) it works but I get a error once a client connects and disconnects, or i get a error if two clients connect and then they both leave i get this error again :(

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Pong.PongPlayerThread.run(PongPlayerThread.java:45)

第 45 行是

if ((line = in.readLine()) != null) // why you check if it is null !?

整理了代码,但现在在我的 pongClient 代码中

Sorted the code out, but now in my pongClient code

 while ((pos = serverInput.readLine()) != null) 
    {
        String text = "nothing";
        serverOut.println(text);
        String[] posValues = pos.split(":");
        model.getBall().setX(Double.parseDouble(posValues[0]));
        model.getBall().setY(Double.parseDouble(posValues[1]));


        if(PongController.moveUp == true)
        {
            System.out.println("Up");
            serverOut.println("up");
            PongController.moveUp = false;
        }
        else
        {
            serverOut.println("nothing");
        }

    }

一旦碰到这个,它就不会做任何事情,并且会再次导致整个错误.

Once it hits this, it wont do anything, and will cause the whole error again.

我发现错误,我放在哪里

I found the error, where i had put

 if ((line = in.readLine()) != null)
             { 

如果再次放入 line = in.readLine() 会导致错误.奇怪,但现在修复了,数据可以从客户端发送到服务器,从服务器发送到客户端:)

if you put line = in.readLine() again, it will cause an error. strange, but it is fixed now, and data can be sent from the client to server, and server to client :)

推荐答案

一旦客户端连接到您的服务器,if ((line = in.readLine()) == null) 将阻止线程直到收到来自客户端的消息.

Once a client connects to your server the if ((line = in.readLine()) == null) will block the thread until a message is received from the client.

建立连接后需要从客户端发送一些东西

You need to send something from the client after the connection is established

PongPlayerThread中,您可以修改以下内容并删除else

In PongPlayerThread you can modify the following and remove the else block

String line = null; 
if ((line = in.readLine()) != null) // why you check if it is null !?
{ 
    System.out.println("Client sent: "+line); 
} 

PongClient中建立socket连接后,你可以发送一些消息,例如serverOut.println("你好");

In PongClient after establishing the socket connection, you can send some message, e.g. serverOut.println("Hello");

这篇关于Java 客户端到服务器未知来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆