小编Max*_*728的帖子

Netty客户端到服务器消息

这实际上是我在这里发表的第一篇文章,我一直试图解决这个问题已经有一段时间了,但我终于打电话给国旗并尝试在这个主题上寻求帮助.

所以我有一个客户端和一个服务器,它是在echo客户端/服务器和安全聊天客户端/服务器之后建模的.我对聊天的SSL部分并不感兴趣,只是为了确保我收到客户端/服务器的响应.我将在本文的底部添加所有相关代码.我目前遇到的问题是,我可以在客户端连接时从服务器向客户端发送消息,但是在服务器向客户端发送初始消息时,我无法从客户端向服务器发送消息.从服务器发送的消息是:

Welcome to the server!
Run Code Online (Sandbox Code Playgroud)

来自客户端的消息是

test
Run Code Online (Sandbox Code Playgroud)

我应该知道我收到了来自客户端的消息,因为它应该回显

[You] test
Run Code Online (Sandbox Code Playgroud)

我知道服务器看到客户端,它给我状态更新但我不能出于某种原因向服务器发送消息.现在提出一个问题...有一次机会我正在使用StringDecoderStringEncoder作为解码器和编码器...如果你正在制作一个游戏(这就是我正在做的),你会有类似的东西登录,玩家动作,世界更新等......发送Strings是最好的方法吗?我知道我看到很多字节流,在我的编程课中,我经历过触摸操作字节流,但我仍然不是很满意它们.如果字节流是更好/最好的方法,那么有人可以详细解释如何操作字节流以处理不同的项目.

如前所述,这是客户端中所有内容的开始:

public class Client {

public Client() {
    // Initialize the window
    GameWindow.init();
    // Initialize the server connection
    ClientHandler.init();
}

public static void main(String[] args) throws Exception {

    // Set a default server address if one isn't specified in the arguments
    if (args.length < 2 || args.length > 3) {
        System.err.println("Usage: " + Client.class.getSimpleName() + " <host> <port> [<first message size>]");
        System.err.println("Using default values."); …
Run Code Online (Sandbox Code Playgroud)

java connection client tcp netty

11
推荐指数
1
解决办法
2万
查看次数

有人可以更好地解释解码器/编码器吗?

修订问题:

好的,所以我想把它融入我自己定制的游戏中.我理解Netty服务器和客户端如何连接的过程.我也理解解码器和编码器在理论上是如何工作的.但这是我仍然想要了解的内容.

我的服务器进程:

Server boots up -> Client starts
Client requests connection -> Server accepts
Server instructs client connection is good -> Client continues to the login screen
(Ignoring any type of security protocol)
Client sends username and password over Channel
Server gets username and password checks it in the database or file
Server pushes -> yes or no
if yes Server sends player stats
if no Server creates new player
Run Code Online (Sandbox Code Playgroud)

在那个过程之后,我知道我需要一个世界处理程序,以便每个人都能近乎实时地看到更新.现在我不知道如何为这些东西实现解码器.

我真的希望看到一些例子,说明如何实施它们.最好是一些说明....注意:我不是说为我解决这个问题但是告诉我如何处理不同的信息.最佳做法和标准请....

netty

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

netty ×2

client ×1

connection ×1

java ×1

tcp ×1