Android - 在单独的线程中连接到套接字

时间:2017-04-25 09:27:06

标签: android multithreading sockets android-asynctask printwriter

我正在尝试在Android中创建一个非常简单的服务器 - 客户端应用程序。 服务器在我的电脑上运行,它是用python编写的。 (只是一个简单的while(true)循环,它接收一个字符串并使用另一个字符串进行响应。) 问题出在Android客户端。所以我试图在一个单独的线程中创建一个单例类,其中:

  • 创建套接字
  • 连接到套接字
  • 可以从其他活动中获取
  • 写入套接字
  • 从套接字中读取

我尝试从另一个asynctask写入和读取。 它一直工作,直到我再次尝试写入套接字。 (1写是没问题,任何其他尝试都失败。)我没有得到任何异常,我检查了套接字是否关闭或写入程序为空等等。消息只是没有写入套接字。

这个解决方案有什么问题? :/

你能帮帮我吗?

以下是主题:

public class ConnectThread extends Thread
{
// singleton Part


private static class ThreadHolder {
    static final ConnectThread instance = new ConnectThread();
}
public static synchronized ConnectThread getInstance(){
    if(ThreadHolder.instance == null)
        Log.d("mytag", "NEW INSTANCE CREATED");
   // return (ThreadHolder.instance == null) ? ThreadHolder.instance = new ConnectThread() : ThreadHolder.instance;
    return ThreadHolder.instance;
}

private ConnectThread(){
}

// implementation part
private Socket mSocket;
private BufferedWriter socketWriter;
private BufferedReader socketReader;

public Socket getSocket() {
    return mSocket;
}


public void WriteToSocket(String msg)
{
    try{
        if(!(mSocket.isClosed()))
        {
            Log.d("mytag", "Writing to socket");
            if(socketWriter == null)
                Log.d("mytag", "Writer closed - in write to socket");
            socketWriter.write(msg);
            socketWriter.flush();
        }else
            Log.d("mytag", "CANT write to socket");
    }catch(IOException e)
    {
        e.printStackTrace();
        Log.d("mytag", e.toString());
    }
}

public String ReadFromSocket()
{
    try
    {
        if(!(mSocket.isClosed())) {
            Log.d("mytag", "Reading from socket");
            if(socketReader == null)
            {
                Log.d("mytag", "Reader closed - in read from socket");
            }
            return socketReader.readLine();
        }else
        {
            Log.d("mytag", "CANT from socket");
            return null;
        }
    }catch (IOException e)
    {
        e.printStackTrace();
        return null;

    }
}

@Override
public void run() {
   try
   {
       mSocket = new Socket();
       mSocket.setKeepAlive(true);
       try
       {
           mSocket.setTcpNoDelay(true);
       }
       catch (SocketException e)
       {
       }

       mSocket.connect(new InetSocketAddress("192.168.0.128", 8888), 2000);
       if(!(mSocket.isClosed()))
       {
           Log.d("mytag", "SOCKET IS RUNNING");
           socketWriter = new BufferedWriter(new OutputStreamWriter(this.mSocket.getOutputStream()));
           socketReader = new BufferedReader(new InputStreamReader(this.mSocket.getInputStream()));
           if(socketWriter == null)
           {
               Log.d("mytag", "WRITER NOT CREATED");
           }else
               Log.d("mytag", "WRITER READY");
           if(socketReader == null)
           {
               Log.d("mytag", "READER NOT CREATED");
           }else
               Log.d("mytag", "READER READY");
       }
   }catch (IOException e)
   {
       e.printStackTrace();
   }


}

}

以下是尝试阅读,写作:

        @Override
    protected Void doInBackground(Void... params)
    {
        PrintDebugMsg("do in background");



        //--------------------------------------------------------------------------------------
        changeProgressMsg(progressDialog, "Checking network availability...");
        //progressDialog.setTitle("Checking network availability...");
        //check network:

        ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(parentContext.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        if(netInfo != null && netInfo.isConnected())
        {
            networkAvail = true;
            response += "| Network available |";
        }

        PrintDebugMsg("do in background 2");


        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        changeStatusImg(imgvNetworkStatus, networkAvail?R.drawable.online:R.drawable.offline);


        //--------------------------------------------------------------------------------------
        changeProgressMsg(progressDialog, "Pinging server");
        //progressDialog.setTitle("Pinging server...");


        //check server status
        try {
            PrintDebugMsg("do in background 3");


            if(!(ConnectThread.getInstance().getSocket().isClosed()))
            {

                ConnectThread.getInstance().WriteToSocket(PING_FROM_DROID);

                String line     =   "";
                line = ConnectThread.getInstance().ReadFromSocket();

                if(line.equals(PING_ACK))
                {
                    serverAvail = true;
                    response += " | pinged |";
                    PrintDebugMsg("do in background 4", true);

                }

            }
            else{
                response += " | NOT pinged |";
                PrintDebugMsg("do in background 5", true);
                throw new UnknownHostException();
            }


            PrintDebugMsg("do in background 6", true);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response += " | UnknownHostException: " + e.toString() + " - during server check |";
            PrintDebugMsg("do in background 7", true);
        } finally{
            PrintDebugMsg("do in background 9", true);
            if(ConnectThread.getInstance().getSocket() != null){

            }
        }

        PrintDebugMsg("do in background 10", true);
        if(serverAvail)
        {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        changeStatusImg(imgvServerStatus, serverAvail?R.drawable.online:R.drawable.offline);


        //--------------------------------------------------------------------------------------
        changeProgressMsg(progressDialog, "Connectiong to server...");

        //connect to server:

        try {


            PrintDebugMsg("do in background 11",true);
            //socket = new Socket(dstAddress, dstPort);
            //socket  = ConnectThread.getInstance().getSocket();


            PrintDebugMsg("do in background 12",true);
            if(!(ConnectThread.getInstance().getSocket().isClosed())) {
                PrintDebugMsg("do in background 13",true);

                PrintDebugMsg("do in background 14",true);

                PrintDebugMsg("do in background 15",true);

                ConnectThread.getInstance().WriteToSocket(CONN_REQ_FROM_DROID);

                String line     =   "";
                line = ConnectThread.getInstance().ReadFromSocket();
                PrintDebugMsg("conn line = " + line, true);
                if(line != null && line.equals(CONN_ACK))
                {
                    connected = true;
                    response += "| connected |";
                    PrintDebugMsg("do in background 12");
                }
            }else
            {
                response += "| NOT connected |";
                PrintDebugMsg("do in background 13");
                throw new UnknownHostException();
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response += " | UnknownHostException: " + e.toString() + " - during connecting |";
        }finally{

            PrintDebugMsg("connection finished");
            }

        if(connected) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        changeStatusImg(imgvConnectionStatus, connected?R.drawable.online:R.drawable.offline);


//--------------------------------------------------------------------------
------------

        return null;
        }

" util"功能:

 private void PrintDebugMsg(String msg, boolean b)
    {
        if(b)
            Log.d("mytag", msg);
    }

    private void changeProgressMsg(final ProgressDialog dialog,final String value){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.setMessage(value);
            }
        });
    }

    private void changeStatusImg(final ImageView imgView, final int imgId){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imgView.setImageResource(imgId);
            }
        });
    }

1 个答案:

答案 0 :(得分:3)

Sever.java

public class Server {

    public static void main(String[] args) {
        new Server().startServer();
    }

    public void startServer() {
        final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);

        Runnable serverTask = new Runnable() {
            @Override
            public void run() {
                try {
                    ServerSocket serverSocket = new ServerSocket(8000);
                    System.out.println("Waiting for clients to connect...");
                    while (true) {
                        Socket clientSocket = serverSocket.accept();
                        clientProcessingPool.submit(new ClientTask(clientSocket));
                    }
                } catch (IOException e) {
                    System.err.println("Unable to process client request");
                    e.printStackTrace();
                }
            }
        };
        Thread serverThread = new Thread(serverTask);
        serverThread.start();

    }

    private class ClientTask implements Runnable {
        private final Socket clientSocket;

        private ClientTask(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }

        @Override
        public void run() {
            System.out.println("Got a client !");

            // Do whatever required to process the client's request

            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}