Visual Basic (hereinafter referred to as VB) is an eleven powerful programming language. Especially after 4.0, OLE Automation technology is supported, which brings greater convenience to programming. Some time ago, I tried to write a web-enabled database. But since there is no Internet connection, I can't test it. So, I thought of OLEAutomation, which can be used to test network functions on one machine. After modification, you can also use it to connect through Modem. Next, I will introduce how to use Visual Basic to write a small network system.
----First, establish a support network OLEAutomation
----Start VB. Create a list box List1 in the form Form1, build a Frame1 above it, and set its Caption property to empty. Create a Label1 in the middle, and similarly set its Caption to empty. Finally, create a Label2 with the Caption UserList on List1. Finally, install a timer Timer1, set its Interval to 3000, and set Enabled to False. At this point, the form part of NetWorkConnection is completed.
----Then, select Options in the Tools menu of VB and fill in the various contents as required.
----Next, select Module in the Insert menu to create a new module Module1. Enter the following code in (General).
(UserInfo data type)
TypeUserInfo
UsernameAsString
AliasAsInteger
EndType
(maximum number of users)
PublicConstMaxUser=10
(define message)
PublicConstMsg_User_LogOn=1
PublicConstMsg_User_LogOff=2
(Set data type)
PublicUsers(MaxUser)AsUserInfo
PublicInbox(MaxUser)AsString
PublicUserSystemInboxAsInteger
PublicOnline(MaxUser)AsBoolean
Submain()
Form1.Show
EndSub
----UserInfo data type records the user name and alias of the logged in user. Only use aliases for display and communication. The username is only used to determine whether the user is valid. For security reasons, the above data cannot be accessed by users at will and must be accessed through the following subroutines.
----Select ClassModule in the Insert menu to create a new class Class1. Rename it to Common and set its properties.
----Fill in the following code.
----(Provides the function of obtaining user ID value. Users can use alias to return ID value through this function)
PublicFunctionGetUserID(AliasAsString)AsInteger
ForI=1ToMaxUser
IfUsers(I).Alias=AliasThenGetUserID=I
NextI
EndFunction
----(Provides the function of obtaining system information. Users can use it to know whether the user has made any changes)
PublicFunctionGetSystemMessage()AsInteger
GetSystemMessage=UserSystemInbox
EndFunction
----(Provides the function of obtaining user information. Use it to obtain the aliases of all online users, separated by "|".)
PublicFunctionGetUserInfo()AsString
ForI=1ToMaxUser
IfUsers(I).Username<>""Then
temp=temp Users(I).Alias "|"
EndIf
NextI
GetUserInfo=temp
EndFunction
----(Provides the function of obtaining user private information. Used to accept other users
information sent by the user. )
PublicFunctionGetUserMessage(IDAsInteger)AsString
IfID<=0OrID>MaxUserThen
ExitFunction
EndIf
GetUserMessage=Inbox(ID)
EndFunction
----(Provides logout function. Used to log out of the network.)
PublicFunctionLogOff(IDAsInteger)AsBoolean
IfID<=0OrID>MaxUserThen
LogOff=False
ExitFunction
EndIf
IfUsers(ID).Username<>""Then
Users(ID).Username=""
LogOff=True
Else
LogOff=False
EndIf
UserSystemInbox=Msg_User_LogOff
`--------------UpdateForm1------------
ForI=0ToForm1.List1.ListCount-1
IfForm1.List1.List(I)=Users(ID).AliasThen
`Find user aliases in List1 and delete
Form1.List1.RemoveItemI
ExitFor
EndIf
NextI
IfForm1.List1.ListCount=0Then`if no user is logged in
Form1.Label1.Caption="DisConnected"
Form1.timer1.Enabled=False
EndIf
EndFunction
----(Provides login function to access the Internet)
PublicFunctionLogOn(UsernameAsString,
AliasAsString)AsInteger
ForI=1ToMaxUser
IfUsers(I).Username=""Then
Users(I).Username=Username
Users(I).Alias=Alias
LogOn=I
UserSystemInbox=Msg_User_LogOn`Send "user login" information
`--------------UpdateForm1------------
Form1.List1.AddItemAlias`There are users online
Form1.Label1.Caption="Connected"
Form1.timer1.Enabled=True
ExitFunction
EndIf
NextI
LogOn=0
EndFunction
----(Provides the function of refreshing the user's online status flag. This enables the system to determine whether you are online. If this function is not called within 6 seconds, the system will automatically delete you.)
PublicSubRefresh(IDAsInteger)
IfID<=0OrID>MaxUserThenExitSub
Online(ID)=True
EndSub
----(Provides the function of sending users' private information. Used to transfer information to other users.)
PublicFunctionSendUserMessage(MessageAs
String,ToIDAsInteger)AsBoolean
IfToID<=0OrToID>MaxUserThen
SendUserMessage=False
ExitFunction
EndIf
Inbox(ToID)=Message
SendUserMessage=True
EndFunction
----Enter the remaining code in the Code of Form1.
(Initialize Form1)
PRivateSubForm_Load()
Label1.Caption="DisConnected"
Form1.Caption="NetWorkConnectedServer"
Form1.Show
ForI=1ToMaxUser
Users(I).Username=""
NextI
EndSub
----(Check whether the user is online regularly by judging the value of Online)
PrivateSubtimer1_Timer()
ForI=1ToMaxUser
IfUsers(I).Username<>""Then
IfOnline(I)=FalseThen
Fors=0ToList1.ListCount-1
IfList1.List(s)=Users(I).AliasThen
List1.RemoveItems
Users(I).Username=""
UserSystemInbox=Msg_User_LogOff
`Send "user logout" message
EndIf
Nexts
EndIf
Online(I)=False
EndIf
NextI
IfList1.ListCount=0Then
`If there is no user
Label1.Caption="DisConnected"
timer1.Enabled=False
EndIf
EndSub
----Run this program. Start another VB and start writing the user part. Arrange these controls as shown below in the default form.
----Fill in the following code
PublicIDAsInteger
PublicConnectedAsObject
PrivateSubCommand1_Click()`Login
DimusernameAsString
DimaliasAsString
SetConnected=CreateObject
("NetWorkConnection.Common")Start NetWorkConnection
username=Text1.Text
alias=Text2.Text
ID=Connected.logon(username,alias)`Log in and return the ID value
Timer1.Enabled=True
Command4_Click
EndSub
PrivateSubCommand2_Click()`Logout
x=Connected.logoff(ID)
Timer1.Enabled=False
Setx=Nothing`release the object
EndSub
PrivateSubCommand3_Click()`Send user information
DimTempIDAsInteger
DimTempStringAsString
DimxAsString
DimyAsBoolean
x=Combo1.Text
TempID=Connected.getuserid(x)`Get the ID value of the specified user
TempString=Text3.Text
y=Connected.sendusermessage(TempString,TempID)
EndSub
PrivateSubCommand4_Click()
ForI=0ToCombo1.ListCount1`Clear Combo1
Combo1.RemoveItem0
NextI
x=Connected.GetUserInfo`Receive user information
cd$=x
lastst=1
ForI=1ToLen(cd$)
IfMid$(cd$,I,1)="|"Then
Namef$=Mid$(cd$,lastst,I-lastst)
Combo1.AddItemNamef$` detach user alias and add to Combo1
lastst=I 1
EndIf
NextI
EndSub
PrivateSubForm_Load()
Timer1.Enabled=False
Timer1.Interval=300
EndSub
PrivateSubTimer1_Timer()
Connected.Refresh(ID)`Refresh user logo
x=Connected.GetSystemMessage()`Receive system information
y=Connected.GetUserMessage(ID)`Receive user information
Ify<>""Andy<>Label6.CaptionThenLabel6.Caption=y
Ifx<>Val(Label4.Caption)Then`Refresh Combo1
Label4.Caption=x
Command4_Click
EndIf
EndSub
----Start running. Enter your Username and Alias, click LogOn, and check the previous VB example to see if your name is included. If so, it proves that your "hub" is successful. At this time, no matter what the reason is for logged-in users to disconnect without using LogOff, the system will automatically delete these users after 6 seconds. Make sure other users are not affected.
----This program has been modified to support Modem functions. The user part of the program can remain intact. When compiling, select RemoteSupportFile in Options and use the included installation program to install it on the network server to truly achieve "networking". ->