How to get the IP address and port number assigned by .NET.

I have two Windows Forms applications, one acting as a server (i.e. Winform: server role ), and the other acting as a client (i.e. Winform: client role >). There are 6 PCs on my local network, and these PCs are connected to each other via an 8-port switch, and each PC has more than one LAN card.

There is one computer running [Winform: server role], and the other five - the [Winform: client] role. In [Winform: Server role], I use the following code to get the local IP address and port number, and [Winform: Server role] will listen for all incoming TCP requests according to this automatically assigned IP address and port number.

Dim Listener As System.Net.Sockets.TcpListener Dim Client As New System.Net.Sockets.TcpClient Dim Message As String = "" Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 0) Listener.Start() End Sub 

How does everyone [Winform: Client role] know my [Winform: Server role] IP address and port number at runtime?

I need to clarify my intention. My current approach to my intention may be wrong. I am trying to create a "zero client-server network configuration" and it is plug and play. The server will know where the client is and vice versa. I know that there is a program (that is, MaxiVista) did it this way.

MaxiVista has two applications, that is, a server and a client. Users only need to run the server application on the PC designated as the server role and run the client application on another PC designated as a client. Then the server will be able to find all executable clients on the same local network.

That is my intention. Plug and play with a “zero client-server network configuration” within the same local network.

+7
source share
3 answers

Well, they really don't.

You can configure DNS, for example. yourappserver to point to your server and then connect clients to it, but this is obviously a bit more complicated (plus hard coding is not the best way to do this).

What you can use is some kind of service declaration - for example. through mDNS . This works when the server periodically reports "I am a small server, short and full (WhateverYourAppIsCalled server on port 12345)" and your clients to listen to such requests or even request them ("is there a WhateverYourAppIsCalled server around here?"). See Also: http://en.wikipedia.org/wiki/Zero_configuration_networking#Service_discovery

(As a last resort, you can force the server to broadcast its presence on the network and force clients to listen to such broadcasts, but then you basically reprofile mDNS)

+3
source

Clients do not have the ability to automatically determine the IP address of the server. Here are some solutions to your problem:

  • Each client has some initialization data that tells him where the server is located (there may be a database, ini file, etc.). This data is likely to be stored locally.
  • There are some initialization data on the server that tells where the clients are. The server can then use this information to either directly contact the client, or write its own IP address to a file / database on the client machine that the client is looking for.
+2
source

Mapping from a known machine name to an IP address is done by the naming service, and DNS is done by the standard one. Contact your local network administrator if you cannot get TcpClient.Connect (string, int) to work.

You cannot afford to start listening to the server on port 0. It must be a known port number, or clients will not know which port number to use in the Connect () call. Choose a number, any number in excess of a thousand. Like 1667. If there is a conflict with another TCP server running on this computer using the same port number, you will find out about it. On clients, the server name and port number belong to the parameter, so it can be easily changed by the administrator of the local network.

+2
source

All Articles