我有五个域服务器,我想在我的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>
谢谢。
答案 0 :(得分:2)
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已对其进行了测试 - 请参阅下面的评论)。我猜您的权限有问题。你能告诉我们你的清单吗?
答案 1 :(得分:1)
最好的方法是使用InetAddress
+ SocketAddress
解析主机,然后使用套接字连接到主机。
尝试:
InetAddress addr = InetAddress.getByName("ANY1.g%%%%%%%.dk");
SocketAddress sockaddr = new InetSocketAddress(addr, 443);
socket = new Socket();
socket.connect(sockaddr, timeoutint)