これは少し長いので、質問から始めましょう:Androidフォンで(プライベートネットワーク上ではなく)ソケットをリンクするためのIPを取得するにはどうすればよいですか?
また、ポートが電話ISPによってブロックされているかどうかを確認するにはどうすればよいですか?
もう少し情報:
地図上にユーザーの場所を表示するプログラムがあり、ユーザーをクリックしてチャットを開始できます。ソケット接続をテストしましたが、2つのエミュレーターで正常に動作していましたが、電話で試したところ、ソケットをリンクできませんでした。
時間外の例外:
NotificationChat.ChatSocket = new Socket(serverAddr, 5000);
そして、私の最も良い推測は、サーバー(別名電話1)のIPが正しくないか、ポートがブロックされているか使用中である可能性があります。
電話のIPを取得する2つの方法を試しました。
public static String getLocalIpAddress() {
try {
Socket socket = new Socket("www.google.com", 80);
Log.i("iptest", socket.getLocalAddress().toString().substring(1));
String ip=socket.getLocalAddress().toString().substring(1);
socket.close();
return ip;
} catch (Exception e) {
Log.i("", e.getMessage());
return "exception in get ip";
}
/*
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("b2264", ex.toString());
}
return null; */
}
私が得たIPは10.227.130.191でした。覚えていれば、これはクラスAのローカルIPです。
サーバー側:
while(flag==1)
{
if(ss==null)
{
try {
ss = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Log.d("thread","chatnotifiction befor ss accpect");
Socket NotAvilabale=null;
NotAvilabale = ss.accept();
if(ChatSocket!=null)
{
Log.d("test55","not avilable");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(NotAvilabale.getOutputStream())),true);
out.println("notav");
NotAvilabale.close();
continue;
}
ChatSocket=NotAvilabale;
Log.d("thread","chatnotifiction after ss accpect");
CharSequence contentText = "someone wants to talk to you";
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, notificationIntent, 2);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFI_ID, notification);
} catch (IOException e) {
Log.d("chat notifi io exception","chat notifi io exception ");
e.printStackTrace();
}catch (Exception e) {
Log.d("chat notifi Exception 2","chat notifi Exception 2 ");
// TODO: handle exception
}
}
ソケットの経験はあまりありません。初めて使用します。皆さんの1人がもう少し経験を積んで、私を助けてくれることを願っています。
よろしくお願いします(ちりめん英語でごめんなさい)。