home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Server_Key3610311192001.psc / Server.ctl < prev   
Encoding:
Text File  |  2000-11-06  |  6.6 KB  |  207 lines

  1. VERSION 5.00
  2. Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
  3. Begin VB.UserControl Server 
  4.    CanGetFocus     =   0   'False
  5.    ClientHeight    =   420
  6.    ClientLeft      =   0
  7.    ClientTop       =   0
  8.    ClientWidth     =   420
  9.    InvisibleAtRuntime=   -1  'True
  10.    Picture         =   "Server.ctx":0000
  11.    ScaleHeight     =   420
  12.    ScaleWidth      =   420
  13.    ToolboxBitmap   =   "Server.ctx":0974
  14.    Begin MSWinsockLib.Winsock Winsock 
  15.       Index           =   1
  16.       Left            =   -120
  17.       Top             =   -120
  18.       _ExtentX        =   741
  19.       _ExtentY        =   741
  20.       _Version        =   393216
  21.    End
  22. End
  23. Attribute VB_Name = "Server"
  24. Attribute VB_GlobalNameSpace = False
  25. Attribute VB_Creatable = True
  26. Attribute VB_PredeclaredId = False
  27. Attribute VB_Exposed = False
  28. '------------------------------------------
  29. '---------Written by Drew Lederman---------
  30. '------------------------------------------
  31.  
  32. Option Explicit
  33. 'Event Declarations:
  34. Event DataArrival(ByVal SckIndex As Integer, ByVal Data As String, ByVal bytesTotal As Long, ByVal RemoteIP As String, ByVal RemoteHost As String)
  35. Event Error(ByVal SckIndex As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String)
  36. Attribute Error.VB_Description = "Error occurred"
  37. Event SocketOpened(ByVal SckIndex As Integer, ByVal LocalPort As Long, ByVal RemoteIP As String, ByVal RemoteHost As String)
  38. Event SocketClosed(ByVal SckIndex As Integer, ByVal LocalPort As Long, ByVal RemoteIP As String, ByVal RemoteHost As String)
  39. Event ServerStarted()
  40. Event ServerStopped()
  41. Event StartFailed()
  42. 'Default Property Values:
  43. Const m_def_State = "Closed"
  44. 'Property Variables:
  45. Dim m_State As String
  46.  
  47.  
  48.  
  49. Public Property Get ServerPort() As Long
  50.     ServerPort = Winsock(1).LocalPort
  51. End Property
  52.  
  53. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  54. 'MemberInfo=0
  55. Public Function StartServer(LocalPort As Long, Optional LocalIP As String) As Boolean
  56. Attribute StartServer.VB_Description = "Starts server process"
  57.     On Error GoTo errhandle
  58.     
  59.     If LocalIP = "" Then LocalIP = Winsock(1).LocalIP
  60.     
  61.     'Open the server port
  62.     Winsock(1).Close
  63.     Winsock(1).Bind LocalPort, LocalIP
  64.     Winsock(1).Listen
  65.     
  66.     'Wait until it is open
  67.     Do While Winsock(1).State <> sckListening: DoEvents
  68.         If Winsock(1).State = sckError Then StartServer = False: RaiseEvent StartFailed: Exit Function
  69.     Loop
  70.     
  71.     m_State = "Running"
  72.     
  73.     StartServer = True
  74.     RaiseEvent ServerStarted
  75.     Exit Function
  76. errhandle:
  77.     StartServer = False
  78.     RaiseEvent StartFailed
  79. End Function
  80.  
  81. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  82. 'MemberInfo=0
  83.  
  84. Public Function StopServer() As Boolean
  85. Attribute StopServer.VB_Description = "Stops server process"
  86.     Winsock(1).Close
  87.     
  88.     On Error Resume Next
  89.     Dim x As Integer
  90.     'close each connection and unload the winsock control
  91.     For x = 2 To Winsock.UBound
  92.         Winsock(x).Close
  93.         Unload Winsock(x)
  94.     Next x
  95.     On Error GoTo 0
  96.     
  97.     m_State = "Closed"
  98.     RaiseEvent ServerStopped
  99. End Function
  100.  
  101. Private Sub UserControl_Initialize()
  102.     m_State = m_def_State
  103. End Sub
  104.  
  105. Private Sub UserControl_Resize()
  106.     UserControl.Height = 420
  107.     UserControl.Width = 420
  108. End Sub
  109.  
  110. Private Sub Winsock_Close(Index As Integer)
  111.     If Index <> 0 Then
  112.         With Winsock(Index)
  113.         RaiseEvent SocketClosed(Index, .LocalPort, .RemoteHostIP, .RemoteHost)
  114.         End With
  115.         'unload when the connection is closed
  116.         Unload Winsock(Index)
  117.     End If
  118. End Sub
  119.  
  120. Private Sub Winsock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  121.     On Error GoTo errhandle
  122.     
  123.     Dim nIndex As Long
  124.     If Index = 1 Then
  125.         'set nIndex to the max winsock index, so we dont get errors
  126.         nIndex = Winsock.UBound + 1
  127.         'load a new winsock to accept the request
  128.         '(this is what enables mutiple connections)
  129.         Load Winsock(nIndex)
  130.         Winsock(nIndex).Accept (requestID)
  131.         
  132.         With Winsock(nIndex)
  133.         RaiseEvent SocketOpened(nIndex, .LocalPort, .RemoteHostIP, .RemoteHost)
  134.         End With
  135.     End If
  136.     
  137.     Exit Sub
  138. errhandle:
  139.     
  140. End Sub
  141. '
  142. ''WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  143. ''MemberInfo=8,1,2,0
  144. Public Property Get ConnectionCount() As Long
  145.    ConnectionCount = Winsock.Count - 1
  146. End Property
  147.  
  148.  
  149. Private Sub Winsock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
  150.     Dim strData As String
  151.     Winsock(Index).GetData strData, , bytesTotal
  152.     'pass the data to the control's dataarrival event
  153.     RaiseEvent DataArrival(Index, strData, bytesTotal, Winsock(Index).RemoteHostIP, Winsock(Index).RemoteHost)
  154. End Sub
  155.  
  156.  
  157. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  158. 'MemberInfo=5
  159. Public Sub SendData(ByVal Data As Variant, ByVal SckIndex As Long)
  160. Attribute SendData.VB_Description = "Send data to a  remote computer."
  161.     On Error Resume Next
  162.     Winsock(SckIndex).SendData Data
  163. End Sub
  164.  
  165.  
  166.  
  167. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  168. 'MemberInfo=13,1,2,
  169. Public Property Get State() As String
  170. Attribute State.VB_Description = "Returns the state of the server."
  171. Attribute State.VB_MemberFlags = "400"
  172.     State = m_State
  173. End Property
  174.  
  175.  
  176.  
  177. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  178. 'MemberInfo=5
  179. Public Sub CloseSocket(SckIndex As Integer)
  180.      On Error Resume Next
  181.         Winsock(SckIndex).Close
  182. End Sub
  183.  
  184. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  185. 'MemberInfo=13
  186. Public Function GetRemoteHost(SckIndex As Integer) As String
  187. Attribute GetRemoteHost.VB_Description = "Returns the remote host name of the specified socket."
  188.     On Error Resume Next
  189.         GetRemoteHost = Winsock(SckIndex).RemoteHost
  190. End Function
  191.  
  192. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  193. 'MemberInfo=13
  194. Public Function GetRemoteIE = !
  195. oteaTBed Functivh0e)
  196.   Vnin GetRemoYavAs Inte
  197.   Bo OiTpoNol_InitializeIG COMMENTED LINESvaiptionG MODIlm x As Integer
  198.   Bipt:Bros2pv  Vnin GetRemoYavAs Inte
  199.   Bo OiTpoNol_InitializeIG CBpavAVSD0nin GetRemoY"_Initialgs('MemberInfo5 As St   iOSemberol's dataarrivos('Memberh0e)
  200.   Vnin5
  201. PteOit;
  202. '
  203. OWIdexWckInde(insock(In GetRemoYavAs Inte
  204.   Bo OiTpoNor OiTpoNol_
  205. EndBo ".ibMENTl
  206. 'WARNINgs('MemberInfo5 As EepoNor OiTplPort As L aB_Description = "ReturnTEDit;
  207. '
  208. usnin GetRemoYDescriptT    iptT    #WARNINgs('oeOWING COMMENTEd #WARNINgsb BiRemoYe('oeOWINGrNal byt #WrcionWrcionWrcionWrcionWrcionWrcionWrcionWrcionWrcionWrcionWrcionWrcion