为什么我无法通过socket连接android和PC?

时间:2016-05-14 15:54:58

标签: java android c++ sockets tcp

我刚刚启动了我的第一个Android应用程序并遇到了一些麻烦。我已经在Qt中用C ++编写了服务器代码。从现在开始我使用另一个控制台应用程序作为客户端,一切正常。现在我试图让我的Android(Android Studio)上的客户端部分。我希望在我按下按钮时连接客户端。只有连接 - 没有数据交换等。当客户端(android)连接服务器时应该显示通信。 这是我的服务器如何连接func。看起来像:

 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;
}

更新: /我在评论/

中更改了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 个答案:

答案 0 :(得分:1)

Most probably您的应用程序没有访问网络所需的权限。尝试添加

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

<manifest>

中的AndroidManifest.xml标记内