插座一段时间后关闭

时间:2011-11-25 09:59:43

标签: sockets garbage-collection

我的套接字关闭或者在一段时间后由同行重置,我认为通过其读者或作者的垃圾收集问题。

Asynctask处理回复:

@Override
protected Void doInBackground(Void... params) {

    //Log.e("NEW LISTENER THREAD NAME", name);

    //initializations
    try{
        clientSocket = new Socket();
        //clientSocket.setTcpNoDelay(true);
        clientSocket.connect(new InetSocketAddress(serverURL, dataServerPort));
        requestSender = new PrintWriter(new PrintStream(clientSocket.getOutputStream(), true,"UTF-8"));
        Sender.Init();
    }catch(Exception e){
        e.printStackTrace();
    }

    gsonObj = new GsonBuilder().create();//This the object that handels every comming response
    finish = false;

    try{
        listener = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }catch (IOException e) {
        Log.e("FROM CREATING LISTENER", "FROM CREATING LISTENER ========> ");
        e.printStackTrace();
    }

    LOGGED_IN = StaticArea.getLoggedIn(cnt);
    if(LOGGED_IN){
        USER = StaticArea.getUserName(cnt);
        Sender.ResumeUser();

        /*********DELEGATING CONNECTING TO SERVER TO BE USED IN SERVICE*************/
        Message connectionMsg = new Message();
        connectionMsg.obj = Boolean.valueOf(true);
        serviceHandler.handleMessage(connectionMsg);
        /*********END DELEGATING CONNECTING TO SERVER*************/

    }else{

        /*********DELEGATING CONNECTING TO SERVER TO BE USED IN SERVICE*************/
        Message connectionMsg = new Message();
        connectionMsg.obj = Boolean.valueOf(false);
        serviceHandler.handleMessage(connectionMsg);
        /*********END DELEGATING CONNECTING TO SERVER*************/

    }

    GoOnline();

    while(!finish){

        try{

            answerS = listener.readLine();

            if(answerS != null )//to avoid any null response
            if(answerS.contains(Response.MYRESPONSE){

                if(MyService.theHandler != null){
                    Message msg = new Message();
                            msg.obj = answerS;
                            MyService.theHandler.sendMessage(msg);

Sender类是具有静态方法并使用我的套接字输出的类:

public class Sender {

private static Gson gsonObj;

public static void Init() {
    gsonObj = new GsonBuilder().create();
}

public static void SendTestRequest(){

    try{
        Request req = new Request();
        req.setR_TYPE(Request.TEST);

        String reqString = gsonObj.toJson(req);
        requestSender.println(reqString);
        requestSender.flush();
    }catch(Exception e){

    }


}//end method

0 个答案:

没有答案