Multiple Connection torcher please help!!

PiggyBank1974

Member
Joined
Sep 28, 2004
Messages
7
Hi there,

Im lost with this multiple connection, Ive managed to successfully to get a single client connected to my server, but rewriting it to take multiple connections is torcher :( , Ive had a look at many examples but ive become so confused to say the least.

Here is the ClsSocketServer
Code:
Public Class ClsSocketServer

 Dim m_Port As Int32
 Dim m_Backlog As Int32
 Dim MyServerSocket As System.net.sockets.Socket
 Dim BindAddress As System.Net.IPAddress
 Dim BindEndPoint As System.Net.IPEndPoint
 Dim ipadds As ClsIPAddresses

Event Declarations:
 Event OnServerStarted(ByVal Address As String, ByVal Port As Int32)
 Event OnServerStop()
 Event OnConnectionRequest()

 Public Sub New()
  ipadds = New ClsIPAddresses
 End Sub

 <System.ComponentModel.DefaultValue(CInt(6000)), System.ComponentModel.Description("Sets the Port Number to use")> _
 Public Property PortNumber() As Int32
  Get
     Return m_Port
  End Get
  Set(ByVal Value As Int32)
   If Value < System.Net.IPEndPoint.MinPort Or Value > System.Net.IPEndPoint.MaxPort Then
     MessageBox.Show("You must enter a Port Number between " & CStr(System.Net.IPEndPoint.MinPort) & " and " & CStr(System.Net.IPEndPoint.MaxPort), "Port Number Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
     Exit Property
   End If

   m_Port = Value
  End Set
End Property

 <System.ComponentModel.DefaultValue(CInt(5)), System.ComponentModel.Description("Sets the Simultaneous Connections to use")> _
 Public Property SimultaneousConnections() As Int32
  Get
     Return m_Backlog
  End Get
  Set(ByVal Value As Int32)
   If Value < 1 Or Value > System.Net.Sockets.SocketOptionName.MaxConnections Then
     MessageBox.Show("You must enter a Simultaneous Connections between 1 and " & CStr(System.Net.Sockets.SocketOptionName.MaxConnections), "Simultaneous Connections Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
     Exit Property
   End If

   m_Backlog = Value
  End Set
End Property

 Public Sub StartServer()

  BindAddress = System.Net.IPAddress.Parse(ipadds.HostIPAddress(ClsIPAddresses.Addresses.NetworkIP))
  BindEndPoint = New System.Net.IPEndPoint(BindAddress, m_Port)
  MyServerSocket = New System.Net.sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)

  MyServerSocket.Bind(BindEndPoint)

  MyServerSocket.Listen(m_Backlog)

  RaiseEvent OnServerStarted(ipadds.HostIPAddress(ClsIPAddresses.Addresses.NetworkIP), m_Port)

  MyServerSocket.BeginAccept(New AsyncCallback(AddressOf ConnectionRequest), MyServerSocket)

 End Sub

 Public Sub StopServer()
  MyServerSocket.Shutdown(Net.Sockets.SocketShutdown.Both)
  MyServerSocket.Close()
  RaiseEvent OnServerStop()
 End Sub

 Public Sub ConnectionRequest(ByVal ar As IAsyncResult)
 Dim Local_ClientSocket As System.net.sockets.Socket

  Local_ClientSocket = CType(ar.AsyncState, System.net.sockets.Socket)


  MyClientSocket = Local_ClientSocket.EndAccept(ar)

 Try
   If MyClientSocket.Connected = False Then
     Console.WriteLine("Connection refused.", Err.Number)
     Exit Sub
   Else
       MyClientSocket.BeginReceive(Buffer, 0, BufferSize, 0, AddressOf OnData_Arrival, MyClientSocket)
   End If
 Catch
   Exit Sub
 End Try

  RaiseEvent OnConnectionRequest()

  MyServerSocket.BeginAccept(New AsyncCallback(AddressOf ConnectionRequest), MyServerSocket)

 End Sub

End Class

Ive commented out some of the client connection code as this will need changing for mulitple connections (I think).


And here is the Address Class ClsIPAddresses

Code:
Public Class ClsIPAddresses
 Dim AddressArray() As String
 Dim HostName As String

 Public Enum Addresses
  NetworkIP = 0
  InterenetIP = 1
 End Enum

 Public Sub New()
  HostName = System.Net.Dns.GetHostName()

  ReDim AddressArray(System.Net.Dns.GetHostByName(HostName).AddressList.Length - 1)

  For A As Int32 = 0 To System.Net.Dns.GetHostByName(HostName).AddressList.Length - 1
     AddressArray(A) = System.Net.Dns.GetHostByName(HostName).AddressList(A).ToString
  Next A
 End Sub

 Public Function HostIPAddress(ByVal Address As Addresses) As String

  Select Case Address
   Case ClsIPAddresses.Addresses.NetworkIP
    Return AddressArray(0)
   Case ClsIPAddresses.Addresses.InterenetIP
    Return AddressArray(1)
  End Select

 End Function

End Class

I know its not relatively hard, but its one of these things thats hard to get your head around to change Im not sure of several things, and inevitably change it.

Not sure abouts

(1) when a person connects, i need a way to figure out after they have sent data who they are(ID number), so i can direct this to the right Table for the data to get parased(Threads).

(2) Can i make a ClsClientSocket class but have this indexed. but if i do how do i know the person who has sent the data.

as you can see im going way ahead of myself but i must to find out which one my work.

Please help even if you think its not worth mentioning something it may put me on the right track.

The pig.....
 
Back
Top