简体   繁体   English

DataOutputStream-始终写入服务器?

[英]DataOutputStream- Always writing to server?

For the purposes of a simple 2D game, I need my client program to always be sending coordinates to a server. 出于简单2D游戏的目的,我需要我的客户端程序始终将坐标发送到服务器。

So, I created a test to see if I could make both players have the same velocity by sending and retrieving values from the server. 因此,我创建了一个测试,以查看是否可以通过从服务器发送和检索值来使两个玩家具有相同的速度。

package main;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class DataSender implements Runnable{

    private DataInputStream fromServer;
    private DataOutputStream toServer; 

    Player player;
    Player opponent;

    public DataSender(DataInputStream fromServer, DataOutputStream toServer, Player player, Player opponent){

        this.fromServer = fromServer;
        this.toServer = toServer; 

        this.player = player;
        this.opponent = opponent;   
    }

    @Override
    public void run() {

        while(true){

                try {
                    toServer.writeInt(player.velX);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    opponent.velX = fromServer.readInt();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }   
    }
}

The while(true) loop only executes one time upon thread creation. while(true)循环仅在创建线程时执行一次。 How can I establish a constant stream of data? 如何建立恒定的数据流?

Your readInt function is probably blocking. 您的readInt函数可能正在阻塞。 If the server doesn't reply to the client with the opponents position, then the loop will stop until 4 bytes are received from the server. 如果服务器没有用对手的位置回复客户端,则循环将停止,直到从服务器接收到4个字节为止。

Or, it could be the writeInt call. 或者,可能是writeInt调用。 Be sure to disable Nagle's algorithm. 确保禁用Nagle的算法。 It will cause undue latency with such small amounts of data being sent. 发送如此少量的数据会导致不适当的延迟。 In Java, its referred to as TCP_NODELAY . 在Java中,其称为TCP_NODELAY This could also be what's wrong with the code, the bytes you are writing are not being sent because there's not enough data being written to cause transmission. 这也可能是代码的问题,因为没有足够的数据写入导致传输,所以未发送正在写入的字节。

Even better, use UDP instead, if you're entirely open to suggestion here. 更好的是,如果您完全愿意在这里提出建议,请改用UDP。 UDP is typically used in game programming because the data involved is real time, and is only useful for a short period of time. UDP通常用于游戏编程中,因为所涉及的数据是实时的,并且仅在短时间内有用。 TCP slows this down because you might be acknowledging packets, or waiting to receive packets you no longer care about because they are too old. TCP减慢了速度,因为您可能正在确认数据包,或者等待接收不再关心的数据包,因为它们太旧了。 UDP gives you control over that. UDP使您可以控制。

Also, typically there's a thread used for sending, and a thread for receiving in networking code. 另外,通常有一个用于发送的线程和一个用于接收网络代码的线程。 I suggest you create 2 threads so you don't have this kind of problem. 我建议您创建2个线程,这样就不会出现此类问题。

In this situation, I recommend using a separate thread for your writing of data. 在这种情况下,建议您使用单独的线程来写入数据。 For instance, when the velocity changes via user input, send the velocity/location packet. 例如,当速度通过用户输入改变时,发送速度/位置数据包。 Use a thread like this to constantly read(and block) for the velocity/position of the opponent. 使用这样的线程不断读取(并阻止)对手的速度/位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataOutputStream写入过多 - DataOutputStream writing too much 使用DataOutputStream编写大字符串 - Writing large strings with DataOutputStream 使用 DataOutputStream 写入文件的 Java 不起作用 - Java writing into file with DataOutputStream not working java,用DataOutputStream写入数据大小 - java, writing data size with DataOutputStream 使用DataOutputStream向Server Socket写消息到Client Socket的消息仅在关闭Client Socket后才发送,为什么? - Writing messages to client socket using DataOutputStream to Server Socket only sent after closing the Client Socket why? Java:使用DataOutputStream向套接字写入会减慢然后停止 - Java: Writing to socket with DataOutputStream slows down, then stops 通过写入DataOutputStream通过蓝牙将命令发送到设备 - Sending command to a device over Bluetooth by writing to DataOutputStream Java-DataOutputStream的writeLong方法并写入十六进制 - Java - writeLong method of DataOutputStream and writing hex TCP客户端/服务器程序,DataInputStream / DataOutputStream问题 - TCP client/server program, DataInputStream / DataOutputStream issue 软件导致连接异常终止:写入DataOutputStream时套接字写入错误 - Software caused connection abort: socket write error while writing to DataOutputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM