Implementing client programs automatically looking up and connecting to server programs (Nanjing Audit Institute Education Technology Center 210029 Ding Guo Yong) In a general explanation of Winsock control, use TCP or UDP, client program connection server program If there is no exception to specify the IP address (or machine name) and the port number, then how do the client program initiates and establishes a client program in the case where the port is known and the server address or name is unknown, the author This is encountered in the actual programming and uses the following method to solve this problem. This method is smartly combines TCP with UDP, summarizing UDP lookup, TCP connection. For client programs, use three Winsock controls: 1 SockClient protocol SCKTCPPROTOCOL to initiate TCP connection to the server program 2 SockLink protocol SCKUDPPROTOCOL to send data 3 SockGetData protocol SCKUDPPROTOCOL for acceptance from the service server to return Information Defines a String Type Global Variable MEIP for storing the IP address of this machine. In the Form_Load () event, write the following procedure: DIM NetBCaddress as stringmeip = socklink.localip Get the broadcast address of this network, assume that the IP address is 192.168.100.215, the subnet mask is 255.255.255.0 The broadcast address of the network is 192.168.100.255, how to get the broadcast address from the native IP address, please readers yourself. SockGetData.bind "9400" 'Binds to local port 9400socklink.remotehost = netmask' Send information to this network broadcast address, the content is the IP address of this unit socklink.remoteport = 9401 SockLink.senddata "Dgy .." & meip in SockGetData DataArrival in this definition Private Sub sockgetdata_DataArrival (ByVal bytesTotal as Long) Dim strData as Stringsockgetdata.GetData strData 'data acquisition, the data for the server machine where the IP address of the program If Trim (strData) <> "" Thensockclient.RemoteHost = strData' customer The terminal program is connected to the server program.
SockClient.remotEport = 8092 SockClient.connectens IFEND SUB In the server program, you have to get the client's IP address from the broadcast address and send your IP address to the client, so that the client establishes it with yourself. Service program, Similarly, the following three Winsock controls: 1 SockServer control array protocol SCKTCPPROTOCOL TCP connection 2 SockGetData protocol SCKUDPPPPPPPPPPSEND protocol SCKUDPPROTOCOL is used to send a server program to the client from the broadcast address. IP address Form_load () in sockgetdata.Bind "9401" 'is defined as the structure and function of separating data for the listening port 9401 Option ExplicitPrivate Type UdpData ok as Boolean IP as StringEnd TypePrivate function UdpDataSwitch (UdpData as String) as UdpDataDim p As StringDim MyStrs As VariantDim strs As Variantp = "" MyStrs = Split (UdpData, "..") For Each strs In MyStrs If p = "dgy" Then UdpDataSwitch.ok = True UdpDataSwitch.IP = strs Exit Function End If p = strsNextUdpDataSwitch.ok = FalseUdpDataSwitch.IP = "" End Function in DataArrival event sockgetdata's Private Sub sockgetdata_DataArrival (ByVal bytesTotal As Long) DoEventsDim DataArrival As StringDim CanSend As BooleanDim Cip As Stringsockudplink.GetData DataArrivalCanSend = UdpDataSw itch (DataArrival) .okIf CanSend = True Then sockudpsend.RemoteHost = UdpDataSwitch (DataArrival) .IP sockudpsend.RemotePort = 9400 sockudpsend.SendData sockudpsend.LocalIP End IfEnd Sub connection request as to how to respond sockserver sockclient to see a general description of the programming winsock The article can get the answer. In this way, it is possible to complete the port of the server address or name unknown to the port, which is unknown to the machine address or name unknown, complete the connection of the client program and the server program. Regarding how the broadcast address of the network in VB (especially when dynamically obtain IP addresses), welcome everyone to let me discuss NetMuse@njai.edu.cn (Note: This broadcast address can use 255.255. 255.255, interested in trying a try)