如何在Java中的插座上连续来回发送对象?

发布于 2025-01-20 09:19:12 字数 4473 浏览 1 评论 0原文

我在这里尝试做的是创建一个将成为基本游戏的应用程序,首先我必须使网络功能功能。我正在努力在服务器和客户端之间来回发送对象。我要实现的设计是2个过程,每个过程有2个线程,主线程,然后是一个侦听器线程。我希望侦听器线程聆听传入的对象,因为这将用于事件总线。目前,为了使其正常工作,我只使用一个简单的消息类,该类别包含一个称为文本的单字符串字段。我遇到的问题是,客户端侦听器线程似乎没有启动,并且对象永远不会发送任何一种方式。在这里真正挣扎的学习网络编程,任何帮助都非常感谢。

服务器端

package Server;

import java.net.*;
import java.util.List;

import Utilities.Message;

import java.io.*;

public class BattleshipServer 
{
    
    public static void executeThreadedServer(int port) throws Exception
    {
        ServerSocket server = new ServerSocket(port);
        System.out.println("Awaiting connection");
        Socket socket = server.accept();
        System.out.println("Connection established");
        
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        
        //Start the listener thread
        ServerListener sListener = new ServerListener(kb, out, in);
        sListener.run();
        
        Message msg = new Message("temp");
        
        while(!msg.getText().equalsIgnoreCase("exit"))
        {
            msg = new Message(kb.readLine());
            out.writeObject(msg);
            out.flush();
        }
        
    }
}

------------------------------------------------------------------------------------------------------------

package Server;

import java.io.BufferedReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Utilities.Message;

public class ServerListener implements Runnable 
{

    private BufferedReader keyBoard;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    
    public ServerListener(BufferedReader keyboard, ObjectOutputStream out, ObjectInputStream in)
    {
        this.keyBoard = keyboard;
        this.out = out;
        this.in = in;
    }
    
    @Override
    public void run() 
    {
        System.out.println("Server listener started");
        try {
            while(true)
            {
                Message msg;
                
                while((msg = (Message)this.in.readObject()) != null)
                {
                    System.out.println(msg.getText());
                }
                
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

    }

}

客户端

package Client;

import java.net.*;
import java.util.ArrayList;
import java.util.List;

import Utilities.Message;

import java.io.*;

public class BattleshipClient 
{
    
    public static void executeThreadedClient(String address, int port) throws Exception
    {
        Socket socket = new Socket(address, port);
        
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        
        ClientListener cListener = new ClientListener(kb,out,in);
        cListener.run();
        
        Message msg = new Message("temp");
        while(!msg.getText().equalsIgnoreCase("exit"))
        {
            msg = new Message(kb.readLine());
            out.writeObject(msg);
            out.flush();
        }
        socket.close();
    }
    
}

------------------------------------------------------------------------------------------------------------

package Client;

import java.io.BufferedReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Utilities.Message;

public class ClientListener implements Runnable 
{
    
    private BufferedReader keyBoard;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    
    public ClientListener(BufferedReader keyboard, ObjectOutputStream out, ObjectInputStream in)
    {
        this.keyBoard = keyboard;
        this.out = out;
        this.in = in;
    }

    @Override
    public void run() 
    {
        System.out.println("Client listener started");
        try
        {
            while(true)
            {
                Message msg = (Message)in.readObject();
                System.out.println(msg.getText());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

What I am trying to do here is create an application that will be a basic game, and first I have to get the networking functional. I'm struggling to send objects back and forth between server and client. The design I am trying to achieve is 2 processes with 2 threads each, the main thread and then a listener thread. I want the listener thread to listen for incoming objects, as this will be used for an event bus. Currently to get it working I'm using just a simple message class which holds a single string field called text. The issue I'm having is that the client listener thread doesn't seem to start, and the objects never get sent either way. Really struggling learning network programming here, any help is much appreciated.

Server side

package Server;

import java.net.*;
import java.util.List;

import Utilities.Message;

import java.io.*;

public class BattleshipServer 
{
    
    public static void executeThreadedServer(int port) throws Exception
    {
        ServerSocket server = new ServerSocket(port);
        System.out.println("Awaiting connection");
        Socket socket = server.accept();
        System.out.println("Connection established");
        
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        
        //Start the listener thread
        ServerListener sListener = new ServerListener(kb, out, in);
        sListener.run();
        
        Message msg = new Message("temp");
        
        while(!msg.getText().equalsIgnoreCase("exit"))
        {
            msg = new Message(kb.readLine());
            out.writeObject(msg);
            out.flush();
        }
        
    }
}

------------------------------------------------------------------------------------------------------------

package Server;

import java.io.BufferedReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Utilities.Message;

public class ServerListener implements Runnable 
{

    private BufferedReader keyBoard;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    
    public ServerListener(BufferedReader keyboard, ObjectOutputStream out, ObjectInputStream in)
    {
        this.keyBoard = keyboard;
        this.out = out;
        this.in = in;
    }
    
    @Override
    public void run() 
    {
        System.out.println("Server listener started");
        try {
            while(true)
            {
                Message msg;
                
                while((msg = (Message)this.in.readObject()) != null)
                {
                    System.out.println(msg.getText());
                }
                
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

    }

}

Client Side

package Client;

import java.net.*;
import java.util.ArrayList;
import java.util.List;

import Utilities.Message;

import java.io.*;

public class BattleshipClient 
{
    
    public static void executeThreadedClient(String address, int port) throws Exception
    {
        Socket socket = new Socket(address, port);
        
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        
        ClientListener cListener = new ClientListener(kb,out,in);
        cListener.run();
        
        Message msg = new Message("temp");
        while(!msg.getText().equalsIgnoreCase("exit"))
        {
            msg = new Message(kb.readLine());
            out.writeObject(msg);
            out.flush();
        }
        socket.close();
    }
    
}

------------------------------------------------------------------------------------------------------------

package Client;

import java.io.BufferedReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Utilities.Message;

public class ClientListener implements Runnable 
{
    
    private BufferedReader keyBoard;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    
    public ClientListener(BufferedReader keyboard, ObjectOutputStream out, ObjectInputStream in)
    {
        this.keyBoard = keyboard;
        this.out = out;
        this.in = in;
    }

    @Override
    public void run() 
    {
        System.out.println("Client listener started");
        try
        {
            while(true)
            {
                Message msg = (Message)in.readObject();
                System.out.println(msg.getText());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2025-01-27 09:19:12

尽管您的clientListener和ServerListeners正在实现可运行的功能,但它们并未在单独的线程中运行。在您的实现中,它只是另一个函数调用,因此在“侦听器”()之后的代码从未被调用。

因此,而不是这样做:

ClientListener cListener = new ClientListener(kb,out,in);
cListener.run();

您需要做类似的事情:

Thread clientThread = new Thread(new ClientListener(kb,out,in));
clientThread.start(); 

在服务器端,您需要做类似的事情。

Although your ClientListener and ServerListeners are implementing Runnable, they are not run in a separate thread. In your implementation, it is just another function call and therefore the code after the listener.run() never gets called.

So instead of doing:

ClientListener cListener = new ClientListener(kb,out,in);
cListener.run();

You need to do something like:

Thread clientThread = new Thread(new ClientListener(kb,out,in));
clientThread.start(); 

And on the server side you need to do something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文