简体   繁体   English

通过客户端/服务器发送按键

[英]Sending a keypress over a client / server

I am creating an Android game and have come across a problem. 我正在创建一个Android游戏,遇到了一个问题。 It is a simple Pong style game where a bat is moved left or right to block a ball. 这是一种简单的Pong风格的游戏,在其中左右移动蝙蝠来阻挡球。 However I am trying to control the bat via a UDP connection. 但是,我试图通过UDP连接来控制蝙蝠。 I have my server set up on my Android code and then a separate Java class for the client. 我在我的Android代码上设置了服务器,然后为客户端设置了单独的Java类。 At the minute I can only send strings or longs from the client to server. 目前,我只能从客户端向服务器发送字符串或long。 What I am trying to achieve is, on the client while it is waiting for an input if I press the left or right arrows then it will get this data, convert it to bytes and then send it to the server. 我要达到的目的是,在客户端等待输入时,如果我按向左或向右箭头,它将获取此数据,将其转换为字节,然后将其发送到服务器。 The server should then receive the data as a key press so that the bat image of the game will move in the appropriate direction. 然后,服务器应通过按键接收数据,以便游戏的蝙蝠图像将向适当的方向移动。 If anyone has any idea how to do this it would be greatly appreciated. 如果有人对如何执行此操作有任何想法,将不胜感激。 Thanks 谢谢

I recently created a game myself where I also needed to do some client-server synchronisation. 我最近自己创建了一个游戏,我还需要进行一些客户端-服务器同步。 I came across the KryoNet library which also works for Android. 我遇到了KryoNet库,该库也适用于Android。

http://code.google.com/p/kryonet/ http://code.google.com/p/kryonet/

It might be worth looking into it since it's not that hard to grasp 可能值得研究,因为它并不难掌握

Good luck on your game! 祝您比赛顺利!

You have to make your own "server codes." 您必须制作自己的“服务器代码”。 What I mean by that is you should send certain key words to the server to let it know that a specific action should be taken. 我的意思是,您应该向服务器发送某些关键字,以使其知道应采取特定的措施。 In Sun's tutorials on creating sockets, you're able to see that the connection does not close until a keyword has been sent to the server. 在Sun有关创建套接字的教程中,您可以看到直到将关键字发送到服务器后,连接才会关闭。 You are able to do the same with this. 您可以这样做。

The Server and Client Example, Telling When The Server Should Close 服务器和客户端示例,告诉服务器何时应关闭

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

相关问题 通过服务器将BufferedImage发送到另一个Java客户端 - Sending BufferedImage over server to another java client 在客户端服务器游戏中通过网络发送文件 - Sending file over network in a client server game Java客户端/服务器-通过套接字连接发送多个字符串 - Java client/server - sending multiple strings over a Socket connection 通过网络从C ++客户端向Java服务器发送字符串 - Sending strings over the net from C++ client to Java server 服务器客户端发送对象 - Server Client sending an object 通过Java套接字发送字符串和图片:服务器未从android客户端接收字符串或图片 - Sending string and picture over java sockets: server not receiving string or picture from android client 使用TCP over socket将ArrayList从服务器端发送到客户端 - Sending ArrayList from server side to client side using TCP over socket 通过TCP发送大字符串时丢失数据。 (客户端服务器) - Missing data while sending large string over TCP. (client/server) 发送一个 ArrayList<String> 使用套接字通过 TCP 从服务器端到客户端? - Sending an ArrayList<String> from the server side to the client side over TCP using socket? 通过套接字将图像从android客户端发送到Java服务器时数据丢失 - Data loss while sending image over socket from android client to Java server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM