简体   繁体   English

创建Java TCP协议

[英]Creating Java TCP Protocol

So I am trying a little bit of network and socket programming, I have created simple client and server java application that will connect and allow users to enter messages and talk to each other. 因此,我尝试了一些网络和套接字编程,我创建了简单的客户端和服务器Java应用程序,该应用程序将连接并允许用户输入消息并彼此交谈。

Next I am trying to create a protocol for a game I am looking to make, the game involves 12 lights that are either on or off, the server will use a protocol to send the client values and depending on these values a certain light will turn on. 接下来,我试图为我要制作的游戏创建协议,该游戏涉及12个打开或关闭的指示灯,服务器将使用协议发送客户端值,并且根据这些值,特定指示灯将变为上。

Example - 000000000100 will turn on light three 示例-000000000100将打开灯三

The protocol will work the same way from the client in that a button will be pressed and the string will be sent back to the server, the two strings will be checked if they match and if so the user will get a point. 该协议将以与客户端相同的方式工作,即按下一个按钮,并将字符串发送回服务器,将检查两个字符串是否匹配,如果匹配,则用户将获得一个分数。

The trouble I am having is where to begin? 我遇到的麻烦是从哪里开始? I have no major knowledge on creating protocols and looking to be pointed in the right direction. 我对创建协议并寻求正确的方向没有什么专业知识。

If I send the binary string between client and server will that be considered a protocol or is there more to it? 如果我在客户端和服务器之间发送二进制字符串,那么该字符串将被视为协议还是它有更多内容?

Really, a protocol is whatever you want it to be. 确实,协议就是您想要的协议。

If you send 000000000100 to a server, and that server understands and responds appropriately, then you have a solid application-level protocol. 如果将000000000100发送到服务器,并且该服务器能够理解并正确响应,则您具有可靠的应用程序级协议。 In the context of your game this may be all your protocol even needs to be, or maybe you need to handle error cases (maybe 000000000100 is an invalid value), this is up to you. 在您的游戏环境中,这可能是您甚至需要的所有协议,或者您可能需要处理错误情况(也许000000000100是无效值),这取决于您。

However, you don't need to worry about things such as "did the server even get my message?" 但是,您无需担心诸如“服务器甚至没有收到我的消息吗?”之类的问题。 because TCP handles that. 因为TCP可以处理。 You probably don't want to get to the depth and complication that is TCP for something like this. 您可能不想像这样的东西深入了解TCP的复杂性。

If you will to create the protocol, this will be too easy, because the rules of the protocol will be established for you, and it depend of the nature of the system. 如果要创建协议,这将太容易了,因为将为您建立协议的规则,这取决于系统的性质。

For example: If the system that you are creating the protocol is a simple chat, in the protocol you need put all data to allows the efective comunication between the server and the client. 例如:如果您正在创建协议的系统是一个简单的聊天,则在协议中您需要将所有数据放入服务器和客户端之间的有效通信中。 Example

>AAAA;MSG=BLABLA;TIME=12121212<

Suppose, it is the basic structure of you protocol, where ">" "<" are delimeters of you protocol (very useful), "AAAA" the type of message in case that you had more than one message. 假设它是您协议的基本结构,其中“>”“ <”是您协议的分隔符(非常有用),“ AAAA”是消息的类型,以防您有多个消息。 "MSG" the message, and "TIME" the time of transmission. “ MSG”消息,“ TIME”发送时间。

Like you see, you define that you want. 如您所见,您定义了自己想要的。

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

相关问题 JAVA Chat Sever使用TCP协议与Iphone客户端通信 - JAVA Chat Sever communicates with Iphone clients using TCP protocol 用Java创建TCP服务器并发送/接收消息 - Creating a TCP Server in Java and send / receive messages 创建输出/输入流时,Java TCP Client是否冻结? - Java TCP Client freezes when creating output / input streams? 如何使用Java套接字将基于tcp / ip的消息转换为(copco)开放协议? - How to convert message to (copco) open protocol based on tcp/ip with Java sockets? 如何在单个 Java class 中将 Traccar Sever 连接复制到设备(通过 TCP 的卡斯特协议)? (连接、断开编码和解码) - How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode) 如何使用Java套接字向我自己的基于TCP的协议添加加密? - How can I add encryption to my own TCP-based-Protocol using Java Sockets? 使用Java中的TCP协议游戏系统的多客户端多实例集中式服务器 - Multi-client multi instance centralised server using TCP protocol gaming system in Java 关闭 TCP 协议的简单规则 - Simple rule for closing a TCP protocol 为自定义TCP协议实现客户端 - Implementing a client for custom TCP protocol 什么是TCP协议中的syn错误? - What is an syn error in TCP Protocol?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM