Java 在Android emulator上运行的客户端无法创建套接字

Java 在Android emulator上运行的客户端无法创建套接字,java,android,client,Java,Android,Client,StackOverflow中提到的讨论对我没有帮助 我试图让一个运行在Android emulator上的客户端连接到同一台机器上运行在eclipse上的服务器。 服务器配置为在端口5000上侦听。 服务器似乎不是问题所在。当我在eclipse上运行客户机时,他们可以打开套接字并进行通信。 当我尝试在Android emulator上运行Client类时,mainActivity会告诉客户端在创建时进行连接。 但是由于某种原因,dbugging流到达catch块,而不是创建套接字 我还尝试使用c

StackOverflow中提到的讨论对我没有帮助

我试图让一个运行在Android emulator上的客户端连接到同一台机器上运行在eclipse上的服务器。 服务器配置为在端口5000上侦听。 服务器似乎不是问题所在。当我在eclipse上运行客户机时,他们可以打开套接字并进行通信。 当我尝试在Android emulator上运行Client类时,mainActivity会告诉客户端在创建时进行连接。 但是由于某种原因,dbugging流到达catch块,而不是创建套接字

我还尝试使用
client.connect(“10.0.2.2”,5000)但它没有帮助

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Client client = new Client(this);
    try {
        client.connect("192.168.1.10",5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ClientThread clientThread = new ClientThread(this);
    try {
        clientThread.startClientThread("10.0.2.2",5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Client.java

   package com.example.oshri.p;

import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Observable;


public class Client extends Observable implements Runnable {
    MainActivity creatingActivity; // the activity that creates Client

  private Socket socket;
  private BufferedReader br;
  private PrintWriter pw;
  private boolean connected;
  private int port=5555; //default port
  private String hostName="localhost";//default host name

  public Client(MainActivity activity) {
        connected = false;
        this.creatingActivity = activity;
   }

  public void connect(String hostName, int port) throws IOException {
      if(!connected)
      {
         this.hostName = hostName;
         this.port = port;
         socket = new Socket(hostName,port);
         //get I/O from socket
         br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
         pw = new PrintWriter(socket.getOutputStream(),true);

           connected = true;
         //initiate reading from server...
         Thread t = new Thread(this);
         t.start(); //will call run method of this class
      }
  }

  public void sendMessage(String msg) throws IOException
  {
        if(connected) {
            pw.println(msg);
      } else throw new IOException("Not connected to server");
  }

  public void disconnect() {
        if(socket != null && connected)
      {
        try {
            socket.close();
        }catch(IOException ioe) {
            //unable to close, nothing to do...
        }
        finally {
            this.connected = false;
        }
      }
  }

  public void run() {
       String msg = ""; //holds the msg recieved from server
       try {
          while(connected && (msg = br.readLine())!= null)
          {
              creatingActivity.displayServerAnswer("Server:"+msg);

             this.setChanged();
                 this.notifyObservers(msg);
          }
       }
       catch(IOException ioe) { }
       finally { connected = false; }
  }

  public boolean isConnected() {
        return connected;
  }


  public int getPort(){
          return port;
      }

  public void setPort(int port){
          this.port = port;
      }

  public String getHostName(){
          return hostName;
      }

  public void setHostName(String hostName){
          this.hostName = hostName;
      }
}

@greenapps指出了问题: 套接字是在主活动中创建的

@Squonk指出,IP应为10.0.2.2- 仿真器中用于连接到运行仿真器的计算机的本地主机的转换地址

代码已编辑:

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Client client = new Client(this);
    try {
        client.connect("192.168.1.10",5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ClientThread clientThread = new ClientThread(this);
    try {
        clientThread.startClientThread("10.0.2.2",5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
ClientThread.java

package com.example.oshri.parkit;

import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Observable;


public class ClientThread extends Observable implements Runnable {
  MainActivity creatingActivity; // the activity that creates ClientThread
  private Socket socket; //Uses to connect to the server
  private BufferedReader br; //For reading input from server.
  private PrintWriter pw; //For writing output to server.
  private boolean connected; //Status of client.
  private int port=5555; //default port
  private String hostName="localhost";//default host name

  public ClientThread(MainActivity activity) {
        connected = false;
        this.creatingActivity = activity;
   }

  public void startClientThread(String hostName, int port) throws IOException {
      if(!connected)
      {
         this.hostName = hostName;
         this.port = port;

         Thread t = new Thread(this);
         t.start(); //will call run method of this class, that first thing will create a client socket(cannot create socket in main thread)
      }
  }

  public void sendMessage(String msg) throws IOException
  {
        if(connected) {
            pw.println(msg);
      } else throw new IOException("Not connected to server");
  }

  public void disconnect() {
        if(socket != null && connected)
      {
        try {
            socket.close();
        }catch(IOException ioe) {
            //unable to close, nothing to do...
        }
        finally {
            this.connected = false;
        }
      }
  }

  private void connect(){
      // create socket to connect the server
      try {
          socket = new Socket(hostName, port);
      }
      catch(SocketException e){
          System.out.println("client socket could not be created");
      }
      catch (UnknownHostException e){
          System.out.println("UnknownHostException");
      }
      catch (IOException e){
          System.out.println("IOException");
      }

      // create buffers for socket IO
      try{
          br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          pw = new PrintWriter(socket.getOutputStream(),true);
      }
      catch (IOException e){
          System.out.println("socket buffers could not be created");
      }

  connected = true;
  }
  public void run() {
       connect(); //connects the server

   String msg = ""; //holds the msg recieved from server
   try {
      while(connected && (msg = br.readLine())!= null)
      {
         //System.out.println("Server:"+msg);
          creatingActivity.displayServerAnswer("Server:"+msg);

         //notify observers//
         this.setChanged();
        //notify+send out recieved msg to Observers
             this.notifyObservers(msg);
      }
   }
   catch(IOException ioe) { }
   finally { connected = false; }
  }
  public boolean isConnected() {
        return connected;
  }


  public int getPort(){
          return port;
      }

  public void setPort(int port){
          this.port = port;
      }

  public String getHostName(){
          return hostName;
      }

  public void setHostName(String hostName){
          this.hostName = hostName;
      }
}

Client.java的代码不完整。不放完整代码也有什么例外。你能把stacktrace放进去吗snippet@aberry我编辑了这个问题,它是一个翻译地址,在模拟器中用来连接到运行模拟器的机器的本地主机。哦,是的,我监督了。那么您将有一个NetworkOnMainThread异常。它将在日志中。确实如此。所有网络代码都应该在一个线程中完成。