Titanium JIRA Archive
Appcelerator Community (AC)

[AC-2236] Android: Close a socket then reopen it, unable to write on it.

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionCannot Reproduce
Resolution Date2013-12-29T19:35:55.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsStudio, Titanium SDK & CLI
Labelsandroid
ReporterMarvin Rosillette
AssigneeShak Hossain
Created2013-11-21T02:32:50.000+0000
Updated2016-03-08T07:41:14.000+0000

Description

Hi everybody and thanks for reading, I'm going through an issu and I really don't know how to handle it. First, I create a button to open a new window, instantiate a new socket and connect it to my computer through wifi. Here is the code :

var OptionWindow = require('display/OptionWindow');
var TactSocket = require('trans/tactSocket');

startButton.addEventListener('singletap',function(){
     new appDisplay().open({
         activityEnterAnimation: Ti.Android.R.anim.fade_in
     });
     new TactSocket("192.168.1.27",2009).connect();
});
When I close the window open before, I also want to close the socket, so here is the code in the window :
 
//create a window
var self = Ti.UI.createWindow({
	backgroundColor:'#000000',
	navBarHidden:true,
	fullscreen:true,
	windowSoftInputMode : Ti.UI.Android.SOFT_INPUT_ADJUST_RESIZE
});

//Listen to the 'close' event, and fire a new event 'app_close'
self.addEventListener('close',function(){
    Ti.App.fireEvent('app_close');
});
Then in my tactsocket function I did this :
function tactSocket(host,port){
    var tactsocket = Ti.Network.Socket.createTCP({
        host : host,
        port : port,
        connected : function(e) {
            alert('connected socket);
            //alert(tactsocket.isWriteable);           
        },
        error : function(e) {
            alert('Error (' + e.errorCode + '): ' + e.error);
        }
    });

    //Listen to the 'app_close' event to close the socket
    Ti.App.addEventListener('app_close',function(){        
        tactsocket.close();
        alert('socket closed');        
    });

    return self;
}
What happen now :

Open the App

click start button

window open et socket connect, alert show me that the socket is well connected and i can write in the socket

close the window, alert show me that the socket is well closed

open the window again, alert show me that the socket is well connected, but this time I am unable to write in the socket, I get a runtime error saying the socket is not connected

close the window I also get a runtime error saying the socket is not connected

May be I'm missing something about socket.

Comments

  1. Marvin Rosillette 2013-11-27

    nothing ?
  2. Mauro Parra-Miranda 2013-12-04

    Hi, Thank your providing the code snippet. Since it is not the complete implementation of the scenario, we can't fully test it. We need runnable test case to provide you necessary feedback. Based on the code you posted, it looks like you are creating objects without assign those to any variable, so we will need to add variables to the test and see if this fails. For example, startButton.addEventListener('singletap',function(){ new appDisplay().open({ activityEnterAnimation: Ti.Android.R.anim.fade_in }); new TactSocket("192.168.1.27",2009).connect(); }); This segment of the code is just creating TackSocket without assigning it to anywhere. If you can provide us more info, we can look into it further. Regards, Mauro
  3. Mauro Parra-Miranda 2013-12-13

    Hello Marvin, I created another testcase (i.e., create a new mobile project and paste this to the app.js):
       var win = Ti.UI.createWindow({
       	backgroundColor:'white',
       });
       
       var createButton = Ti.UI.createButton({
       	title:'create socket',
       	top:20,
       	left:20,
       });
       
       var deleteButton = Ti.UI.createButton({
       	title:'delete socket',
       	top:20, 
       	right:20,
       });
       
       var tactsocket;
       var host = "74.125.20.105";
       var port = 80;
        
       createButton.addEventListener('click',function(){
       	tactsocket = Ti.Network.Socket.createTCP({
               host : host,
               port : port,
               connected : function(e) {
                   alert('connected socket');
                   alert(e);
               },
               error : function(e) {
                   alert('Error (' + e.errorCode + '): ' + e.error);
               }
           });
       	tactsocket.connect();
       });
       
       deleteButton.addEventListener('click',function(){
       	tactsocket.close();
       	alert('deleted socket');
       });
       
       win.add(createButton);
       win.add(deleteButton);
       win.open();
       
    and looks like it's not failing (the socket will be reused after re initializing it with a .connect(). Can you please take a look and let us know if is working for you as well? For more information on the sockets lifecycle, you can get into this doc: http://docs.appcelerator.com/titanium/3.0/#!/guide/TCP_Socket_API_Spec-section-29004739_TCPSocketAPISpec-Socketlifecycle Best, Mauro
  4. Shak Hossain 2013-12-29

    Hi Marvin Rosillette, We are marking this ticket as "cannot reproduce". We posted a sample test case. Please let us know if you continue to get error with our test case. We wait for your feedback. If we don't hear back from you in a week, we will consider this issue resolved and close the ticket.

JSON Source