Results 1 to 5 of 5

Thread: [RESOLVED] Intended for Griminal

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Resolved [RESOLVED] Intended for Griminal

    Per my message; here's the rough code you can attempt to pull apart and/or finish. It was too large to copy and there's no attachment feature. In 2005, you don't have to do alot of this (mainly; inheriting the arraylist and creating your own collection object. Deffinitely use a Generic instead). I started writing it in 1.1 and don't have the old IDE anymore to finish it:
    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System
    5. Imports System.Net
    6. Imports System.Net.Sockets
    7.  
    8. Public Class SocketClients
    9.     Inherits ArrayList
    10.  
    11.     Protected Shared RequestPort As Int32
    12.     Private _PortRange(0) As Port
    13.     Private _ReqListener As Net.Sockets.TcpListener
    14.     Private _LocalEP As Net.IPEndPoint
    15.     Private _thrdPend As New Threading.Thread(AddressOf WaitForPending)
    16.  
    17.     Public Event NewClient(ByVal obj As Client)
    18.  
    19.     Private Structure Port
    20.         Public PortNum As Int32
    21.         Public avaliable As Boolean
    22.         Public Sub New(ByVal num As Int32, ByVal avail As Boolean)
    23.             PortNum = num
    24.             avaliable = avail
    25.         End Sub
    26.     End Structure
    27.  
    28.     Public Sub New(ByVal RequestPortIn As Int32, ByVal PortStart As Int32, ByVal PortEnd As Int32)
    29.         MyBase.New()
    30.         If RequestPort >= PortStart And RequestPort <= PortEnd Then
    31.             Throw New ArgumentException("Request port cannot be in the Port pool", "RequestPort")
    32.         ElseIf PortStart > PortEnd Then
    33.             Throw New ArgumentException("PortStart cannot be prior to PortEnd", "PortStart")
    34.         ElseIf PortStart < 49152 Then
    35.             Throw New ArgumentException("""Well-Known"" and ""DCCP Well-Known"" ports cannot be used dynamically.", "PortStart")
    36.         ElseIf PortEnd > 65535 Then
    37.             Throw New ArgumentException("Requested Range is out of scope.", "PortEnd")
    38.         Else
    39.             Try
    40.                 For i As Int32 = PortStart To PortEnd
    41.                     _PortRange(UBound(_PortRange)) = New Port(i, True)
    42.                     If Not i = PortEnd Then
    43.                         ReDim Preserve _PortRange(_PortRange.Length)
    44.                     End If
    45.                 Next i
    46.  
    47.                 RequestPort = RequestPortIn
    48.                 _LocalEP = New Net.IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0), RequestPort)
    49.                 _ReqListener = New Net.Sockets.TcpListener(_LocalEP)
    50.             Catch ex As Exception
    51.                 Stop        'XXX DEBUG XXX
    52.             End Try
    53.         End If
    54.     End Sub
    55.  
    56.     Public Sub StartListen()
    57.         _ReqListener.Start()
    58.         Select Case _thrdPend.ThreadState
    59.             Case Threading.ThreadState.Unstarted
    60.                 _thrdPend.Start()
    61.             Case Threading.ThreadState.Running
    62.                 'Do nothing, already running
    63.             Case Threading.ThreadState.Suspended
    64.                 _thrdPend.Resume()
    65.             Case Threading.ThreadState.SuspendRequested
    66.                 _thrdPend.Resume()
    67.        End Select
    68.     End Sub
    69.  
    70.     Public Sub StopListen()
    71.         _ReqListener.Stop()
    72.         _thrdPend.Abort()
    73.     End Sub
    74.  
    75.     Private Sub WaitForPending()
    76.         While Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.StopRequested Or _
    77.             Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.AbortRequested
    78.             If Threading.Thread.CurrentThread.ThreadState = Threading.ThreadState.SuspendRequested Then
    79.                 Threading.Thread.CurrentThread.Suspend()
    80.             Else
    81.                 If _ReqListener.Pending Then
    82.                     SyncLock Threading.Thread.CurrentThread     'Begin Negotiation
    83.                         Dim socTemp As Net.Sockets.Socket = _ReqListener.AcceptSocket
    84.                         Dim holdPort As Port
    85.                         Dim buffer() As Byte
    86.  
    87.                         For i As Int32 = 0 To UBound(_PortRange, 1) - 1
    88.                             If _PortRange(i).avaliable = True Then 'According to our system, it's free
    89.                                 If validateport(_PortRange(i).PortNum) Then
    90.                                     holdPort = _PortRange(i)
    91.                                     _PortRange(i).avaliable = False
    92.                                     i = UBound(_PortRange, 1) - 1   'Seems more clever then an "Exit For"
    93.                                 Else                        'Some other program must have it, reflect in our array and continue
    94.                                     _PortRange(i).avaliable = False
    95.                                 End If
    96.                             End If
    97.                         Next i
    98.  
    99.                         If holdPort.PortNum = 0 Then
    100.                             buffer = Text.Encoding.ASCII.GetBytes("NOAVAIL")
    101.                         Else
    102.                             Dim client As New Client(socTemp.RemoteEndPoint.Serialize.Item(4) & "." & _
    103.                                 socTemp.RemoteEndPoint.Serialize.Item(5) & "." & _
    104.                                 socTemp.RemoteEndPoint.Serialize.Item(6) & "." & _
    105.                                 socTemp.RemoteEndPoint.Serialize.Item(7), holdPort.PortNum)
    106.                              buffer = Text.Encoding.ASCII.GetBytes("S:" & holdPort.PortNum)
    107.                             Me.Add(client, True)
    108.                         End If
    109.                         socTemp.Send(buffer, buffer.Length, SocketFlags.None)
    110.                         socTemp.Shutdown(SocketShutdown.Receive)
    111.                     End SyncLock
    112.                 End If
    113.             End If
    114.         End While
    115.  
    116.         Select Case Threading.Thread.CurrentThread.ThreadState
    117.  
    118.             Case Threading.ThreadState.AbortRequested
    119.  
    120.                 Threading.Thread.CurrentThread.Abort()
    121.  
    122.         End Select
    123.  
    124.     End Sub

  2. #2

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Intended for Griminal

    VB Code:
    1. Private Function validateport(ByVal port As Int32) As Boolean
    2.  
    3.         Try
    4.  
    5.             Dim soc As New Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    6.  
    7.             soc.Bind(New IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0).GetHashCode, port))
    8.  
    9.             soc = Nothing
    10.  
    11.             Return True
    12.  
    13.         Catch ex As Exception
    14.  
    15.             Return False
    16.  
    17.         End Try
    18.  
    19.     End Function
    20.  
    21.  
    22.  
    23. #Region "Shadows"
    24.  
    25.     Public Shadows Function Add(ByVal value As Client, Optional ByVal Connect As Boolean = False) As Integer
    26.  
    27.         Return MyBase.Add(value)
    28.  
    29.         RaiseEvent NewClient(value)
    30.  
    31.         If Connect Then
    32.  
    33.             value.Connect()
    34.  
    35.         End If
    36.  
    37.     End Function
    38.  
    39.  
    40.  
    41.     Default Public Shadows Property Item(ByVal index As Integer) As Client
    42.  
    43.         Get
    44.  
    45.             Return DirectCast(MyBase.Item(index), Client)
    46.  
    47.         End Get
    48.  
    49.         Set(ByVal Value As Client)
    50.  
    51.             MyBase.Item(index) = Value
    52.  
    53.         End Set
    54.  
    55.     End Property
    56.  
    57.  
    58.  
    59.     Public Shadows Function GetRange(ByVal index As Integer, ByVal count As Integer) As SocketClients
    60.  
    61.         Return DirectCast(MyBase.GetRange(index, count), SocketClients)
    62.  
    63.     End Function
    64.  
    65.  
    66.  
    67.     Public Shadows Function Contains(ByVal item As Client) As Boolean
    68.  
    69.         Return MyBase.Contains(item)
    70.  
    71.     End Function
    72.  
    73.  
    74.  
    75.     Public Shadows Function IndexOf(ByVal value As Client) As Integer
    76.  
    77.         Return MyBase.IndexOf(value)
    78.  
    79.     End Function
    80.  
    81.  
    82.  
    83.     Public Shadows Function IndexOf(ByVal value As Client, ByVal startIndex As Integer) As Integer
    84.  
    85.         Return MyBase.IndexOf(value, startIndex)
    86.  
    87.     End Function
    88.  
    89.  
    90.  
    91.     Public Shadows Function IndexOf(ByVal value As Client, ByVal startIndex As Integer, ByVal count As Integer) As Integer
    92.  
    93.         Return MyBase.IndexOf(value, startIndex, count)
    94.  
    95.     End Function
    96.  
    97.  
    98.  
    99.     Public Shadows Sub Insert(ByVal index As Integer, ByVal value As Client)
    100.  
    101.         MyBase.Insert(index, value)
    102.  
    103.     End Sub
    104.  
    105.  
    106.  
    107.     Public Shadows Sub InsertRange(ByVal index As Integer, ByVal c As SocketClients)
    108.  
    109.         MyBase.InsertRange(index, c)
    110.  
    111.     End Sub
    112.  
    113.  
    114.  
    115.     Public Shadows Sub AddRange(ByVal c As SocketClients)
    116.  
    117.         MyBase.AddRange(c)
    118.  
    119.     End Sub
    120.  
    121.  
    122.  
    123.     Public Shadows Function LastIndexOf(ByVal value As Client) As Integer
    124.  
    125.         Return MyBase.LastIndexOf(value)
    126.  
    127.     End Function
    128.  
    129.  
    130.  
    131.     Public Shadows Function LastIndexOf(ByVal value As Client, ByVal startIndex As Integer) As Integer
    132.  
    133.         Return MyBase.LastIndexOf(value, startIndex)
    134.  
    135.     End Function
    136.  
    137.  
    138.  
    139.     Public Shadows Function LastIndexOf(ByVal value As Client, ByVal startIndex As Integer, ByVal count As Integer) As Integer
    140.  
    141.         Return MyBase.LastIndexOf(value, startIndex, count)
    142.  
    143.     End Function
    144.  
    145.  
    146.  
    147.     Public Shadows Sub Remove(ByVal obj As Client)
    148.  
    149.         MyBase.Remove(obj)
    150.  
    151.     End Sub
    152.  
    153.  
    154.  
    155.     Public Shadows Sub SetRange(ByVal index As Integer, ByVal c As SocketClients)
    156.  
    157.         MyBase.SetRange(index, c)
    158.  
    159.     End Sub
    160.  
    161.  
    162.  
    163.     Public Shadows Function Clone() As SocketClients
    164.  
    165.         Return DirectCast(MyBase.Clone, SocketClients)
    166.  
    167.     End Function
    168.  
    169. #End Region
    170.  
    171.  
    172.  
    173.     Public Class Client
    174.  
    175.         Private _IPAddress As IPAddress
    176.  
    177.         Private _RemEP As IPEndPoint
    178.  
    179.         Private _LocEP As IPEndPoint
    180.  
    181.         Private _soc As Net.Sockets.Socket
    182.  
    183.         Private _UserName As String
    184.  
    185.         Private _State As ConnectedState
    186.  
    187.  
    188.  
    189.         Public Enum ConnectedState
    190.  
    191.             NotConnected = 0
    192.  
    193.             Connecting = 1
    194.  
    195.             Connected = 2
    196.  
    197.             Errors = 3
    198.  
    199.         End Enum
    200.  
    201.  
    202.  
    203.         Private regexIP As New System.Text.RegularExpressions.Regex("^(25[0-5]|2[0-4]" & _
    204.  
    205.             "[0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]" & _
    206.  
    207.             "[0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
    208.  
    209.  
    210.  
    211.         Public Sub New(ByVal strIP As String, ByVal intPort As Int32)
    212.  
    213.             Try
    214.  
    215.                 If regexIP.IsMatch(strIP) Then
    216.  
    217.                     _IPAddress = New IPAddress(IPAddress.Parse(strIP).GetHashCode)
    218.  
    219.                     _RemEP = New IPEndPoint(_IPAddress, SocketClients.RequestPort)
    220.  
    221.                     _LocEP = New IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0), intPort)
    222.  
    223.                     _State = ConnectedState.NotConnected
    224.  
    225.                 Else
    226.  
    227.                     _State = ConnectedState.Errors
    228.  
    229.                     Throw New System.ArgumentException(strIP & " is not a valid IP Format.", "strIP")
    230.  
    231.                 End If
    232.  
    233.             Catch ex As ArgumentException
    234.  
    235.                 _State = ConnectedState.Errors
    236.  
    237.                 Throw New System.ArgumentException(strIP & " is not a valid IP Format.", "strIP", ex)
    238.  
    239.             End Try
    240.  
    241.         End Sub
    242.  
    243.         Public Sub New(ByVal lngIP As Long, ByVal intPort As Int32)
    244.  
    245.             Try
    246.  
    247.                 _IPAddress = New IPAddress(lngIP)
    248.  
    249.                 _RemEP = New IPEndPoint(_IPAddress, SocketClients.RequestPort)
    250.  
    251.                 _LocEP = New IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0), intPort)
    252.  
    253.                 _State = ConnectedState.NotConnected
    254.  
    255.             Catch ex As ArgumentException
    256.  
    257.                 _State = ConnectedState.Errors
    258.  
    259.                 Throw New ArgumentException(lngIP & " is not a valid IP Format.", "lngIP", ex)
    260.  
    261.             End Try
    262.  
    263.         End Sub
    264.  
    265.  
    266.  
    267.         Public Sub CreateSocket(ByVal addressFamily As Net.Sockets.AddressFamily, ByVal socketType As Net.Sockets.SocketType, ByVal protocolType As Net.Sockets.ProtocolType)
    268.  
    269.             _soc = New System.Net.Sockets.Socket(addressFamily, socketType, protocolType)
    270.  
    271.         End Sub
    272.  
    273.  
    274.  
    275. #Region "Connection State"
    276.  
    277.         Public Sub Connect()
    278.  
    279.             Try
    280.  
    281.                 _State = ConnectedState.Connecting
    282.  
    283.                 If _soc Is Nothing Then
    284.  
    285.                     CreateSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    286.  
    287.                 End If
    288.  
    289.                 _soc.Bind(_LocEP)
    290.  
    291.  
    292.  
    293.                 _State = ConnectedState.Connected
    294.  
    295.             Catch ex As Exception
    296.  
    297.                 _State = ConnectedState.Errors
    298.  
    299.             End Try
    300.  
    301.         End Sub
    302.  
    303.         Public Sub Disconnect()
    304.  
    305.             'TODO:
    306.  
    307.         End Sub
    308.  
    309. #End Region
    310.  
    311.  
    312.  
    313.         Public ReadOnly Property IPAddress() As IPAddress
    314.  
    315.             Get
    316.  
    317.                 Return _IPAddress
    318.  
    319.             End Get
    320.  
    321.         End Property
    322.  
    323.  
    324.  
    325.         Public ReadOnly Property RemoteEndPoint() As IPEndPoint
    326.  
    327.             Get
    328.  
    329.                 Return _RemEP
    330.  
    331.             End Get
    332.  
    333.         End Property
    334.  
    335.  
    336.  
    337.         Public ReadOnly Property UserName() As String
    338.  
    339.             Get
    340.  
    341.                 Return _UserName
    342.  
    343.             End Get
    344.  
    345.         End Property
    346.  
    347.  
    348.  
    349.         Public ReadOnly Property State() As ConnectedState
    350.  
    351.             Get
    352.  
    353.                 Return _State
    354.  
    355.             End Get
    356.  
    357.         End Property
    358.  
    359.  
    360.  
    361.     End Class
    362.  
    363. End Class

  3. #3

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Intended for Griminal

    That being said, there's alot of things I would have done differently structurely in here. It's very very rough, but might give you some ideas.

  4. #4

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Intended for Griminal

    I know which thread you're talking about now:
    http://www.vbforums.com/showthread.php?p=2517452

    That example was also in 1.1. Don't use it char for char in 2005 (I would imagine you'd get some design time errors?). And if I remember.... I think the snippet above is an extension of that example I posted in the link. Alot of this should look sort of familiar? It's been half a year since I looked at any of this.

  5. #5
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: [RESOLVED] Intended for Griminal

    PM?
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width