2016-07-22 32 views
1

我試圖建立一個Android應用程序,其中兩個手機將能夠發送短信給對方而沒有數據或蜂窩連接,而是使用WiFi連接。我找到here,我需要首先創建一個p2p(wifidirect)連接,然後嘗試使用服務器套接字等待客戶端連接併發送數據。現在,我可以建立p2p連接,但我不能使用套接字來傳輸數據。在客戶端和服務器端,我在我的onPostExecute()方法中得到空結果。我應該如何建立服務器和客戶端?無法連接到服務器套接字來傳輸數據android

我在MainActivity中創建了每個類的對象來啓動客戶端和服務器端。它們都以背景方法執行。

Client task = new Client(MainActivity.this,d.deviceAddress); 
task.execute(); 

sever task = new server(MainActivity.this); 
       task.execute(); 

這裏是我的服務器端:

public class server extends AsyncTask<String, String, String>{ 

    InputStream inputstream ; 
    Socket client; 
    ByteArrayOutputStream result; 
    byte[] buffer; 
    private Context context; 
    public static InetAddress address; 
    public static boolean connected; 

    public server(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     ServerSocket serverSocket; 

     //byte[] ipAddress={'5','4','3','1'}; 

     try { 

       address = InetAddress.getByName("hazel");// i tried to use getByAddress(ipAddress) as well ,no change 
       serverSocket = new ServerSocket(8888,10,address); 
       // serverSocket.setSoTimeout(10000); 
       client = serverSocket.accept(); 
       connected =true; 


      } catch (IOException e) { 
       return null; 
      } 
     return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      if (result != null) { 
       super.onPostExecute(result); 
       Toast.makeText(context, "result is "+result,Toast.LENGTH_LONG).show(); 
       } 
       Toast.makeText(context, " is someone connected "+ connected ,Toast.LENGTH_LONG).show(); 
     } 

} 

下面是客戶端:

public class Client extends AsyncTask<String, String, String> { 

    Context ctx; 
    String deviceAddress ; 
    String sek; 
    boolean isresolved = false; 
    InetAddress inetaddress; 


     Client task = new Client(MainActivity.this,d.deviceAddress); 
     task.execute(); 
    public Client(MainActivity ctx, String deviceAddress) { 

     this.ctx = ctx; 
     this.deviceAddress = deviceAddress; 

    } 


    @Override 
    protected String doInBackground(String... arg0) { 

       String host="hazel"; 
       int port=8888; 

       int len; 
       Socket socket = new Socket(); 
       byte buf[] = new byte[1024]; 

       try { 

         socket.bind(null); 

        // byte[] ipAddress={'5','4','3','1'}; 

        inetaddress = InetAddress.getByName("hazel"); 
        InetSocketAddress address = new InetSocketAddress(inetaddress,8888); 


        isresolved = address.isUnresolved(); 
        // the isresolved will get false value after executing the code 
        // i think it means that adress has no problem 


        socket.connect(address,1000); 



       } catch (SecurityException e) { 
        Log.v("Exception" , "is " + e); 

       } catch (Exception e) { 
        Log.v("Exception" , "is " + e); 

       } 

       finally { 
        if (socket != null) { 
         if (socket.isConnected()) { 
          try { 
           socket.close(); 
          } catch (IOException e) { 

           Log.v("Exception " , "is " + e); 
          } 
         } 
        } 
       }  

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (result != null) {    
      super.onPostExecute(result); 
      Toast.makeText(ctx, "clientside , result : "+result ,Toast.LENGTH_LONG).show(); 
      }else Toast.makeText(ctx, "result is null :(" , result : "+result ,Toast.LENGTH_LONG).show(); 
    } 

} 

當我運行服務器端的onPostExecute將與空結果被調用。我期待在連接到服務器的人之後調用onPostExecute。

在客戶端,我在onPostExecute方法上得到一個空結果,同時也是未解析的方法返回false。

我需要首先連接到服務器發送數據,所以任何人都可以指出我的邏輯有什麼問題?我嘗試以不同的方式創建套接字(例如端口,帶主機名的端口,帶有IP地址的端口)並放入serverSocket.accept(),但它不起作用。

回答

0

以及它很傷心,我不得不回答我的問題,但在p2p連接ip永遠是恆定的。其「192.168.49.1」。所以我剛剛創建了一個如下的套接字:

clientSocket = new Socket(「192.168.49.1」,9889);

它不管你是否在doInBackgroundmethod或新線程中運行你的代碼,但端口必須相同

0
  1. doInBackground不會返回任何東西,但null,所以onPostExecute不看別的
  2. 如果您希望服務器持續運行和偵聽連接,你需要一個線程/服務無限運行和接受連接。 AsyncTask旨在運行短期任務。
  3. 例如,您可能想要查看更多有關回調的反應式編程方法,例如onClientConnect()方法。
相關問題