2012-04-14 33 views
2

我在同一台PC上运行android模拟器和服务器(java)上的客户端,并使用套接字编程。在下面附上代码。单独的两个都运行良好,但实际的数据传输没有发生,我无法弄清楚。使用套接字的Android客户端和PC服务器通信

服务器端(PC):

import java.net.*; 
import java.io.*; 

public class Server_tcp { 
    void run(){ 
     { 
      try { 
       System.out.println("In 1st try blck"); 
       try { 
        System.out.println("In 2nd try blck"); 
        Boolean end = false; 
        ServerSocket ss = new ServerSocket(4444); 
        System.out.println("socket created"); 
        while(!end){ 
         //Server is waiting for client here, if needed 
         Socket s = ss.accept(); 
         System.out.println("socket accepted"); 
         BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); 
         PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush 
         String st = input.readLine(); 
         System.out.println("Tcp Example" + "From client: "+st); 
         output.println("Good bye and thanks for all the fish :)"); 
         s.close(); 
         if (st==null){ end = true; } 
        } 
        ss.close(); 
       } catch (UnknownHostException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } catch (IOException exp) { 
       // TODO Auto-generated catch block 
       exp.printStackTrace(); 
      } 
     } 
    } 
    public static void main(String args[]) 
    { 
     Server_tcp server = new Server_tcp(); 
     while(true){ 
      server.run(); 
     } 
    } 
} 

客户端(安卓):

package com.try3; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.EditText; 

import java.io.*; 
import java.net.*; 

public class ClientTCPActivity extends Activity { 
/** Called when the activity is first created. */ 

    private EditText et; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     System.out.println("Before try net"); 
     trynet(); 
     System.out.println("after try net"); 
    } 
    public void trynet() { 
     System.out.println("inside try net"); 
     try { 
      System.out.println("inside try"); 
      Socket s = new Socket("127.0.0.1",4444); 

      //outgoing stream redirect to socket 
      OutputStream out = (OutputStream) et.getContentDescription(); 

      PrintWriter output = new PrintWriter(out); 
      output.println("Hello Android!"); 
      BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); 

      //read line(s) 
      String st = input.readLine(); 
      System.out.println(st); 

      //Close connection 
      s.close(); 

      System.out.println(" try "); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

从Android模拟器,你应该使用10.0.2.2被提及到您的PC,而不是127.0.0.1的

我不明白你在这里要做什么。

OutputStream out = (OutputStream) et.getContentDescription(); 
PrintWriter output = new PrintWriter(out); 

您是不是要找这样的:

PrintWriter output = new PrintWriter(s.getOutputStream(),true); 

+0

嗨aneesh谢谢,但仍然没有传输数据... – Jam 2012-04-16 08:34:18

+0

如果您在HoneyComb SDK或更高版本上运行此操作,则会抛出NetworkOnMainThreadException。所以你将不得不在一个单独的线程中进行Socket连接。 – coderplus 2012-04-16 17:09:32

相关问题