Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I connect in either direction on a network?

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
0
0
AU
I have 2 computers using standard Winsock connection between apps.
Bith have fixed addresses but the Server has one fixed port.

Mostly I use the 'client' to connect with the 'server' but occasionally I want the 'server' to initiate the connection to report back errors.

Apart from having both computers listening and both being a client, is there any other way that the same connection can be initiated from either end?
 
Have both ends Listen on the same LocalPort for a connection request most of the time (acting as a server awaiting a client).

When one end wants to open a conection (become a client), Close the Winsock control, set LocalPort = 0, and then do a Connect to the other end using the agreed-upon RemotePort number.

After boths ends Close the open connection, each end should Listen again.

This use of "server" and "client" is from the TCP perspective, where servers Listen/Accept and clients Connect. It has nothing to do with which you consider to be the server or the client in any larger sense.

This approach assumes a single connection between two programs, not a multiclient server scenario.
 
Thanks.
What is the reason for setting the local port to 0 before connecting?

 
at a guess to stop the "server" trying to manage another connection.
 
You set LocalPort to 0 in order to request a new ephemeral port from the underlying Winsock protocol stack.

Otherwise if you try to use the same port that was allocated previously you can run into "wrong state" errors because of the way TCP connections are managed.

Some of this is discussed in The TIME-WAIT state in TCP and Its Effect on Busy Servers, though the "busy servers" part does not apply here.
 
I have a setup that continually connects a 'checker' client in turn to 20 workstations (as servers), send and retrieves their data then closes, at the rate of one workstation each second

I use 3 winsocks alternately in the 'checker' so I can have a one second gap between when the connection is made and the data is sent and closed one second later. This eliminated the occasional error due to a slow connection that sometimes occurred.

I find that the local port of the 'checker' starts at about 1000 and steps on one number each connection until it reaches about 5000 then reverts to about 1000 without me doing anything. Like -
Winsock 1 = 1000,1004,1007
Winsock 2 = 1002,1005,1008
Winsock 3 = 1003,1007,1009

No errors seem apparent.

Setting the local port to 0 before each connection does not seem to stop this.

I would have thought that that each 'checker' should be able to use the same fixed local port all the time?

Setting the local ports of the three winsocks to a fixed value (like 1000,1001,1002) causes an 'address in use' error on the second attempt unless you wait about 90 seconds between tries.

Can you explain why this behavior happens and is there a better way of doing it?
 
Ahh, that's the one: "address in use." Sorry.

"Closed" ports stay in TIME-WAIT for 4 minutes, so on the average it takes about 2 minutes before they can be reused.

You're right, setting LocalPort to 0 will not prevent you from exhausting the ephemeral port pool.

The better way is to stop creating and dropping connections, TCP connections are not cheap. If you find yourself doing this then UDP may be a better protocol for your needs.
 
I am connecting in turn with a 40 Lantronix LAN to RS485 adapters that internally uses TCP.
I notice they disconnect in less than a milliseconds compared with a Windows PC that takes about 100ms.

The adapters are connected to large LED information signs.

Is there any way of forcing 'resetting' the pool say after all 40 have been interrogated in turn? This would be about every 15 seconds.


 
There are ways to relocate and enlarge the ephemeral port pool through registry settings, but that will only postpone the problem by a slight amount. You can shorten the TIME-WAIT interval but it will impact anything on the system using TCP (which includes Windows itself), and it is a STRONGLY DISCOURAGED practice.

The real problem is that you are disconnecting and reconnecting.

Your descriptions haven't covered anything that would justify this. Why not just keep the connections open and reuse them as intended?
 
This system is a forerunner of a replacement system I hope to have adopted that could have over 500 such adapters in the future.
They are scattered throughout our city.

I was under the impression that there is a limit to the number of winsocks you can have open at any time in one computer. (64???)

Is this true?

I transmit to about 10 adapters every second so each of 600 could be updated every minute.
By opening and closing in rotation I only have 4 winsocks open at any one time. This gives time for each connection to be properly established (about 100ms each needed for 100% reliability)
 
No, the limit is quite a bit higher than 64.

Also, the TIME-WAIT issue is by connection not by port. A connection is defined by 4 values: local IP, local port, remote IP, and remote port.

So if you connect to a device with IP 1.2.3.4 @ port 8123 and get a local port of 1000 assigned, a new connection to IP 1.2.3.100 should also get local port 1000 assigned with no delay.


But if there are only 600 of these devices I can't think of a reason not to just connect to all of them and hold and use the connections. The overhead is high, but connection thrashing is a killer. Most of the serious overhead is non-paged pool memory used by the sockets, but on a modern version of Windows (Vista or newer) this is less of an issue because it is managed better.

The memory manager in 32-bit Windows Vista and later, including Server 2008 and Windows 7 (there is no 32-bit version of Windows Server 2008 R2) doesn’t carve up the system address statically; instead, it dynamically assigns ranges to different types of memory according to changing demands. However, it still sets a maximum for nonpaged pool that’s based on the amount of physical memory, either slightly more than 75% of physical memory or 2GB, whichever is smaller.
Pushing the Limits of Windows: Paged and Nonpaged Pool
 
Is it OK to have all the 600 winsocks with the same name but a separate index?
This makes the code much simpler and easy to manage faults.
The Lantronix adapters (acting as servers) connect dramatically faster than a PC so the time wait seems to be associated with the 'connectee'

 
A control array should be fine.

The time you are talking about is probably unrelated to TIME-WAIT, which is a TCP connection state and not a time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top