Localhost TCP

Hello,

I’m a noob to Xojo, and I actually try to send tcp packet on my localhost with console app.

I’m not a specialist of POO too, so I tried a bit of code here :

  Dim TCPSocket1 As TCPSocket
  
  TCPSocket1 = New TCPSocket
  
  TCPSocket1.Address = "127.0.0.1"
  TCPSocket1.Port = 6500
  
  Print "Trying to open connection..."
  
  TCPSocket1.Connect
  
  If TCPSocket1.IsConnected then
    Print "It works !"
  Else
    Print "Connection failed"
  End if
  
  TCPSocket1.Close
  Print "Socket closed"

All this code is in the Run Event Handler, and I have my TCP controller below the app.

Basically, that should send a packet to my localhost and return me an answer if it works or not… Apparently it doesn’t for the moment :confused:
I’m sure there is a lot of mistakes or missing elements… If you guys could direct me on the right way…

Thanks by advance

How are you listening to TCP traffic on localhost ?

Well, for one thing, the connect method is asynchronous, so if you check isConnected immediately like that, it’s definitely going to return false.

Since you said this is all in the Run event, I assume you’re using a console app. You should initialize your app, send off the request and then wait for it to be connected or an error to occur.

Thanks for answering guys,

For Michel :
That was one of the stupid point I talked about, so now I add a listen method in run.

For greg :
I erased the isConnected for the moment, too complicated for a simple task :slight_smile:

The thing is I just wanna know if my socket can access to my localhost by print it on the screen.
For now, I have the socket stat in run (address, port, listen and connect method)
I have my TCPSocket with Connected event for the print
And a ServerSocket but I don’t know what I’m supposed to put in there… I’m really lost for it.

Have you looked in the examples? TCP is a communications protocol and requires something to communicate with. You do not just “connect to localhost”. You connect to some application that is running on localhost and listening for connections. That typically requires 2 applications. One to listen, the other to connect.

Do look at the examples that come with your Xojo install. They should make this a lot clearer.