简体   繁体   English

无法在Java和Android之间建立套接字连接

[英]Can't establish socket connection between Java and Android

I am currently trying to host a server socket on my android phone, and use my computer to connect to that one. 我目前正在尝试在我的android手机上托管服务器套接字,并使用我的计算机连接到该套接字。 There are several applications which already do so (for example Skype), so it has to be possible. 已经有几个应用程序正在执行此操作(例如Skype),因此必须可行。 The problem I am currently facing, is that I cannot connect to the server socket opened on my emulator. 我当前面临的问题是我无法连接到模拟器上打开的服务器套接字。 This is code I am running in Android Developer Tools emulator to open my server socket: 这是我在Android Developer Tools模拟器中运行的代码,用于打开服务器套接字:

public void run(){
    Log.i(TAG, "1");
    try{
      serverSocket = new ServerSocket(25555);
      Log.i(TAG, "2. Details: " + serverSocket);
      while(true)
      {
      clientSocket = serverSocket.accept(); //it keeps sticking here, and waiting for connections.
      Log.i(TAG, "3");

      in = new BufferedReader(
             new InputStreamReader(clientSocket.getInputStream()));
      out = new PrintWriter(clientSocket.getOutputStream(), true);

      String input = in.readLine();
      out.println("received: " + input);

      in.close();
      out.close();
      }
    } catch(Exception e) {
        Log.i(TAG, "ERROR:");
    e.printStackTrace();
    }
  }

and this is the code I am running in normal eclipse to connect to this server socket: 这是我在普通日食中运行以连接到此服务器套接字的代码:

try {
    socket = new Socket(-snip-,25555);
    System.out.println("Connection has been made");
  } catch (NumberFormatException e) {
    System.out.println("Connection wasn't made. Closing the application.");
  }

This just gives me this error: 这只是给我这个错误:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)

Now my question is, is it something I did wrong in my code, or is it some problem with the emulator? 现在我的问题是,这是我在代码中做错了吗,还是模拟器有问题?

By the way, I added those lines of code to my AndroidManifest.xml: 顺便说一下,我将这些代码行添加到了AndroidManifest.xml中:

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

Thanks in advance. 提前致谢。

Acording to the discussion, you have to connect a client to an Android Virtual Device Server. 根据讨论,您必须将客户端连接到Android虚拟设备服务器。 This means you need to know you Android Virtual Device's IP. 这意味着您需要知道您的Android虚拟设备的IP。 There are some ways to achieve this: 有一些方法可以实现此目的:

  1. The first is to create a network and then connect both your Android Device and your Computer/Client, and then they will have an IP. 首先是创建网络,然后将您的Android设备和计算机/客户端连接在一起,然后它们将具有IP。

  2. You can assign an static IP to the Android Device, in this case you should check this link . 您可以为Android设备分配一个静态IP,在这种情况下,您应该检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android无法建立URL连接 - android can't establish URL connection 无法在Java服务器和Android客户端之间建立连接 - Unable to establish a connection between a java server and an android client Java套接字聊天无法建立与端口的连接 - Java Socket Chat unable to establish connection to port 如何使用Java优先在没有外网IP的两台机器之间建立套接字连接? - How to establish socket connection between two machines with each has no out-net ip preferentially using java? 如何在Android Studio上使用SilverTunnel-NG建立与.onion URL的套接字连接? 有没有其他选择? - How can i establish a socket connection to an .onion url using SilverTunnel-NG on Android Studio? Is there any alternative? 如何在Play 2服务器和C#应用程序之间建立套接字连接? - How to establish a socket connection between a Play 2 server and a C# application? 无法建立与安全mysql开放套接字的Java SSLSocket连接 - Failing to establish an Java SSLSocket connection to a secure mysql open socket 无法在本地主机上建立与服务器的连接:8000 - can't establish a connection to the server at localhost:8000 无法与aSmack 4.0.2建立新连接 - Can't establish a new connection with aSmack 4.0.2 Java和Python之间的套接字连接 - Socket connection between Java and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM