Results 1 to 25 of 25

Thread: Delegates *Given Up*

  1. #1

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Delegates *Given Up*

    I have Two classes that I am porting from C#, SocketClient and SocketServer both Delegate Declarations are the same

    VB Code:
    1. 'Delegate Declarations
    2.  
    3. Public Delegate Sub MESSAGE_HANDLER(ByRef pSocket As SocketClient)
    4.  
    5.         Public Delegate Sub CLOSE_HANDLER(ByRef pSocket As SocketClient)
    6.  
    7.         Public Delegate Sub ERROR_HANDLER(ByRef pSocket As SocketClient, ByRef pException As Exception)
    8.  
    9.         Private GetMessageHandler As MESSAGE_HANDLER
    10.  
    11.         Private GetCloseHandler As CLOSE_HANDLER
    12.  
    13.         Private GetErrorHandler As ERROR_HANDLER

    This is how its done in C#
    Code:
    'here is where i run into problems
    
                  pClientSocket = AddSocket(
    				new SocketClient(this, 
                    pSocket,
                    pSocket.RemoteEndPoint.ToString().Substring(0,15), 
                    GetPort,
                    GetSizeOfRawBuffer, 
                    GetUserArg,
                    new SocketClient.MESSAGE_HANDLER(GetMessageHandler), 
                    new SocketClient.CLOSE_HANDLER(GetCloseHandler), 
                    new SocketClient.ERROR_HANDLER(GetErrorHandler)));
    I cant figure out how do to this in vb.net

    any ideas?
    Last edited by <ABX; Mar 11th, 2004 at 09:51 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You want to convert the C# code to VB.NET?

  3. #3

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    ya, this is the part that i cant figure out be
    Code:
    new SocketClient.MESSAGE_HANDLER(GetMessageHandler), 
    new SocketClient.CLOSE_HANDLER(GetCloseHandler), 
    new SocketClient.ERROR_HANDLER(GetErrorHandler))
    the constuctor is declared like this:

    Code:
    SocketServer pSocketServer, 
    Socket pClientSocket, 
    String strIpAddress, 
    Int16 iPort,
    Int32 iSizeOfRawBuffer,
    Object pUserArg,
    MESSAGE_HANDLER pfnMessageHandler,
    CLOSE_HANDLER pfnCloseHandler,
    ERROR_HANDLER pfnErrorHandler
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    C# vs VB.NET

    this = me
    arrays [] = ()

    Can you post the two classes because I can't figure out some part ?

  5. #5

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    maybe this will help

    First Attempt

    VB Code:
    1. Dim newSock As New SocketClient(Me, _
    2.                             pSocket, _
    3.                             pSocket.RemoteEndPoint.ToString().Substring(0, 15), _
    4.                             GetPort, _
    5.                             GetSizeOfRawBuffer, _
    6.                             GetUserArg, _
    7.                             [U]New SocketClient.MESSAGE_HANDLER(GetMessageHandler)[/u], _
    8.                             [U]New SocketClient.CLOSE_HANDLER(GetCloseHandler)[/u], _
    9.                             [U]New SocketClient.ERROR_HANDLER(GetErrorHandler)[/U])

    the error message under all 3 is " 'delegatename' Is a Delegate Type. Delegate Construction Permits only a single AddressOf Operator in the argument list. ect."

    My second Attempt At getting it to work

    VB Code:
    1. Dim newSock As New SocketClient(Me, _
    2.                             pSocket, _
    3.                             pSocket.RemoteEndPoint.ToString().Substring(0, 15), _
    4.                             GetPort, _
    5.                             GetSizeOfRawBuffer, _
    6.                             GetUserArg, _
    7.                             [U]New SocketClient.MESSAGE_HANDLER(AddressOf GetMessageHandler)[/U], _
    8.                             [U]New SocketClient.CLOSE_HANDLER(AddressOf GetCloseHandler)[/U], _
    9.                             [U]New SocketClient.ERROR_HANDLER(AddressOf GetErrorHandler[/U]))

    Message:

    "'AddressOf' operand must be the name of the method; parthenahs needed"

    I cant figure out what it wants me to do...
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Hey, you have the full C# source code? That's from that a site that has it thru some pages isn't it?

    If you have the full code in a .cs file or whatever it is could you post it here plzkthx?
    \m/\m/

  7. #7

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    yeah, if you follow the link in the sample application a newer version is posted here and downloadable: www.continuumtechnologycenter.com
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Thanks, only now i realised that those classes don't use any kind of "events", just delegates. That sucks
    Also it doesnt use propertys or things like that

    edit: I dont f***ing get how anyone is suposed to work with that in a .NET language, I won't use it
    Must be some C or ASM coder that programmed that socket class in c# by mistake
    Last edited by PT Exorcist; Mar 7th, 2004 at 03:35 PM.
    \m/\m/

  9. #9

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. 'I did the following to fix my problem
    2.                             Dim pMessage_Handler As SocketClient.MESSAGE_HANDLER
    3.                             pMessage_Handler.Combine(pMessage_Handler, GetMessageHandler)
    4.                             Dim pClose_Handler As SocketClient.CLOSE_HANDLER
    5.                             pClose_Handler.Combine(pClose_Handler, GetCloseHandler)
    6.                             Dim pError_Handler As SocketClient.ERROR_HANDLER
    7.                             pError_Handler.Combine(pError_Handler, GetErrorHandler)

    Im not sure if this works or not because i cant figure out why i cant get the server class to get the data, prolly because i made a small mistake somewhere.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Can you even make the socket class connect for an FTP for example and stay connected? I can send/receive the 1st line but after that it will just disconnect..
    \m/\m/

  11. #11

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I still cant figure this out???

    VB Code:
    1. Public Class Client
    2.  
    3.     Public Delegate Sub MESSAGE_HANDLER(ByRef pSocket As SocketClient)
    4.    
    5.     Public Function DoSomethingWithDelegate(ByRef pfnMessageHandler As MESSAGE_HANDLER)
    6. 'Do whatever with delegate
    7.     End Function
    8.  
    9. End Class

    VB Code:
    1. Public Class Server
    2.  
    3.      Public Delegate Sub MESSAGE_HANDLER(ByRef pSocket As SocketClient)
    4.    
    5.     Public Function PassDelegate()
    6.          'Create Delegate
    7.          Dim Test as New AsyncCallback(AddressOf CallbackMethod)
    8.          'Now how can i pass Test to DoSomethingWithDelegate ???
    9.     End Function
    10.  
    11. End Class
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What is it you are trying to do here? I mean how is the code you posted suppose to work? Or what is the C# that is converts from?

  13. #13

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Im almost certain that my problem is in this method

    C# Version Of Method
    Code:
    private void AcceptThread()
        {
          Socket        pSocket       = null;
          SocketClient pClientSocket = null;
    	    
          try
          {
            // Create a new TCPListner and start it up
            GetTcpListener = new TcpListener(Dns.Resolve(GetIpAddress).AddressList[0],GetPort);
            GetTcpListener.Start();
            
            for (;;)
            {
              try
              {
                // If a client connects, accept the connection.
                pSocket = GetTcpListener.AcceptSocket();
              }
              
              catch (System.Net.Sockets.SocketException e)
              {
                // Did we stop the TCPListener
                if (e.ErrorCode != 10004)
                {
                  // Call the error handler
                  GetErrorHandler(null, e);
                  GetErrorHandler(null, new Exception("Waiting for new connection 1"));
    	      
                  // Close the socket down if it exists
                  if (pSocket != null)
                    if (pSocket.Connected)
                      pSocket.Close();
                }
                else
                {
                  GetErrorHandler(null, new Exception("Shutting Down Accept Thread"));
                  break;
                }
              }
    	  
              catch (Exception e)
              {
                // Call the error handler
                GetErrorHandler(null, e);
                GetErrorHandler(null, new Exception("Waiting for new connection 2"));
    	    
                // Close the socket down if it exists
                if (pSocket != null)
                  if (pSocket.Connected)
                    pSocket.Close();
              }
    
              try
              {
                if (pSocket.Connected)
                {
    				Debug.WriteLine(pSocket.Connected);
                  // Create a SocketClient object
                  pClientSocket = AddSocket(new SocketClient(this, 
                    pSocket, 
                    pSocket.RemoteEndPoint.ToString().Substring(0,15), 
                    GetPort,
                    GetSizeOfRawBuffer, 
                    GetUserArg,
                    new SocketClient.MESSAGE_HANDLER(GetMessageHandler), 
                    new SocketClient.CLOSE_HANDLER(GetCloseHandler), 
                    new SocketClient.ERROR_HANDLER(GetErrorHandler)));
    
                  // Call the Accept Handler
                  GetAcceptHandler(pClientSocket);
                  
                  // Wait for a message
                  pClientSocket.Receive();
                }
              }
    
              catch (Exception e)
              {
                // Call the error handler
                GetErrorHandler(null, e);
                GetErrorHandler(null, new Exception("Waiting for new connection 3"));
              }
            }
          }
    	    
          catch (Exception e)
          {
            // Call the error handler
            GetErrorHandler(null, e);
            GetErrorHandler(null, new Exception("Shutting Down Accept Thread"));
    	      
            // Close the socket down if it exists
            if (pSocket != null)
              if (pSocket.Connected)
                pSocket.Close();
          }
        }

    VB.Net Ported Version Of Method

    VB Code:
    1. Private Sub AcceptThread()
    2.             Dim pSocket As Socket = Nothing
    3.             Dim pClientSocket As SocketClient = Nothing
    4.  
    5.             Try
    6.                 '// Create a new TCPListner and start it up
    7.                 GetTcpListener = New TcpListener(Dns.Resolve(GetIpAddress).AddressList(0), GetPort)
    8.                 GetTcpListener.Start()
    9.  
    10.  
    11.                 Do ' Replaces for(;;) ....
    12.  
    13.                     Try
    14.  
    15.                         '// If a client connects, accept the connection.
    16.                         pSocket = GetTcpListener.AcceptSocket()
    17.  
    18.                     Catch e As System.Net.Sockets.SocketException
    19.  
    20.                         '// Did we stop the TCPListener
    21.                         If Not (e.ErrorCode = 10004) Then
    22.          
    23.                             '// Call the error handler
    24.                             GetErrorHandler(Nothing, CType(e, SocketException))
    25.                             GetErrorHandler(Nothing, New Exception("Waiting for new connection 1"))
    26.                             '
    27.                             '// Close the socket down if it exists
    28.                             If Not (pSocket Is Nothing) And pSocket.Connected Then
    29.                                 pSocket.Close()
    30.                             End If
    31.                         Else
    32.  
    33.                             GetErrorHandler(Nothing, New Exception("Shutting Down Accept Thread"))
    34.                             'Exit For
    35.                             Exit Do
    36.                         End If
    37.  
    38.                     Catch e As Exception
    39.                         '{
    40.                         '// Call the error handler
    41.                         GetErrorHandler(Nothing, CType(e, SocketException))
    42.                         GetErrorHandler(Nothing, New Exception("Waiting for new connection 2"))
    43.  
    44.                         '// Close the socket down if it exists
    45.                         If Not pSocket Is Nothing Then
    46.                             If (pSocket.Connected) Then
    47.                                 pSocket.Close()
    48.                             End If
    49.                         End If
    50.                     End Try '
    51.  
    52.                     Try
    53.  
    54.                         If (pSocket.Connected) Then
    55.                             '{
    56.  
    57.                             Debug.WriteLine(pSocket.Connected)
    58.                             'TODO: Check Code
    59.  
    60.                             '//Create Delegates
    61.  
    62.  
    63.                             pClientSocket = AddSocket(New SocketClient(Me, _
    64.                                 pSocket, _
    65.                                 pSocket.RemoteEndPoint.ToString().Substring(0, 15), _
    66.                                 GetPort, _
    67.                                 GetSizeOfRawBuffer, _
    68.                                 GetUserArg, _
    69.                                 [B]New SocketClient.MESSAGE_HANDLER(GetMessageHandler), _
    70.                                 New SocketClient.CLOSE_HANDLER(GetCloseHandler), _
    71.                                 New SocketClient.ERROR_HANDLER(GetErrorHandler)[/B]))
    72.  
    73.  
    74.  
    75.                             '// Call the Accept Handler
    76.                             GetAcceptHandler(pClientSocket)
    77.  
    78.                             '// Wait for a message
    79.                             pClientSocket.Receive()
    80.                         End If '} // if socket.Connected
    81.                         '} // try
    82.  
    83.                     Catch e As FieldAccessException
    84.  
    85.                         '// Call the error handler
    86.                         'GetErrorHandler(Nothing, e)
    87.                         GetErrorHandler(Nothing, New Exception("Waiting for new connection 3"))
    88.                     End Try
    89.                 Loop 'Next
    90.                
    91.  
    92.             Catch e As Exception
    93.  
    94.                 '// Call the error handler
    95.                 GetErrorHandler(Nothing, CType(e, SocketException))
    96.                 GetErrorHandler(Nothing, New Exception("Shutting Down Accept Thread"))
    97.  
    98.                 '// Close the socket down if it exists
    99.                 If Not (pSocket Is Nothing) And pSocket.Connected Then
    100.                     pSocket.Close()
    101.                 End If
    102.  
    103.             End Try
    104.         End Sub

    [Full source Files Attached]
    Last edited by <ABX; Mar 7th, 2004 at 09:03 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  14. #14

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    *bump*
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    <ABX , do you really have to use this class ? Why don't you try another code as PT Exorcist is complaining too

  16. #16

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    yeah, maybe i'll start fresh and write my own classes. I just thought it would be interesting to port.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  17. #17
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by <ABX
    yeah, maybe i'll start fresh and write my own classes. I just thought it would be interesting to port.
    It's , if the code is correct and working.......

  18. #18
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    when working with Delegates , you need to create a Sub that matches the Delegate ( to handle it ) , so what you should do is something along these lines ...
    VB Code:
    1. #[color=blue]Region[/color][color=gray] " Private Methods "[/color]
    2.  
    3.         [color=green]'/// place these 3 subs just above the "  AddSocket " Sub ...[/color]
    4.         [color=blue]Private Sub[/color] getMessageHandling([color=blue]ByRef[/color] pSocket [color=blue]As[/color] SocketClient)
    5.             [color=green]'/// handle MESSAGE_HANDLER stuff here.[/color]
    6.         [color=blue]End Sub[/color]
    7.  
    8.         [color=blue]Private Sub[/color] getCloseHandling([color=blue]ByRef[/color] pSocket [color=blue]As[/color] SocketClient)
    9.             [color=green]'/// handles CLOSE_HANDLER[/color]
    10.         [color=blue]End Sub[/color]
    11.  
    12.         [color=blue]Private Sub[/color] getErrorHandling([color=blue]ByRef[/color] pSocket [color=blue]As[/color] SocketClient, [color=blue]ByRef[/color] pException [color=blue]As[/color] Exception)
    13.             [color=green]'/// handles ERROR_HANDLER[/color]
    14.         [color=blue]End Sub[/color]
    15.  
    16. [color=green]'/// * * * then your problem area * * * ... [/color]
    17.  
    18.         [color=blue]New[/color] SocketClient.MESSAGE_HANDLER([color=blue]AddressOf[/color] getMessageHandling), _
    19.         [color=blue]New[/color] SocketClient.CLOSE_HANDLER([color=blue]AddressOf[/color] getCloseHandling), _
    20.         [color=blue]New[/color] SocketClient.ERROR_HANDLER([color=blue]AddressOf[/color] getErrorHandling)))
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  19. #19

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I have given up but that wouldnt work anyway

    They are defined like this
    VB Code:
    1. Public Delegate Sub MESSAGE_HANDLER(ByRef pSocket As SocketClient)
    2.  
    3.         Public Delegate Sub CLOSE_HANDLER(ByRef pSocket As SocketClient)
    4.  
    5.         Public Delegate Sub ERROR_HANDLER(ByRef pSocket As SocketClient, ByRef pException As Exception)

    The Problem is in the code snippit AcceptThread above is that the handlers are already set
    For Example

    GetMessageHandler is a delegate that already "Pointing" to a sub outside my class. This was set in SocketServer's Contructor but I need to pass the "pointer" to the method to the ScoketClient's Constructor
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  20. #20
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I haven't had the time to read this whole thread, but here is some VB code that I use to interact with those C# classes that they guy demonstrates on his site:
    VB Code:
    1. 'CLIENT CODE:
    2. 'module variable
    3. Private sClient As New SocketSystem.CSocketClient(10240, Nothing, AddressOf MessageHandlerClient, _
    4.                         AddressOf CloseHandler, AddressOf ErrorHandler)
    5.  
    6.  
    7.  
    8.     Public Sub MessageHandlerClient(ByVal pSocket As CSocketClient, ByVal iNumberOfBytes As Int32)
    9. 'Code here.
    10.     End Sub
    11.  
    12.     Public Sub CloseHandler(ByVal pSocket As CSocketClient)
    13.         'Code here
    14.     End Sub
    15.     Public Sub ErrorHandler(ByVal pSocket As CSocketClient, ByVal ex As Exception)
    16.         'code here.
    17.     End Sub
    18.  
    19.  
    20.  
    21.  
    22.  
    23. 'SERVER CODE:
    24. 'module variable
    25.     Private sServer As New SocketSystem.CSocketServer
    26.  
    27. 'This goes in a start method.
    28.             sServer.Start(System.Environment.MachineName, shtPortNumber, intMaxConnections, intRawBufferSize, Nothing, _
    29.                             AddressOf MessageHandlerServer, AddressOf AcceptHandler, _
    30.                             AddressOf CloseHandler, AddressOf ErrorHandler)
    31.  
    32.  
    33.     Public Sub MessageHandlerServer(ByVal pSocket As CSocketClient, ByVal iNumberOfBytes As Int32)
    34. 'Code here.
    35.     End Sub
    36.  
    37.     Public Sub AcceptHandler(ByVal pSocket As CSocketClient)
    38.         'Code here.
    39.     End Sub
    40.  
    41.     Public Sub CloseHandler(ByVal pSocket As CSocketClient)
    42.         'Code here.
    43.     End Sub
    44.  
    45.     Public Sub ErrorHandler(ByVal pSocket As CSocketClient, ByVal ex As Exception)
    46.         'Code here.
    47.     End Sub

  21. #21
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    <ABX goto : The Code Project

  22. #22
    Lively Member
    Join Date
    Sep 2003
    Location
    USA
    Posts
    102
    Not sure if you figured it out yet but according to the way the code is that you showed it does not appear that the delegates are assigned to any procedures and the must be. All a delegate is, is a parameterized variable for storing the address of a procedure that is assigned to it if it has the same parameters.

    But then again I could be stupid and did not notice that they are assigned to something lol.
    ===============
    Tek
    ===============

  23. #23
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    The code I posted above.....WORKS.

    I use it in a app. Take the time to look at it, and this whole conversation can end.

  24. #24

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    hellswrath you missunderstood my problem perhaps this will help clear it up.

    I have tried to port these two classes SocketClient and SocketServer to vb.net from C# i wanted to keep the structure the same.

    In a Method Called AcceptThread In the SocketServer Class It Creates an Instance Of a SocketClient Class To Handle when it Accepts a Conection.

    Now My Problem is that When the Class SocketServer Is Contructed the Handlers for the events are Given then

    VB Code:
    1. 'SocketServer Start Method
    2.  
    3. Public Sub Start(ByVal strIpAddress As String, _
    4.             ByVal iPort As Int16, _
    5.             ByVal iSizeOfRawBuffer As Int32, _
    6.             ByRef pUserArg As Object, _
    7.             ByRef pfnMessageHandler As MESSAGE_HANDLER, _
    8.             ByRef pfnAcceptHandler As ACCEPT_HANDLER, _
    9.             ByRef pfnCloseHandler As CLOSE_HANDLER, _
    10.             ByRef pfnErrorHandler As ERROR_HANDLER)
    11.  
    12.             If (GetAcceptThread Is Nothing) Then
    13.  
    14.                 '// Set connection values
    15.                 GetIpAddress = strIpAddress
    16.                 GetPort = iPort
    17.  
    18.                 '// Init the array of SocketClient references
    19.                 GetSocketClientList = New ArrayList
    20.  
    21.                 '// Save the Handler Functions
    22.                 GetMessageHandler = pfnMessageHandler
    23.                 GetAcceptHandler = pfnAcceptHandler
    24.                 GetCloseHandler = pfnCloseHandler
    25.                 GetErrorHandler = pfnErrorHandler
    26.  
    27.                 '// Save the buffer size and user arguments
    28.                 GetSizeOfRawBuffer = iSizeOfRawBuffer
    29.                 GetUserArg = pUserArg
    30.  
    31.                 '// Start the listening thread if one is currently not running
    32.                 'ThreadStart tsThread = new ThreadStart(AcceptThread)
    33.                 Dim tsThread As ThreadStart = New ThreadStart(AddressOf AcceptThread)
    34.                 GetAcceptThread = New Thread(tsThread)
    35.                 GetAcceptThread.Name = "Accept"
    36.                 GetAcceptThread.Start()
    37.             End If
    38.  
    39.         End Sub

    VB Code:
    1. 'SocketClient Constructor
    2. Public Sub New( _
    3.             ByRef pSocketServer As SocketServer, _
    4.             ByRef pClientSocket As Socket, _
    5.             ByVal strIpAddress As String, _
    6.             ByVal iPort As Int16, _
    7.             ByVal iSizeOfRawBuffer As Int32, _
    8.             ByRef pUserArg As Object, _
    9.             ByRef pfnMessageHandler As MESSAGE_HANDLER, _
    10.             ByRef pfnCloseHandler As CLOSE_HANDLER, _
    11.             ByRef pfnErrorHandler As ERROR_HANDLER)
    12.  
    13.             '// Set reference to SocketServer
    14.             GetSocketServer = pSocketServer
    15.  
    16.             '// Init the socket references
    17.             GetClientSocket = pClientSocket
    18.  
    19.             '// Set the Ipaddress and Port
    20.             GetIPAddress = strIpAddress
    21.             GetPort = iPort
    22.  
    23.             '// Set the handler functions
    24.             GetMessageHandler = pfnMessageHandler
    25.             GetCloseHandler = pfnCloseHandler
    26.             GetErrorHandler = pfnErrorHandler
    27.  
    28.             '// Create the raw buffer
    29.             GetSizeOfRawBuffer = iSizeOfRawBuffer
    30.             ReDim GetRawBuffer(GetSizeOfRawBuffer)
    31.  
    32.             '// Save the user argument
    33.             GetUserArg = pUserArg
    34.  
    35.             '// Allocate a String Builder class for Application developer use
    36.             GetStringBuffer = New StringBuilder
    37.  
    38.             '// Init the NetworkStream reference
    39.             GetNetworkStream = New NetworkStream(GetClientSocket)
    40.  
    41.             '// Set the async socket function handlers
    42.             GetCallbackReadFunction = New AsyncCallback(AddressOf ReceiveComplete)
    43.             GetCallbackWriteFunction = New AsyncCallback(AddressOf SendComplete)
    44.  
    45.             '// Set Available flags
    46.             IsAvailable = True
    47.  
    48.             '// Init the dispose flag
    49.             IsDisposed = False
    50.  
    51.             '// Set these socket options
    52.             GetClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveBuffer, 1048576)
    53.             GetClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendBuffer, 1048576)
    54.             GetClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.KeepAlive, 1)
    55.             GetClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.DontLinger, 1)
    56.             GetClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Tcp, System.Net.Sockets.SocketOptionName.NoDelay, 1)
    57.         End Sub

    <ABX goto : The Code Project
    To look for....?
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  25. #25
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    to look for best tested classes for server/client sockets . It's here anyways if you would like : http://www.codeproject.com/info/sear...=3%2F12%2F2004

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