VB automatically obtains the Server IP address and connects to the server (using UDP broadcast)

Source: Internet
Author: User

Recently, I am using VB To Write Remote Control for multiple users. I will write a new article for my thoughts ......

From: http://www.ibiancheng.cn/Article/VBNetArticle/200804/123.html

In the document that explains how to use network controls such as Winsock Controls, when using TCP or UDP to connect a client program to a server program, you must specify an IP address (or machine name) without exception) and port number, so how can the client automatically obtain the Server IP address and establish a connection when the port is known and the server program's machine address or name is unknown, I encountered this problem in actual programming and used the following methods to solve this problem.
This method adds two Winsock Controls to the server. One is used as the server, and the other is used to broadcast the IP address of the local machine through UDP. The client also obtains the IP address of the server through UDP, then, change the client to the TCP mode to initiate a connection to the server. The complete code is as follows:

'Add code to the server program (the program source is www.ibiancheng.cn)
'Create a project and put two Winsock Controls on the form, named winsock1 and Winsock2.
'The interval property of a timer control timer1 is set to 1000 (you can also set it yourself)
'--------------------- Broadcast the IP address of the local machine to the entire network ---------------------------
Dim meip as string 'stores the local IP Address
Dim netmask as string 'stores the local broadcast address
Dim maxconnext as long
Dim nowconnext () as Boolean
'---------------------------------------------------------
Private sub form_load ()
Winsock1.protocol = sckudpprotocol
Winsock1.remoteport = 9400 'Bind the remote port number to 9400
Meip = winsock1.localip 'get the local IP Address
Netmask = "127.0.0.255" 'is the broadcast address of the local machine. The broadcast address should be calculated based on your own IP address.
'-----------------------------------------
Winsock2 (0). localport = 1001
Winsock2 (0). Listen
End sub
'----------------------- Start to broadcast the IP address of the local machine to the entire network --------------------------
Private sub timerrenttimer () 'sends information to the broadcast address on this site
Winsock1.remotehost = netmask sends information to the broadcast address on this site, the content is the IP address of the Local Machine
Winsock1.senddata meip
End sub
Private sub winsock2_close (index as integer)
Winsock2 (INDEX). Close
Winsock2 (INDEX). localport = 1001
Winsock2 (INDEX). Listen
End sub
'The server receives a connection request from the client. The code here may be a bit problematic. Please write it again or contact me
Private sub winsock2_connectionrequest (index as integer, byval requestid as long)
On Error resume next
Dim I as long
If Index = 0 then' main Winsock -- Winsock (0) Message
I = 1
While I <= maxconnext and nowconnext (I) 'find the idle WinSock in the previous Winsock
I = I + 1
Doevents
Wend
If I <= maxconnext then 'indicates that a user has left the current mounted socket.
Winsock2 (I). localport = 0' use a ready-made socket
Winsock2 (I). Accept requestid 'receive
Nowconnext (I) = true 'winsock (I) is occupied
Else 'No user left in the socket currently loaded
Maxconnext = maxconnext + 1' the current receiving port is added
Load Winsock2 (maxconnext) 'to generate a new 'winsock
Winsock2 (maxconnext). localport = 0
Winsock2 (maxconnext). Accept requestid 'receive
Redim preserve nowconnext (maxconnext + 1)
Nowconnext (maxconnext) = true
End if
End if
End sub
'-------------------------------------------------------
'Client Program (Program Source: www.ibiancheng.cn)
'Create a project and put a Winsock Control on the form named winsock1.
'-----------------------------
Dim cansend as string 'Save the Server IP Address
'----------------------------
Private sub form_load ()
'--------------------------------------
Winsock1.protocol = sckudpprotocol
Winsock1.bind "9400" 'listens on port 9400
'----------------------------------------
Winsock2.remotehost = "1001"
End sub
'-----------------------------------------------------------
Private sub winsock1_dataarrival (byval bytestotal as long)
Doevents
Dim dataarrival as string
Dim CIP as string
Winsock1.getdata dataarrival
Cansend = dataarrival 'get the Server IP Address
Winsock1.close 'disconnect
Winsock1.localport = 0' change the port number to 0.
Winsock1.protocol = scktcpprotocol 'change the connection mode
Winsock1.remoteport = 1001 'set the server port number
Winsock1.remotehost = cansend 'request to connect to the server
Winsock1.connect
End sub

 

The main method used is UDP broadcast.

At the end of this article, we hope that the principles can be used as an example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.