Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 253
user774411 Avatar asked May 30 '11 16:05

user774411


People also ask

How do I find my IP address and port number?

The port number is “tacked on” to the end of the IP address, for example, “192.168. 1.67:80” shows both the IP address and port number.

Where do I find my port number?

How to find your port number on Windows. Type “Cmd” in the search box. Open Command Prompt. Enter the netstat -a command to see your port numbers.

What is IP address and port number?

The IP address refers to the Internet Protocol Address. These basically identify a host present in a network. We use Port numbers for identifying any process/ service present on your system. Bits Used. The size of IPv4 is 4 bytes (32 bits), and that of IPv6 is 16 bytes (128 bits).

Are IP addresses assigned to ports?

IP addresses are assigned to item data ports in the Web Client on an item's page while it is in Edit mode from the Data - Ports tab via a IP Address Assignments dialog.


1 Answers

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)

like image 195
Piskvor left the building Avatar answered Oct 13 '22 01:10

Piskvor left the building