2014-01-22 47 views
1

我正在製作一個Android應用程序,我需要建立與本地IP地址的連接 我搜索了很多並找到了相同的示例代碼,我試圖在我的代碼中實現與Android應用程序中的本地IP地址連接時出錯

http://android-er.blogspot.in/2011/01/simple-communication-using.html

http://www.pixelstech.net/article/1368328614-Android-socket-programming-example

但問題是這樣的,我無法連接到IP地址不帶有10.19.21.128 和端口號8221

我找不到我失蹤的地方。如果有人以前做過這個或有想法或任何有Android應用程序和IP地址之間的通信的示例,那麼我會感謝他。

注意我只需要發送一個測試msg hi到IP地址。

感謝 圖莎爾

+0

您的Android設備上安裝connectBot,並嘗試使用它連接到指定的IP和端口。結果可能會提供一個提示。 – alfasin

+0

U的意思是說,如果我檢查模擬器,它將無法工作。目前我正在測試Android模擬器。如果你有相同的示例代碼或Url中實現相同的東西,Plz讓我知道。謝謝 – user3221976

+0

@mani,如果可能,給出完整的代碼。這是一個不完整的朋友。 – user3221976

回答

1

客戶

class ClientThread implements Runnable { 

    @Override 
    public void run() { 
     try { 
      socket = new Socket(); 
      InetSocketAddress socketAddress = new InetSocketAddress(SERVER_IP, SERVERPORT); 
      socket.connect(socketAddress); 


      CommunicationThread commThread = new CommunicationThread(socket); 
      new Thread(commThread).start(); 

     } catch (UnknownHostException e1) { 

      e1.printStackTrace(); 
     }catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    }  
} 
class CommunicationThread implements Runnable { 

    private Socket clientSocket; 
    private BufferedReader input; 

    public CommunicationThread(Socket clientSocket) { 
     this.clientSocket = clientSocket; 
     try { 
      this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    public void run() { 

     while (!Thread.currentThread().isInterrupted()) { 


      try { 
       final String read1 = input.readLine(); 
       updateConversationHandler.post(new updateUIThread(read1));                          


         } catch (IOException e) { 
          e.printStackTrace(); 

         } 
     } 
    } 
} 

class updateUIThread implements Runnable { 
    private String msg; 

    public updateUIThread(String str) { 
     this.msg = str; 
    } 
    @SuppressLint("SdCardPath") 
    @Override 
    public void run() { 
     text1.setText(text1.getText().toString()+"Server Says: "+ msg + "\n"); 

     } 

} 

服務器

class ServerThread implements Runnable { 

    public void run() { 
     socket = null; 
     try { 
      serverSocket = new ServerSocket(SERVERPORT); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (!Thread.currentThread().isInterrupted()) { 
      try { 
       socket = serverSocket.accept(); 

       CommunicationThread commThread = new CommunicationThread(socket); 
       new Thread(commThread).start(); 


       }catch (IOException ioException) { 
        ioException.printStackTrace();     

       } 
     } 
    } 
} 

class CommunicationThread implements Runnable { 


    private Socket clientSocket; 

    private BufferedReader input; 

    public CommunicationThread(Socket clientSocket) { 

     this.clientSocket = clientSocket; 

     try { 
      this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));       

     } catch (IOException e) { 
      e.printStackTrace(); 
      new Thread(new ServerThread()).start(); 
     } 
    } 

    public void run() { 


     while (!Thread.currentThread().isInterrupted()) { 

      try { 

       final String read1 = input.readLine(); 

        updateConversationHandler.post(new updateUIThread(read1));          

      } catch (IOException e) { 
       e.printStackTrace(); 

      } 
     } 
    } 

} 

class updateUIThread implements Runnable { 
    private String msg; 

    public updateUIThread(String str) { 
     this.msg = str; 
    } 

    @Override 
    public void run() { 
      text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");   
    } 
} 
相關問題