我有五个域服务器,我想在我的android应用程序中使用java套接字编程连接到该服务器。
这是我的服务器
“ ANY1.g %%%%%%%%%。dk”
“ ANY2.g %%%%%%%%%。dk”
“ ANY3.g %%%%%%%%%。dk”
“ ANY4.g %%%%%%%%%。dk”
“ ANY5.g %%%%%%%%。dk”
因此,当我通过运行提示符中的ping命令ping到任何服务器时,它工作正常并得到响应。
但是,当我尝试使用此服务器创建套接字时,会显示“未知主机”错误。
这是我的代码:
try
{
hostname = "ANY SERVER DOMAIN NAME";
port = ANY;
socket = new Socket(hostname, port);
socket.setReceiveBufferSize(2048);
socket.setKeepAlive(true);
Log.e("SOCKET",socket.isConnected());
}
catch(Exception e)
{
Log.e("ERROR",e.getMessage());
}
代码是否有问题或任何建议将被接受?
清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.siliconithub.android.greencall"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".GreenCall"
android:label="@string/title_activity_green_call"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RecentCallActivity"
android:label="@string/title_activity_recentcall"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".RecentCallDetailActivity"
android:label="@string/title_activity_recentcall_detail"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".FavouritesActivity"
android:label="@string/title_activity_fovourites"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".ContactsActivity"
android:label="@string/title_activity_contacts"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".ContactDetailsActivity"
android:label="@string/title_activity_contactdetails"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".TelnetServerActivity"
android:label="@string/title_activity_telnetserver"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".SettingActivity"
android:label="@string/title_activity_setting"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black" >
</activity>
<activity
android:name=".SearchContactsActivity"
android:label="@string/title_activity_search_contacts"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<service
android:name=".UpdateStatusService"
android:label="@string/title_service_update_status"
android:icon="@drawable/ic_app_icon"
android:enabled="true" />
<receiver android:name=".PhoneStateBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
谢谢。
最佳答案
import java.net.Socket;
public class Main {
public static void main(String[] args) {
try {
Socket socket = new Socket("%%%%.%%%%%.dk", PORT NUMBER);
socket.setReceiveBufferSize(2048);
socket.setKeepAlive(true);
System.out.println("socket " + socket.isConnected());
} catch (Exception e) {
System.out.println("error " + e.getMessage());
}
}
}
这返回
socket true
在Java中(没有Android)。它也可以在Android中使用(sherpya已对其进行了测试-请参阅下面的评论)。我想您的权限有问题。你能告诉我们你的清单吗?