客户/服务器套接字。不读或写

时间:2014-06-21 08:20:27

标签: java sockets objectinputstream objectoutputstream

首先,这是我的可编辑projects

IDE = Netbeans

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

ServerSocket Project的代码片段:

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 Project的代码片段:

 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);
            }

最后这里是状态Windows:

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();

解决了问题

1 个答案:

答案 0 :(得分:0)

发生冻结是因为你试图在我相信的主线程上进行网络连接,这是一个糟糕的计划 - 你需要将它们放在第二个线程上。您可以使用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已经在后台线程上运行网络,所以你根本不需要搞乱它。