2016-11-01 4 views
0

내 노트북에서 공유하는 Wi-Fi에 Android 기기를 연결했습니다. 안드로이드 앱에서 IP 주소를 입력하고 "확인"을 클릭하면 Wireshark (패킷 스니퍼)에서 주소로가는 패킷을 찾을 수 없습니다.왜이 Android Socket 프로그램이 작동하지 않습니까?

Android 프로젝트의 매니페스트에

을 추가했습니다.

<uses-permission android:name="android.permission.INTERNET"/>

안드로이드 코드 :

private boolean attemptOpenDoor(){ 

    // Store values at the time of the login attempt. 
    String studentId = mStudentIdView.getText().toString(); 
    String password = mPasswordView.getText().toString(); 

    final EditText et = new EditText(this); 
    new AlertDialog.Builder(this).setTitle("please input IP address") 
      .setIcon(android.R.drawable.ic_dialog_info).setView(et) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        IPAddr = et.getText().toString(); 
       } 
      }).setNegativeButton("cancel", null).show(); 

    try { 
     Socket socket = new Socket(); 
     socket.connect(new InetSocketAddress(IPAddr, 8866), 5000); 
     OutputStream ou = socket.getOutputStream(); 
     ou.write((studentId+password).getBytes("UTF-8")); 
     ou.close(); 
     socket.close(); 
    }catch (SocketTimeoutException aa) { 
     //连接超时 在UI界面显示消息 
     AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create(); 
     alertDialog.setMessage("服务器连接失败!请检查网络是否打开"); 
     alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     alertDialog.show(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return true; 
} 
+0

대단히 감사합니다. – bowwwww

답변

0

프로그램은 사용하여 소켓을 열려고하는 시점에 있기 때문에 작동하지 않습니다

socket.connect(new InetSocketAddress(IPAddr, 8866), 5000); 

대화는 여전히 최대이며 IPAddr의 값이 아직 설정되지 않았습니다. 사용자가 입력란에 올바른 입력을 입력하면 연결을 시작해야합니다.

또한 위의 방법은 연결이 설정되거나 5 초가 지나야 OnClickListener 내부로 직접 호출하는 것이 현명하지 않을 때까지 실행중인 스레드를 차단합니다. 연결이 진행되는 동안 UI 스레드를 차단하지 않으려면 AsyncTask 또는 유사하게 사용해야합니다.

+0

대단합니다! 고마워요! – bowwwww

0

당신은 "onclick을"후 연결 코드를 이동해야합니다.

new AlertDialog.Builder(this).setTitle("please input IP address") 
     .setIcon(android.R.drawable.ic_dialog_info).setView(et) 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       IPAddr = et.getText().toString(); 
       try { 
    Socket socket = new Socket(); 
    socket.connect(new InetSocketAddress(IPAddr, 8866), 5000); 
    OutputStream ou = socket.getOutputStream(); 
    ou.write((studentId+password).getBytes("UTF-8")); 
    ou.close(); 
    socket.close(); 
}catch (SocketTimeoutException aa) { 
    //连接超时 在UI界面显示消息 
    AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create(); 
    alertDialog.setMessage("服务器连接失败!请检查网络是否打开"); 
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 
    alertDialog.show(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
      } 
     }).setNegativeButton("cancel", null).show(); 
관련 문제