2014-06-21 75 views
0

首先这里是我编辑的projects客户端/服务器套接字。不读或写

IDE = Netbeans的

我有一个项目的ServerSocket和客户Socket的第二个项目。

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

在这里到底是状态的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读取数(或者它不写),异常不会被抛出,过程冻结,不做任何事情。我不知道我是否做错了什么。我搜索了整个网页,但找不到任何可用的答案。你可以帮帮我吗?

UPDATE:

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

解决发生

回答

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已经在后台线程上运行网络连接,所以你根本不需要搞砸。

+0

感谢您的答案,请检查我的项目在顶部。 这是一个很高的项目,所以我很有限。我必须使用 套接字进行网络连接。 我在单独的线程中尝试此操作。 我只需要测试连接。 – katamarani4

相关问题