简体   繁体   English

将坐标从Java客户端传递到Java服务器

[英]Passing coordinates from a java client to a java server

Firstly I know coordinates is probably the wrong terminology? 首先,我知道坐标可能是错误的术语? I'd assume i'd simply be passing the x and y variables, but coordinates describe it better I feel. 我假设我只是传递x和y变量,但是坐标感觉更好。

Now I need to have a Server which can be accessed by 2 clients, it is a racing game and it requires each client to be able to maneuver a racecar simultaniously, each using a different control scheme but that's neither here nor there. 现在,我需要一个可以由2个客户端访问的服务器,这是一个赛车游戏,它要求每个客户端都能够同时操纵赛车,每个客户端使用不同的控制方案,但这既不是这里也不是那里。

I was hoping someone would be able to assist me when it came to sending the x and y positions of a racecar to the server and having the server send them onto the next player and vice versa to allow both racecars to move at the same time on each clients window. 我希望有人能够将赛车的x和y位置发送到服务器,并让服务器将它们发送到下一个玩家,反之亦然,以允许两个赛车同时移动,从而能够为我提供帮助。每个客户窗口。 So far i've only done the simple server stuff, such as the knock knock server on the sun website, and a simple echo server which repeats a string I send to the server. 到目前为止,我只做过简单的服务器工作,例如sun网站上的点击敲门服务器,还有一个简单的echo服务器,它重复了我发送到服务器的字符串。

When I tried to use int instead of string I recieved an error that the int I wanted to pass was dynamic (obviously changes with each movement) and cannot be passed as static (using readInt and writeInt). 当我尝试使用int而不是字符串时,收到一个错误,我想要传递的int是动态的(显然随着每次移动都会发生变化),并且不能作为静态传递(使用readInt和writeInt)。

So any help on how to create the wanted movement on both client windows through the server would be appreciated. 因此,任何有关如何通过服务器在两个客户端窗口上创建所需移动的帮助都将受到赞赏。

Thanks 谢谢

A simple solution is to use ObjectOutputStream with serializable objects : 一个简单的解决方案是将ObjectOutputStream与可序列化的对象一起使用:

class Coordinates implements Serializable{...}

ObjectOutputStream out = new ObjectOutputStream(...);
out.writeObject(new Coordinates(...));

Craig, you're close. 克雷格,你很近。 Dynamic/static is a misunderstanding, this has nothing to do with your racing game or values changing. 动态/静态是一种误解,与您的赛车游戏或价值观改变无关。 It's just that you can't deserialize fields, that are declared static (or transient), as it says in the javadoc for ObjectInputStream: 就像您在ObjectInputStream的Javadoc中所说的那样,只是不能对声明为静态(或瞬态)的字段进行反序列化:

Fields declared as transient or static are ignored by the deserialization process. 反序列化过程将忽略声明为瞬态或静态的字段。

The solution is (hopefully) simple: remove the static modifier from your x/y integers, that should solve the problem. 解决方案很简单(希望如此):从x / y整数中删除static修饰符,即可解决问题。 btw - if your String had been static, it would have failed exactly the same way. 顺便说一句-如果您的String是静态的,则将以完全相同的方式失败。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM