TCPSocket.Listen and Ports

[quote=473909:@Markus Rauch]in the AddSocket Event from ServerSocket1

xojo 2019r3.1 at google drive
TestServerClient.xojo_binary_project[/quote]
I’m on 2019r1.1

Converted. Doesn’t help, unfortunately. I was always able to connect, it’s probably that the app that’s connecting (not mine) connects in a different way than Xojo expects. However, VB.NET can do it, so I’m sure there must be a way…

So an other app connects to your app?
What app may that be? Are you sure it’s using tcp and not udp?
It may require some data within a certain time?
Enlighten us. Or show the VB code?

agree that looks wrong in xojo because after the connection is done the listening port is free for any other new connection.
the listening port is not used for data transfer.
why exact do you need this real port number?

[quote=473939:@Markus Rauch]agree that looks wrong in xojo because after the connection is done the listening port is free for any other new connection.
the listening port is not used for data transfer.
why exact do you need this real port number?[/quote]

http://documentation.xojo.com/api/networking/socketcore.html#socketcore-port read the manual…:wink:

[quote]once the connection occurs, you can check to see what port the OS has bound you to.
This will be a random-seeming port number. [/quote]

this andom-seeming port number i expect in the tcpsocket object after i return it in the AddSocket.

No, the server socket’s port will be given if i’m not mistaken. The TCPSocket added in the AddSocket event is a “child” of the serversocket.

But @Andy Broughton didn’t require a server, so then he should not do that. If he wan’ts a single listening socket, then ONLY a single connection can be made to it at a time. After there is a disconnect, the socket should be set to Listen again.

Listen to this, i’m thinking the same. First set the Port then after connecting or listening read the port.

[quote=473929:@Derk Jochems]So an other app connects to your app?[/quote]Yes.

[quote]What app may that be? Are you sure it’s using tcp and not udp?[/quote]It’s a commercial app, and yes, it uses TCP. As I said, no issues connecting via VB.NET, just can’t replicate using Xojo.

[quote]It may require some data within a certain time?[/quote]Maybe, but both apps send the necessary handshaking signal

1 Imports System.Net 2 Imports System.Net.Sockets 3 4 Public Class Form1 5 Dim LANListener As TcpListener 6 Dim LANClient As TcpClient 7 Dim LANStream As NetworkStream 8 Dim LANBuffer As Byte() 9 Dim Handshake As Byte() = {&HF0, &H43, &H10, &H3E, &H19, &H7F, &HF7} 10 11 Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click 12 StopButton.Enabled = True 13 LANListener = New TcpListener(IPAddress.Any, 50000) 14 15 Try 16 LANListener.Start() 17 RichTextBox1.AppendText("LAN Listener Started." & vbNewLine) 18 Catch ex As Exception 19 RichTextBox1.AppendText("Could Not Start LAN Listener. Error: " & ex.Message & vbNewLine) 20 Exit Sub 21 End Try 22 23 While LANListener IsNot Nothing 24 StartButton.Enabled = False 25 If LANListener.Pending Then 26 Try 27 LANClient = LANListener.AcceptTcpClient 28 RichTextBox1.AppendText("Client Connected. IP = " & LANClient.Client.RemoteEndPoint.ToString & vbNewLine) 29 Catch ex As Exception 30 RichTextBox1.AppendText("Could Not Create Socket. Error: " & ex.Message & vbNewLine) 31 Exit Sub 32 End Try 33 Exit While 34 End If 35 Application.DoEvents() ' Make sure connect can be cancelled 36 End While 37 38 If LANListener IsNot Nothing Then LANListener.Stop() 39 40 If LANClient IsNot Nothing Then 41 RichTextBox1.AppendText("Starting to Read from TCPClient." & vbNewLine) 42 LANStream = LANClient.GetStream() 43 Timer1.Start() 44 While LANStream.CanRead And LANClient.Connected 45 If LANStream.DataAvailable Then 46 ReDim LANBuffer(LANClient.Available - 1) 47 LANStream.Read(LANBuffer, 0, LANClient.Available) 48 RichTextBox1.AppendText(BitConverter.ToString(LANBuffer) & vbNewLine) 49 RichTextBox1.ScrollToCaret() 50 End If 51 Application.DoEvents() ' Allow UI to update 52 End While 53 End If 54 StartButton.Enabled = True 55 End Sub 56 57 Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click 58 RichTextBox1.AppendText("Closing Port" & vbNewLine) 59 Timer1.Stop() 60 If LANStream IsNot Nothing Then LANStream.Close() 61 If LANListener IsNot Nothing Then 62 LANListener.Stop() 63 LANListener = Nothing 64 End If 65 End Sub 66 67 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 68 If LANStream.CanWrite Then 69 LANStream.Write(Handshake, 0, Handshake.Length) 70 End If 71 End Sub 72 73 Private Sub Form1_FormClosed(sender As Object, e As EventArgs) Handles MyBase.FormClosed 74 StopButton.PerformClick() 75 End Sub 76 End Class

The returned IP address on line 28 is the local IP (127.0.0.1) and the port is a random number.

If I understand that VB code then this is a Xojo translation.

And your xojo code part?

Thank you @Andrew Lambert !!

So… In the end, nothing to do with Listen or Sockets. It threw me off at first that the returned port was the same as the listen port, because I foolishly thought that the Automatic connect and the Connect method would function the same, and that the random port would be returned upon connect. Kinda weird, but so’s a lot of stuff with Xojo, but anyway, barking up the wrong tree.

Came down to the Handshaking message in the Timer. This is what I had:

Dim HandShake As String = Chr(&hF0) + Chr(&h43) + Chr(&h10) + Chr(&h3E) + Chr(&h19) + Chr(&h7F) + Chr(&hF7) TCPSocket1.Write(HandShake)

This is what Andrew suggested:

Dim HandShake As MemoryBlock = DecodeHex("F043103E197FF7") TCPSocket1.Write(HandShake)

Andrew’s works, mine doesn’t. No idea why. TCPSocket.Write should be able to take a string just as easily as a MemoryBlock, but maybe not?

Bottom line is that TCPSocket.Listen doesn’t work as I would have expected, but neither does TCPSocket.Write.

Thanks everyone!

And yes, the TCPSocket.Port will be the SAME as the listening port after connecting via an automatic connection after calling TCPSocket.Listen, but it will be different (a random port) after connecting via TCPSocket.Connect. I would think that’s non-intuitive, but what do I know?

[quote=473985:@Andy Broughton]
Came down to the Handshaking message in the Timer. This is what I had:

Dim HandShake As String = Chr(&hF0) + Chr(&h43) + Chr(&h10) + Chr(&h3E) + Chr(&h19) + Chr(&h7F) + Chr(&hF7) TCPSocket1.Write(HandShake)

This is what Andrew suggested:

Dim HandShake As MemoryBlock = DecodeHex("F043103E197FF7") TCPSocket1.Write(HandShake)

Andrew’s works, mine doesn’t. No idea why. [/quote]

Did you try ChrB rather than Chr? My guess is the string you are creating using Chr is not correct because you are using values > 127.
Debugging the string variable’s binary data would confirm that.

I think you need some background on TCP/IP networking: there are many books on this topic.
You can also use TcpView to monitor and see what happens when VB.NET application runs.
Don’t guess.

[quote=473985:@Andy Broughton]Thank you @Andrew Lambert !!

So… In the end, nothing to do with Listen or Sockets. It threw me off at first that the returned port was the same as the listen port, because I foolishly thought that the Automatic connect and the Connect method would function the same, and that the random port would be returned upon connect. Kinda weird, but so’s a lot of stuff with Xojo, but anyway, barking up the wrong tree.

Came down to the Handshaking message in the Timer. This is what I had:

Dim HandShake As String = Chr(&hF0) + Chr(&h43) + Chr(&h10) + Chr(&h3E) + Chr(&h19) + Chr(&h7F) + Chr(&hF7) TCPSocket1.Write(HandShake)

This is what Andrew suggested:

Dim HandShake As MemoryBlock = DecodeHex("F043103E197FF7") TCPSocket1.Write(HandShake)

Andrew’s works, mine doesn’t. No idea why. TCPSocket.Write should be able to take a string just as easily as a MemoryBlock, but maybe not?

Bottom line is that TCPSocket.Listen doesn’t work as I would have expected, but neither does TCPSocket.Write.

Thanks everyone![/quote]

It didn’t work because you should have used ChrB() instead of Chr() you want the byte values to be sent.
The version of @Andrew Lambert is much more readable.

Glad it worked out.:wink:

[quote=474004:@Maurizio Rossi]I think you need some background on TCP/IP networking: there are many books on this topic.[/quote]No doubt.
However, I’m not sure how that would lead to an understanding of the Xojo syntax and functionality, especially since the VB.NET version returns the random port. My real problem I think is my inexperience with Xojo.

[quote]You can also use TcpView to monitor and see what happens when VB.NET application runs.
Don’t guess.[/quote]I’ll check out TCPView. Thanks for the tip!

[quote=473987:@Kevin Gale]Did you try ChrB rather than Chr? My guess is the string you are creating using Chr is not correct because you are using values > 127.
Debugging the string variable’s binary data would confirm that.[/quote]

[quote=474012:@Derk Jochems]It didn’t work because you should have used ChrB() instead of Chr() you want the byte values to be sent.
The version of @Andrew Lambert is much more readable.

Glad it worked out.;)[/quote]
Thanks! Yeah, my inexperience with Xojo.
I should really be using MemoryBlock more instead of strings anyway.

[quote=474069:@Andy Broughton]No doubt.
However, I’m not sure how that would lead to an understanding of the Xojo syntax and functionality, especially since the VB.NET version returns the random port. My real problem I think is my inexperience with Xojo.
I’ll check out TCPView. Thanks for the tip![/quote]
The random port isn’t something returned by the framework you are using but is TCP specific and also totally unrelated to the OS you are using.
No framework can dictate how listen, connect or bind works or can be implemented.
These and other details are clearly documented in how a TCP connection is made and behave.
Mastering sockets isn’t exactly something that can be done on trial and error basis.

Ok, I’m curious… so why then does VB.NET return a random port number, and Xojo return the same port number as the listener? I’m not talking about what’s being done “under the hood” but how Xojo differs from VB.NET.
If this is not of interest to you, no need to reply.