(socket == null) always false

13 views
Skip to first unread message

Arne Mailand

unread,
Oct 16, 2016, 10:01:15 AM10/16/16
to App Inventor Open Source Development
Hi,

I'm in trouble because the argument of this if-block is always false:


if(this.socket == null){

this.socket = new Socket(ip, port);

}

my intention is to have the socket connection established only if it hasn't been done so already. But the argument seems to be always false as the connection is never being established this way. when i remove the if-block around the socket initialisation completely, the connection is built up every time, just as it is supposed to. Any idea what's wrong here?
by the way, the result is the same if i do

if(!socket){}

or

if(!socket.isConnected()){}

or

if(socket.isClosed()){}

It just always seems to be false :-(

Here comes the entire code:



protected Socket socket;

@SimpleFunction(description = "Socket-Initialisierung")
public void Init(String ip, int port){

try{

if(this.socket == null){

this.socket = new Socket(ip, port);

}

} catch (IOException e) {

e.printStackTrace();

} finally {}
}

Arne Mailand

unread,
Oct 16, 2016, 12:14:55 PM10/16/16
to App Inventor Open Source Development
Well, in terms of isConnected() and isClosed(), I just found out that these methods respond very irrationally.

for instance, after a connection is established

isConnected() returns TRUE
!isConnected() returns FALSE
isClosed() returns FALSE
!isClosed() returns TRUE

everything fine so far. But when I do socket.close();

isConnected() returns TRUE
!isConnected() returns FALSE
isClosed() returns TRUE
!isClosed() returns FALSE

to cut a long story short: socket.isConnected() returns TRUE although socket.close() was executed and socket.isClosed() returns TRUE.

Hmm any ideas?
Reply all
Reply to author
Forward
0 new messages