Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中发送和接收套接字的实现_Java_Android_Sockets - Fatal编程技术网

Java Android中发送和接收套接字的实现

Java Android中发送和接收套接字的实现,java,android,sockets,Java,Android,Sockets,我目前正在开发android和服务器之间的套接字通信,这是一个在终端上运行的简单java程序。一切进展顺利,但当我关闭应用程序时,logCat中总会出现警告: IInputConnectionWrapper showStatusIcon on inactive InputConnection 我正在互联网上搜索,以找出我在StackOverflow中找到的问题。不同之处在于我可以在我的程序中很好地发送和接收信息。这个类似问题的答案是连接没有关闭。这是否意味着我没有调用socket.clo

我目前正在开发android和服务器之间的套接字通信,这是一个在终端上运行的简单java程序。一切进展顺利,但当我关闭应用程序时,logCat中总会出现警告:

IInputConnectionWrapper    showStatusIcon on inactive InputConnection
我正在互联网上搜索,以找出我在StackOverflow中找到的问题。不同之处在于我可以在我的程序中很好地发送和接收信息。这个类似问题的答案是连接没有关闭。这是否意味着我没有调用
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();
        } 
    }
我的问题是:

我的实现是否有问题,或者是否有更好的方法


非常感谢你的帮助

我不确定这对你是否有用。。但我给你的是我用于客户机-服务器通信的代码

客户端代码:

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

    }

}
服务器端代码:

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

    }

}

有一个工具可以帮助您诊断Android设备和模拟器上的网络相关问题。它可能会帮助您进一步追踪问题。ARO工具是开源的,可在此处使用

非常感谢您的帮助!但在您的程序中,您通过监听控制台发送信息。我想要的是我可以调用函数sendmessage从其他类发送消息。你对此有什么想法吗?还是谢谢你!