Results 1 to 6 of 6

Thread: Winsock problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    26

    Winsock problem

    Hi.

    I'm developing a school project which consists in a Instant Messenger using the Microsoft Winsock control.

    The IM project consists in a Server and a Client, but in this case only clients are allowed to talk, the server is there to put 2 clients in communication with each other.

    The problem I'm having occurs when i try to send a message from a client to another.
    The clients are registering their requestID in a database, and when a client try to communicate, the server searches in a database for the ID of the other client, and then places the communication.

    id is the variable who carries the id in the database

    sock(id).sendData ("example")

    but this won't work... it gives me an error (i know that vb6 is getting the ID right off the database because it printed it right after performing the .seek function)

    But if I instead of id(variable) place directly the id(integer), it works!!!

    ex.

    sock(2).sendData ("example")

    this way things will work perfectly...!

    I hope and can get some help here :S, the only thing left to this to work is this problem... i need to get it to work with the id it gets from the database...

    Thanks in advance!

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock problem

    I've done this type of app before.

    Its easier to maintain an array of connected clients in the server, instead of going into the db everytime a message is sent. In my opinion, the sock(this number) should not be related to anything in the database. This index is always going to be different for each client, each time they connect, because its based on who connects first. I dont think there is any need to base it on the ID of the clients info in the database.

    Its easier to keep an array of users connected. So it would go like this:

    Client1 sends "hello" to Client2
    Server receives "hello,Client2" from Client1, it parses the message and can see that its destined for Client2
    Server searchs the array of connectes clients looking for "Client2" in the list
    Server finds "Client2" at array #X, it then sends the message to Winsock(X).

    You see? If you keep the socket index matching the index of the client in the array, you can make this very easy for yourself
    Chris

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    26

    Re: Winsock problem

    Yeah, your right.... I'm going to try that and see if it solves my problem...

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    26

    Re: Winsock problem

    Geez.... i can't get this right... :S

  5. #5

    Re: Winsock problem

    EDIT: First things first, make sure you set up the server so that the clients is set up in an array, otherwise, things won't work properly :P

    I've got this working one of my server/client programs. This is how it works; once you have gotten the clients to connect (which you probably have done) then when data is received, it sends that data to everybody in that socket array but the person who sent it. Try this for the server side:
    VB Code:
    1. Private Sub sck_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    2.  
    3. Dim LowerBound As Integer
    4. Dim UpperBound As Integer
    5. Dim Loops As Integer
    6. Dim strData As String
    7.  
    8. LowerBound = 1 'The lowest index number of your sockets
    9. UpperBound = 2 'The highest index number of your sockets
    10.  
    11. sck(Index).GetData strData 'get the data that is recieved
    12.  
    13. For Loops = LowerBound To UpperBound 'Loop through each socket
    14.     If sck(Loops).State = sckConnected And Loops <> Index Then 'Checks if the socket is connected and is not the sender
    15.         DoEvents 'Makes sure the socket has finished with everything else first
    16.         sck(Loops).SendData strData 'Send the data to the client
    17.     End If
    18. Next Loops
    19.  
    20. End Sub
    This was not yet tested but it should work
    Last edited by evan29; Jan 22nd, 2007 at 06:52 AM.

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Winsock problem

    I do what 182guy said.

    I usually create a UDT structure to store all clients' info. Here's an example. Might make it easier to read/understand if you paste it into a VB project.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Structure to store information about a chat client.
    4. Private Type CHAT_CLIENT
    5.     strUsername As String
    6.     strIP As String
    7.     'Other info...
    8. End Type
    9.  
    10. 'Variable to store all chat client's info.
    11. Private udtClient() As CHAT_CLIENT
    12.  
    13. 'Maximum number of clients that can connect.
    14. 'Change to whatever you want.
    15. Private Const MAX_CLIENTS As Integer = 32767
    16.  
    17. 'Simple function to return the UBound of udtClient array without an error.
    18. Private Function UBClient() As Long
    19.     On Error GoTo ErrorHandler
    20.    
    21.     UBClient = UBound(udtClient())
    22.    
    23.     Exit Function
    24.    
    25. ErrorHandler:
    26.     Exit Function
    27.    
    28. End Function
    29.  
    30. 'Finds the next available socket to use to accept the connection on.
    31. Private Function NextSocket() As Integer
    32.     Dim intLoop As Integer, intFound As Integer
    33.    
    34.     'First check if we even have any other winsock controls loaded.
    35.     'If not, then just load #1.
    36.     If sckServer.ubound = 0 Then
    37.         'Load next socket.
    38.         Load sckServer(1)
    39.         sckServer(1).Close
    40.         NextSocket = 1
    41.     Else
    42.         'There are other winsock controls loaded.
    43.         'Loop through all of them.
    44.         'If one of them isn't being used (State = sckClosed) then we can use that one.
    45.        
    46.         'If we can't find one already loaded then we need to load a new one
    47.         'unless we've already reached MAX_CLIENTS.
    48.         For intLoop = 1 To sckServer.ubound
    49.            
    50.             If sckServer(intLoop).State = sckClosed Then
    51.                 intFound = intLoop 'Found one.
    52.                 Exit For
    53.             End If
    54.        
    55.         Next intLoop
    56.        
    57.         If intFound > 0 Then 'Check if we found one already loaded.
    58.             NextSocket = intFound
    59.         Else
    60.             'Didn't find one. Check if we can load a new one.
    61.             If sckServer.ubound < MAX_CLIENTS Then
    62.                 intFound = sckServer.ubound + 1
    63.                 Load sckServer(intFound)
    64.                 sckServer(intFound).Close
    65.                 NextSocket = intFound
    66.             Else
    67.                 'We reached MAX_CLIENTS.
    68.                 Debug.Print "CONNECTION REFUSED: MAX_CLIENTS REACHED"
    69.             End If
    70.        
    71.         End If
    72.    
    73.     End If
    74.    
    75. End Function
    76.  
    77. Private Sub sckServer_Close(Index As Integer)
    78.     'A client has disconnected.
    79.     'To acccess this client's info we can just use the Index property of the winsock.
    80.    
    81.     Debug.Print udtClient(Index).strIP & " disconnected."
    82. End Sub
    83.  
    84. Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    85.     'Find the next available socket to use.
    86.     Dim intNext As Integer
    87.    
    88.     intNext = NextSocket
    89.    
    90.     If intNext > 0 Then 'Found one.
    91.         sckServer(intNext).Accept requestID 'Accept connection.
    92.        
    93.         'Now associate a client in udtClient() with this winsock control.
    94.        
    95.         'We may need to ReDim the array in case this one hasn't been loaded yet.
    96.         If intNext > UBClient() Then
    97.             ReDim Preserve udtClient(0 To intNext) As CHAT_CLIENT
    98.         End If
    99.        
    100.         With udtClient(intNext)
    101.             .strUsername = ""
    102.             .strIP = sckServer(intNext).RemoteHostIP
    103.         End With
    104.        
    105.     End If
    106.    
    107. End Sub

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