2016-05-14 167 views
0

我剛開始我的第一個Android應用程序,並有一些麻煩。我已經在Qt中用C++編寫了服務器代碼。由於現在我正在使用另一個控制檯應用程序作爲客戶端,並且一切正常。現在我試圖讓我的android(Android Studio)上的客戶端部分。當我按下按鈕時,我想要連接客戶端。只有連接 - 無數據交換等當客戶端(android)將連接服務器應顯示通信。 這裏是我的服務器連接功能。看起來像:爲什麼我無法通過套接字連接android和PC?

SUCCESSFUL = WSAStartup(MAKEWORD(2,1), &WinSockData); 
sock_CONNECTION= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
memset(& ADDRESS, 0, sizeof(ADDRESS)); 
ADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1"); 
ADDRESS.sin_family = AF_INET; 
ADDRESS.sin_port = htons(9090); 

void Server::Connect() 
{ 
bind(sock_CONNECTION, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS)); 
listen(sock_CONNECTION,1); 
SOCKET accept_Socket =0; 
while(accept_Socket==0) 
{ 
    accept_Socket = accept(sock_CONNECTION, NULL, NULL); 
} 
std::cout << "Client connected" << std::endl; 
sock_CONNECTION=accept_Socket; 
} 

UPDATE: /我改變了IP作爲你的意見/

客戶端的代碼表示。沒有致命的錯誤,只是應用程序無所事事。 代碼:

Socket socket; 
    public void onClick(View v) { 
       try { 
        socket = new Socket("192.168.0.227", 9090); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

錯誤:

Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa23f380, error=EGL_SUCCESS 
java.net.SocketException: socket failed: EACCES (Permission denied) 
    at libcore.io.IoBridge.socket(IoBridge.java:619) 
    at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198) 
    at java.net.Socket.startupSocket(Socket.java:584) 
    at java.net.Socket.tryAllAddresses(Socket.java:128) 
    at java.net.Socket.<init>(Socket.java:178) 
    at java.net.Socket.<init>(Socket.java:150) 
    at com.example.nataliah.client_testapp.Client_Connection$1.onClick(Client_Connection.java:26) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied) 
    at libcore.io.Posix.socket(Native Method) 
    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282) 
    at libcore.io.IoBridge.socket(IoBridge.java:604) 

更新2加入到網絡我收到這些錯誤+舊的許可後:

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) 
                         at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110) 
                         at libcore.io.IoBridge.connectErrno(IoBridge.java:137) 
                         at libcore.io.IoBridge.connect(IoBridge.java:122) 
                         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 
                         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163) 
+1

這是因爲,'127.0.0.1'爲localhost的IP地址。在Java代碼中編輯您的PC的地址。 –

+0

也請在問問題的同時,添加你的堆棧跟蹤。 –

+0

感謝您的回覆,我瞭解這個問題,但我仍然不知道應該在哪個地址更改它... – Yyomi

回答

1

Most probably您的應用程序不具有訪問網絡所需的權限。嘗試添加

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

<manifest>標籤內你AndroidManifest.xml

相關問題