【问题标题】:Client/Server socket. Doesn't read or write客户端/服务器套接字。不读也不写
【发布时间】:2014-08-11 22:43:56
【问题描述】:

首先这里是我的可编译的projects

IDE = Netbeans

我在一个项目中有服务器套接字,在第二个项目中有客户端套接字。

ServerSocket 项目的代码片段:

showStatus(String status); is method which appends text to statusWindow (JTextArea);
Socket gameClientSocket,ObjectOutputStream gameoos and ObjectInputStream gameois is declared outside code fragment

代码:

 private void configureSockets() {
        try{
        properties.showStatus("GAME_THREAD-waiting someone to accept");
        gameClientSocket = gameSocket.accept();

        properties.showStatus("GAME_THREAD-Accepted");
        properties.showStatus("GAME_THREAD-getting outputsstreams");

        gameoos= new ObjectOutputStream(gameClientSocket.getOutputStream());
        gameoos.flush();
        properties.setGameStream(gameoos);


         properties.showStatus("GAME_THREAD-getting inputstreams");
         gameois=new ObjectInputStream(gameClientSocket.getInputStream());
         properties.showStatus("GAME_THREAD-testing connections ,\nwe must receive int 1 ");
         properties.showStatus("GAME_THREAD- received "+gameois.readInt());
         properties.showStatus("GAME_THREAD-tested");



        }catch(IOException ex){
            properties.showStatus(ex.getMessage());}
        }

以及初始化:

gameSocket = new ServerSocket(GAME_PORT);

ClientSocket项目的代码片段:

 System.out.println("GAME_THREAD-configuring gameSocket ");
                properties.showStatus("GAME_THREAD- configuring gameSocket ");
                if(gameSocket==null ){
                gameSocket = new Socket("localhost",GAME_PORT);
                System.out.println("GAME_THREAD- getting Streams");
                properties.showStatus("GAME_THREAD- getting Streams ");

                gameoos = new ObjectOutputStream(gameSocket.getOutputStream());
                gameoos.flush();
                gameois = new ObjectInputStream(gameSocket.getInputStream());

                properties.showStatus("GAME_THREAD-testing  sending  ");
                gameoos.writeInt(1);
                properties.showStatus("GAME_THREAD-seccessfully sent ");

                properties.showStatus("GAME_THREAD- setting Streams to gameWindow ");
                System.out.println("GAME_THREAD-setting Streams to gameWindow");
                properties.setGameStream(gameoos);
            }

最后是状态窗口:

GAME_THREAD - blocking game Window
GAME_THREAD- configuring gameSocket 
GAME_THREAD- getting Streams 
GAME_THREAD-testing  sending  
GAME_THREAD-seccessfully sent 
GAME_THREAD- setting Streams to gameWindow 

和服务器项目状态窗口:

GAME_THREAD-Accepted
GAME_THREAD-getting outputsstreams
GAME_THREAD-getting inputstreams
GAME_THREAD-testing connections ,
we must receive int 1 

问题:

我无法从 ObjectInputStream 读取数字(或者它没有写入),从不抛出异常,进程冻结并且不执行任何操作。我不知道我是否做错了什么。我搜索了整个网络,但找不到任何可用的答案。你能帮帮我吗?

更新:

gameoos.writeint(1);
gameoos.flush();

解决了问题

【问题讨论】:

    标签: java sockets objectinputstream objectoutputstream


    【解决方案1】:

    发生冻结是因为您尝试在主线程上进行网络连接,我相信这是一个糟糕的计划 - 您需要将它们放在第二个线程上。您可以使用来自 ExecutorService 的线程池,http://www.journaldev.com/1069/java-thread-pool-example-using-executors-and-threadpoolexecutor,也可以只运行自己的线程。

    线程需要 Runnable 接口的实现才能运行。

    public class MyThread implements Runnable
    {
        @Override
        public void run()
        {
            //do stuff
        }
    }
    
    public static void main(String[] args)
    {
        Thread thread = new Thread(new MyThread());
        thread.start();
        //...do other stuff and not end the app here
    }
    

    但我认为您不能像那样发送对象,您需要将它们转换为 byte[] 并重新组装它们:send a serializable object over socket

    我个人的建议是完全放弃 ObjectStreams,并使用允许按原样发送对象的框架,例如 KryoNet 框架: https://github.com/EsotericSoftware/kryonet

    使用它的示例代码如下:

      Server server = new Server();
      Kryo kryo = server.getKryo();
      kryo.register(float[].class);
      server.start();
      server.bind(2300, 2301);
      server.addListener(new Listener() {
       public void received(Connection connection, Object object)
       {
          if(object instanceof float[])
          {
            float[] array = (float[])object;
            for(int i = 0; i < array.length; i++)
            {
               System.out.println("" + array[i]);
            }
          }        
       }});
      Client client = new Client();
      Kryo kryo = client.getKryo();
      kryo.register(float[].class);
      client.addListener(new Listener() {
        public void connected(Connection connection)
        {
           connection.sendTCP(new float[] {5, 6, 7, 8});
        }
      };
      client.connect(5000, "127.0.0.1”, 2300, 2301);
    

    而且 KryoNet 已经在后台线程上运行网络,所以你根本不需要搞砸。

    【讨论】:

    • 感谢您的回答,请在顶部查看我的项目。这是高中项目,所以我是有限的。我必须使用套接字进行联网。我正在一个单独的线程中尝试这个。我只需要测试连接。
    猜你喜欢
    • 2016-02-08
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多