在Android中传输和接收套接字实现

时间:2012-06-25 03:56:52

标签: java android sockets

我目前正在开发android和server之间的socket通信,这是一个在终端中运行的简单java程序。事情进展顺利,但是当我关闭应用程序时,logCat中始终会出现警告:

IInputConnectionWrapper    showStatusIcon on inactive InputConnection

我在互联网上搜索,找出我在StackOverflow Similar Problem中发现帖子的问题。不同的是,我可以在我的程序中很好地发送和接收信息。这个类似问题的答案是连接没有关闭。这是否意味着我在手术后没有打电话给socket.close();?这导致了更复杂的实施问题。

首先,我想要一个静态的套接字来监听并发送到服务器。因为每次传输内容时我都不会关闭套接字,所以我只是在听众完成工作后关闭套接字。

详细代码发布在

下面

我在构造函数中初始化连接为:

client = new Socket(mServerName, mport);
out = new DataOutputStream(client.getOutputStream()); 
inFromServer = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(inFromServer);

让他们在整个过程中都在那里。

我写了从android到服务器的传输函数如下:

public void sendRequest(int type, int what1, int what2, Object data)
{
    try{
        if(client.isConnected())
        {
            out.writeUTF(encoded(type,what1,what2,data) + "\n");            
        }
    }catch(IOException e){
        Log.e(TAG, "IOException at SendRequest");
        e.printStackTrace();
    }
}

新线程中的监听器:

try {       
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            ReceiveHandler(line);
        }
    } catch (IOException e) {
        Log.e(TAG, "IOException at StartListen");
        e.printStackTrace();
    }finally{
        try { 
         // The Only Place that I close the Socket
            inFromServer.close();
            out.close();
            reader.close();
            client.close();
        } catch (IOException e) {
            Log.e(TAG, "Close Socket with IOException " + e.toString());
            e.printStackTrace();
        } 
    }

我的问题是:

  

我的实施有什么问题或有更好的方法吗?

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:1)

有一种工具可以帮助您诊断Android设备和模拟器中的网络相关问题。它可以帮助您进一步追踪问题。 ARO工具是开源的,可在http://developer.att.com/developer/forward.jsp?passedItemId=9700312

获取

答案 1 :(得分:0)

我不确定这是否适合您使用..但我给你的是用于客户端服务器通信的代码..

客户端代码:

public class ClientWala {

    public static void main(String[] args) throws Exception{

        Boolean b = true;
    Socket s = new Socket("127.0.0.1", 4444);

    System.out.println("connected: "+s.isConnected());


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

    // to write data to server
    while(b){

        if (!b){

             System.exit(0);
        }

        else {
            pw.write(new Scanner(System.in).nextLine());
        }
    }


    // to read data from server
    InputStream input   = s.getInputStream();
    InputStreamReader isr = new InputStreamReader(input);
    BufferedReader br = new BufferedReader(isr);
    String data = null;

    while ((data = br.readLine())!=null){

        // Print it using sysout, or do whatever you want with the incoming data from server

    }




    }
}

服务器端代码:

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


public class ServerTest {

    ServerSocket s;

    public void go() {

        try {
            s = new ServerSocket(44457);

            while (true) {

                Socket incoming = s.accept();
                Thread t = new Thread(new MyCon(incoming));
                t.start();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    class MyCon implements Runnable {

        Socket incoming;

        public MyCon(Socket incoming) {

            this.incoming = incoming;
        }

        @Override
        public void run() {

            try {
                PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                        true);
                InputStreamReader isr = new InputStreamReader(
                        incoming.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String inp = null;

                boolean isDone = true;

                System.out.println("TYPE : BYE");
                System.out.println();
                while (isDone && ((inp = br.readLine()) != null)) {

                    System.out.println(inp);
                    if (inp.trim().equals("BYE")) {
                        System.out
                                .println("THANKS FOR CONNECTING...Bye for now");
                        isDone = false;
                        s.close();
                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                try {
                    s.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {

        new ServerTest().go();

    }

}
相关问题