2012-11-25 32 views
1

我有一臺運行在我的筆記本電腦上的簡單Java服務器,打開ServerSocket。 Android的簡單客戶端嘗試連接到此服務器(使用我的筆記本電腦的IP地址和我在ServerSocket中指定的端口),併發送一個字符串。客戶端掛在:Android手機與PC通信:手機找不到PC

client = new Socket(IP_ADDRESS, DEST_PORT);

我使用的是三星Galaxy S3連接到我的筆記本電腦。請注意,我嘗試從AsyncTask的客戶端建立連接。

我一直在琢磨這一段時間,並有感覺我錯過了一些簡單的東西。

所以問題是:如何讓我的android手機識別/看到PC?提前致謝!

這裏是我的Java服務器代碼:

//進口等

public class Main 
{ 
    private static final int PORT = 4444; 

    private static ServerSocket serverSocket; 
    private static Socket clientSocket; 
    private static InputStreamReader inputStreamReader; 
    private static BufferedReader bufferedReader; 
    private static String message; 

    public static void main(String[] args) 
    { 
     try 
     { 
      serverSocket = new ServerSocket(PORT, 0, InetAddress.getLocalHost()); 

      System.out.println("IP: " + serverSocket.getInetAddress() + " Port: " + serverSocket.getLocalPort()); 

     } catch (IOException e) 
     { 
      System.out.println("Could not listen on port: 4444"); 
     } 

     System.out.println("Server started. Listening to the port 4444"); 

     while (true) 
     { 
      try 
      { 
       clientSocket = serverSocket.accept(); // accept the client connection 
       inputStreamReader = new InputStreamReader(clientSocket.getInputStream()); 
       bufferedReader = new BufferedReader(inputStreamReader); // get the client message 
       message = bufferedReader.readLine(); 

       System.out.println(message); 
       inputStreamReader.close(); 
       clientSocket.close(); 

      } catch (IOException ex) 
      { 
       System.out.println("Problem in message reading"); 
      } 
     } 
    } 
} 

這裏是我的Android客戶端代碼:

//進口等

public class SimpleClientActivity extends Activity 
{ 
    private EditText textField; 
    private Button button; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     textField = (EditText) findViewById(R.id.editText1); // reference to the text field 
     button = (Button) findViewById(R.id.button1); // reference to the send button 

     // Button press event listener 
     button.setOnClickListener(new View.OnClickListener() 
     { 

      public void onClick(View v) 
      { 
       new ConnectToServerTask().execute(textField); 
      } 
     }); 
    } 
} 

這裏我的AsyncTask代碼:

// Imports etc

public class ConnectToServerTask extends AsyncTask<View, Integer, Socket> 
{ 
    private static final String IP_ADDRESS = "192.168.56.1"; // Toshiba laptop 
    private static final int DEST_PORT = 4444; 

    private EditText mTextField; 

    /** 
    * Store provided views (used later in onPostExecute(...)). 
    * 
    * Create socket to communicate with server (blocking call). 
    */ 
    protected Socket doInBackground(View... params) 
    { 
     // Store provided views. 
     if (params.length != 1) 
      throw new IllegalArgumentException(); 

     mTextField = (EditText) params[0]; 


     // Create socket. 
     Socket client = null; 

     try 
     { 
      client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server 
     } catch (UnknownHostException e) 
     { 
      e.printStackTrace(); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     return client; 
    } 

    /** 
    * Write to server. 
    */ 
    protected void onPostExecute(Socket client) 
    { 
     try 
     { 
      PrintWriter printwriter; 
      String messsage; 

      messsage = mTextField.getText().toString(); // get the text message on the text field 
      mTextField.setText(""); // Reset the text field to blank 

      printwriter = new PrintWriter(client.getOutputStream(), true); 
      printwriter.write(messsage); // write the message to output stream 

      printwriter.flush(); 
      printwriter.close(); 

      client.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

你是否得到任何打印/例外?你可以在服務器上的'accept()'之後打印一個打印嗎? – Danpe

+1

檢查筆記本電腦的防火牆。代碼看起來應該可以工作 – zapl

+0

同意zapl - 確認任何防火牆軟件都允許訪問偵聽器端口。 – Squonk

回答

0

感謝@zapl,檢查PC的IP地址。當正確的地址是192.168.1.56時有192.168.56.1。