Reputation: 1809
I have two Windows Forms applications, one acts as server (that is, Winform:Server role) and another one as a client (that is, Winform: Client role). In my LAN setup, there are 6 PCs and these PCs connected to each other via a 8-port switch and each PC has more than one LAN card.
There is one PC running [Winform: Server role] and five others running the [Winform: client role]. In [Winform: Server role], I'm using following code to obtain the local IP address and port number and the [Winform: Server role] will listen to all incoming TCP requests according to this auto-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 do all [Winform: Client role] know my [Winform: Server role] IP address and port number at run time?
I need to clarify my intention. My current approach to my intention maybe incorrect. I attempt to create a 'zero configuration client-server networking' and that is plug & play. The server will know where the client and vise versa. I know there is a program (that is, MaxiVista) has done that exactly.
MaxiVista has two applications, that is, server and client. Users only need to execute the server application in PC designated as server role and execute the client application in another PC designated as client role. Then the server will be able to find all executing clients in the same LAN.
My intention is just that. Plug and play 'zero configuration client-server networking' within the same LAN.
Upvotes: 6
Views: 4682
Reputation: 941317
Mapping from a well-known machine name to an IP address is done by a naming service, DNS is the standard one. Talk to your LAN administrator if you cannot get TcpClient.Connect(string, int) working.
You cannot afford to have the server start listening on port 0 like that. It must be a well-known port number or the clients will have no idea what port number to use in their Connect() call. Pick a number, any number, above a thousand. Like 1667. If there's a conflict with another TCP server running on that machine using the same port number then you'll find out. On the clients, the server name and port number belongs in a setting so that it can be easily changed by the LAN administrator.
Upvotes: 2
Reputation: 92752
Well, they don't, really.
You could configure DNS for e.g. yourappserver
to point to your server and then have the clients connect to that, but that is obviously a bit complicated (plus hard-coding the value isn't a great way to do this).
What you could use is some sort of service announcement - e.g. via mDNS. This works by having the server periodically announce "I'm a little server, short and stout (server of WhateverYourAppIsCalled on port 12345)" and your clients to listen for such requests, or even requesting them ("is there a server of WhateverYourAppIsCalled around here?"). See also this: http://en.wikipedia.org/wiki/Zero_configuration_networking#Service_discovery
(In a pinch, you could make the server broadcast its presence to the network and have the clients listen for such broadcasts, but then you're basically re-implementing mDNS)
Upvotes: 3
Reputation: 3374
There is no way for the clients to automatically figure out the IP address of the server. Here are some choices for solving your problem:
Upvotes: 2