Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

problem using TCP Socket

I am having problems using tcp socket for communicating to a remote machine. This is the code that I am using:

var socket2 = Titanium.Network.createTCPSocket({
        hostname:'jimbo.primate.com',
        port:4124,
        mode:Titanium.Network.WRITE_MODE
    });
    socket2.connect();
    socket2.write("test");
    socket2.close();

I get the following error:
Error: Host is null. in - [TiNetworkTCPSocketProxy connect:] (TiNetworkTCPSocketProxy.mm 492) on device. Simulator crashes, "Application has exited from Simulator" just after the app is launched.
I know error says the host is null, but host is reachable from terminal or from other desktop app. I also tried using the ip instead of the host name.
Could anybody point me in the right direction here? I'm not sure what I'm doing wrong.

Thank you!!!

— asked November 30th 2010 by Andreea x
  • iphone
  • mobile
  • socket
  • write
2 Comments
  • Hi Andreea

    I expect, you found a solution within this 3 monts.
    But I want to share my experiances for all others who are searching for this.
    You just have to wait after the connect(), until isValid is true.

    socket2.connect();
    while(socket2.isValid==0{}
    socket.write(...);
    

    Call the close() in the onRead Event.

    I know, the busy wait is ugly. But is takes between 3 an 6 loops.
    This schould be ok. Hope, it helps.

    Btw: The app crashes, if you try to close befor the connection is established.

    Tobias

    — commented February 17th 2011 by Tobias Kamber
  • thats bad, dont do that! - use an interval that checks every 10ms for a change - a while is super evil.

    — commented March 31st 2011 by Tobias O.

0 Answers

The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.