Android网络在模拟器中不起作用

时间:2012-09-09 05:47:25

标签: android networking

我正在开发一个基本的客户端 - 服务器应用程序,用于在Android应用程序(客户端)和PC上的Java服务器之间发送消息。当我使用模拟器时,消息正在被发送和接收,但是当我尝试在我的手机上使用该应用程序时,这些消息不起作用。我使用connectify将手机连接到我的笔记本电脑托管的wifi网络。我想知道什么阻止我的手机连接...... 这是代码:

SERVER

public class ServerMain {
public static void main(String argv[]) throws Exception
  {
     String clientSentence;
     String capitalizedSentence;
     int sock = 1234; 
     ServerSocket welcomeSocket = new ServerSocket(sock);

     while(true)
     {
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient =
           new BufferedReader(new            InputStreamReader(connectionSocket.getInputStream()));
        clientSentence = inFromClient.readLine();
        System.out.println("Received: " + clientSentence);
        if(clientSentence.equalsIgnoreCase("QUIT"))
            break;
     }
     welcomeSocket.close();

  }
}

客户端

public class Message extends Activity {
EditText et;
String msg1 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et = (EditText)findViewById(R.id.etTest);
}
            // TODO Auto-generated method stub
class GetMessages extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
       final String msg = et.getText().toString();
         try{
            Socket clientSocket = new Socket("*myip*", 1234);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            String sentence = msg;
            outToServer.writeBytes(sentence + '\n');

        } catch (Exception e) {
           e.printStackTrace();
        }
         return null;
    }

}    
  public void readWebpage(View view) { //The button on click calls this function (from xml)
   new GetMessages().execute();
   }
}

0 个答案:

没有答案