這有點長,所以我將從這個問題開始:如何在Android手機上使用IP鏈接套接字(而不是專用網絡)?使用套接字的兩部手機之間的Android安卓聊天
我該如何檢查一個端口是否被手機ISP阻止?
多一點信息:
我有一個程序,在地圖上顯示用戶的位置,你可以點擊它們,開始聊天。我已經測試了套接字連接,並且它在2個仿真器上工作正常,但是當我在手機上嘗試時,它無法連接套接字。
出時間異常的:
NotificationChat.ChatSocket = new Socket(serverAddr, 5000);
而且我最好的猜測是服務器(又名手機1)的IP是不正確的,或者也許是端口被阻塞或在使用中。
我嘗試2種方式來獲得手機IP:
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
}
}
我沒有與插座很多經驗。這是我第一次使用它們。我希望你們中的一個有更多的經驗並能幫助我。
在此先感謝(抱歉英語crape)。
有點睡覺,但我只是跑到相同的事情馴鹿Lidgren任何人都知道,如果這將解決我的問題? – user1246950