Problem in connecting via TCPSocket

I would like to exchange date between to computers. So I provide one TCPSocket.listen and the second with TCPSocket.connect. Both on the same non reserved Port.
The first (Listening) connects. The event fires and the “IsConnected-Property” shows true. BUT
The second (Connect) gets the RemoteAddress from the “Listener”, but never changes into connected=true. No last error code. AND the Port-Property was altered in something completely different. Of course i never can start writing date because they never disappear from the buffer.

Can somebody please show me which screws to turn to make it work?

[quote=207920:@Bernhard Brämswig]I would like to exchange date between to computers. So I provide one TCPSocket.listen and the second with TCPSocket.connect. Both on the same non reserved Port.
The first (Listening) connects. The event fires and the “IsConnected-Property” shows true. BUT
The second (Connect) gets the RemoteAddress from the “Listener”, but never changes into connected=true. No last error code. AND the Port-Property was altered in something completely different. Of course i never can start writing date because they never disappear from the buffer.

Can somebody please show me which screws to turn to make it work?[/quote]

Have you tried the SynchronousTCPSocketClass and SynchronousTCPSocketExample projects in Examples/Communication ? It looks as what you are after.

I just tried Do until - Poll - Loop in my source code. It helped. But why ist the Portnumber altered?

Before connecting, TCPSocket.Port refers to the remote port which will be connected to; after connecting TCPSocket.Port refers to the semi-random local port being used for the connection.

In the listening socket you need to return to listening mode after disconnecting. In the listening socket error event place the lines

If me.LastErrorCode = 102 Then me.Listen Return End If

This will put the socket back into waiting for a connection from the second app.

Just about any error will disconnect the socket.

FWIW, You may want to consider using a ServerSocket instead to avoid the headaches of managing socket disconnects and creation.

ServerSocket is something I did not (yet) try.
Concerning the random chosen port:
I normally use the port to sneak trough the firewall of my Router. What if the response from the remote client returns a different way and finds the random port locked? Is the only way to put the serving computer into DMZ?

[quote=208375:@Bernhard Brämswig]ServerSocket is something I did not (yet) try.
Concerning the random chosen port:
I normally use the port to sneak trough the firewall of my Router. What if the response from the remote client returns a different way and finds the random port locked? Is the only way to put the serving computer into DMZ?[/quote]
Typically this isn’t a problem. Firewalls are usually smart enough to know the difference between new traffic (which is what it blocks) and established or related traffic (which is this port shifted traffic). It’ll probably work just fine.

Do not worry about the port changing. That is normal and will not be a problem with your firewall. Focus on everything else and forget the changed port. It actually means the connection was successful.

hello all
how can i read data from socket?

The socket will raise its DataAvailable event. Use me.Read / me.ReadAll to get the available data.