使用套接字的Android客户端和PC服务器通信

时间:2012-04-14 18:16:01

标签: java android sockets

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

服务器端(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();
        }
    }
}

客户端(Android):

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

1 个答案:

答案 0 :(得分:0)

在Android模拟器中,您应该使用10.0.2.2而不是127.0.0.1

来引用您的PC

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

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

你的意思是:

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

...