2013-04-03 47 views
0

我有一個簡單的UDP服務器運行在一個設備上,當一個客戶端想要連接時,它向服務器發送一條消息「connect」,然後服務器將地址,並開始發送數據到該地址,現在當另一個客戶端想要加入時,該客戶端發送消息「連接」,但有時沒有任何反應,這就像客戶端想加入消息永遠不會被服務器接收,我將如何修復這個?另外這裏是服務器多個UDP客戶端發送數據包到一個主機不起作用

protected String doInBackground(String... params) { 
    boolean run = true; 
    String data = ""; 
    DatagramPacket packet = null; 
    boolean position = false; 
    while(run) 
    { 
     if(data.equalsIgnoreCase("")) 
     { 

     } 

     //Send some data 
     if(data.equalsIgnoreCase("connect") && wait == true) 
     { 
      Log.d(TAG, "Someone wants to connect"); 
      //Increase the total players by 1 
      players = players + 1; 
      //Notify to the host (client) something has change 
      //notify client 
      //Send a message to the client with the ID 
      byte[] bufer = new byte[256]; 
      //Send a message "connect" to the host 
      String msg = Integer.toString(players); 
      int msgLength = msg.length(); 
      bufer = msg.getBytes(); 
      InetAddress address; 
      //Default ip address of the host 
      //Take the address from the packet 
      addresses.add(packet.getAddress()); 
      Log.d(TAG, "Address is " + addresses.get(addresses.size() - 1)); 
      address = addresses.get(addresses.size() - 1); 
      DatagramPacket p = new DatagramPacket(bufer, bufer.length , address, port); 
      //Send packet 
      try 
      { 
       socket.send(p); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 

      wait = false; 
     } 

     if(wait == true && position == true) 
     { 
      position = false; 
      wait = false; 
     } 

     for(int i = 0;i < positions.length; i++) 
     { 
      if(positions[i] != null) 
      { 
      //Log.d(TAG, "X and Y position of asset:"+i+", is:"+ positions[i]); 
      } 
     } 

     //Needs to try and reteive data... 
     if(wait == false) 
     { 
      //Log.d(TAG, "Waiting to retreive data"); 
      byte[] buf = new byte[256]; 
      packet = new DatagramPacket(buf, buf.length); 
      try 
      { 
       socket.receive(packet); 
       wait = true; 
      } 
      catch (IOException e) 
      { 
       Log.d(TAG, "Error with receiving data"); 
       e.printStackTrace(); 
      } 

      data = new String(buf, 0, packet.getLength()); 
      //Log.d(TAG, "Data received from :" + packet.getAddress() + ", holds this value: " + data); 
      String[] dataStrings = data.split(":"); 
      if(dataStrings[0].equalsIgnoreCase("position")) 
      { 
       position = true; 
      } 
     } 



     //Log.d(TAG, "Data received was :" + data); 

     /*try 
     { 
      Thread.sleep(25); 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      Log.d(TAG, "Error with trying to sleep"); 
      e.printStackTrace(); 
     }*/ 
    } 
    Log.d(TAG, "Error with while run value"); 
    return "finished"; 
} 

客戶端代碼連接到服務器

public void connectToServer() 
{ 
    //Send a connect message to the server 
    try { 
     //Create a socket 
     socket = new DatagramSocket(port); 
     byte[] bufer = new byte[256]; 
     //Send a message "connect" to the host 
     String msg = "connect"; 
     int msgLength = msg.length(); 
     bufer = msg.getBytes(); 
     InetAddress address; 
     //Default ip address of the host 
     address = InetAddress.getByName("192.168.1.59"); 
     DatagramPacket p = new DatagramPacket(bufer, bufer.length , address, port); 
     //Send packet 
     socket.send(p); 

    } catch (UnknownHostException e2) { 
     Log.d(TAG, "Unknown host"); 
     e2.printStackTrace(); 
    } catch (SocketException e) { 
     Log.d(TAG, "Socket problem"); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     Log.d(TAG, "I/O problem"); 
     e.printStackTrace(); 
    } 

    //Receive the message back 
    byte[] buf = new byte[256]; 
    DatagramPacket packet = new DatagramPacket(buf, buf.length); 
    //Try to receive a packet from the server 
    try 
    { 
     Log.d(TAG, "Waiting for data"); 
     socket.receive(packet); 
    } 
    //Error 
    catch (IOException e) 
    { 
     Log.d(TAG, "Error with receiving data"); 
     e.printStackTrace(); 
    } 

    //Convert the packet to a string 
    String data = new String(buf, 0, packet.getLength()); 

    //Use the string to find out what ID this client is 
    ID = Integer.parseInt(data); 
    //Setup the client game 
    setUpClient(); 
} 

我應該嘗試並獲得connectToServer方法不斷地嘗試,如果事情是不正確的代碼?

帆布

回答

0

客戶端不第一個「連接」字符串之後發送任何內容到服務器。因此,服務器在該行永遠等待:

socket.receive(packet); // <- will never return 
wait = true; 

不管怎麼說,你應該如服務器的recv()循環避免使用的AsyncTask長時間運行的任務,並重構服務器代碼的不同情況(收到「連接'消息或來自已知客戶端的消息)。

+0

我現在正在處理這個問題,並且我在Android設備上使用AsyncTask,其中一些在ver4及更高版本上運行,線程無法通過GUI使用,我當然會檢查代碼一旦我得到一切工作正常,只是想獲得東西的工作:) – Canvas

+0

oohh也客戶端將不斷髮送數據到服務器,一旦他們已經連接,我沒有顯示代碼,因爲這不是問題 – Canvas

+0

那麼什麼正在發生的事情是,已經連接的客戶端正在保持套接字接收繁忙,並且新客戶端無法發送連接,因爲它很忙。 – Canvas

相關問題