winsock array problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: winsock array problem

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    winsock array problem

    Hi all, i am trying to devlop a chat server and client in vb i have managed to get the server to allow mutiple conections by creating an array of the winsock. If only one client is connected both client and server can chat away with out any probs( as yet discovered) however if i then conect with a second client the second client can send and recive messages from the server but the flient that was connected first cant it can only send. Once the second client disconects and either the server or the first client send a message the first client recives all the "missing data" in one long string. then they can comunicate as befor.

    Not sure if i should post code here or not so if i have done wrong please forgive me. The source for the server is as follows.
    Code:
    Dim LastSocket As Integer
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    
    Private Sub cmdSend_Click()
    Dim I As Integer
    
    On Error Resume Next
    'This data will be sent to the Client
    For I = 1 To LastSocket
    Winsock1(I).SendData "Server:- " & txtSend.Text
    Next I
    lstMessages.AddItem "Server:- " & txtSend.Text
    txtSend.Text = ""
    txtSend.SetFocus
    End Sub
    
    Private Sub Form_Load()
    On Error Resume Next
    'If one Copy of Our Application is already running then don't load a new one
    If Not App.PrevInstance = True Then
    Winsock1(0).LocalPort = 1412 'This can be any Valid Port Number
    'Wait for Clients to Connect with Your Server
    Winsock1(0).Listen
    End If
    End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'for making a form Movable
    SendMessage Me.hwnd, &HA1, 2, 0&
    End Sub
    
    Private Sub Label1_Click()
    Dim I As Integer
    On Error Resume Next
    For I = 1 To LastSocket
    'So that it will not raise an error after sending the data to the server which is already disconnected
    Winsock1(I).SendData "Server is Disconnected!"
    Winsock1(I).Close
    Next I
    Winsock1(0).SendData "server is disconected"
    Winsock1(0).Close
    'Here DoEvents gives time to perform the winsock operation before unloading it from memory
    DoEvents
    'Now Unload it
    Unload Me
    End Sub
    
    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim I As Integer
    On Error Resume Next
    For I = 1 To LastSocket
    If Winsock1(I).State = sckClosed Then Exit For
    Next I
    If I > LastSocket Then
    LastSocket = LastSocket + 1: I = LastSocket
    Load Winsock1(I)
    End If
    'First Check if the Winsock Control is Connected or not If connected then Close it
    'If Winsock1.State <> sckClosed Then Winsock1.Close
    'Now accept the Request
    Winsock1(I).LocalPort = 1412
    Winsock1(I).Accept requestID
    End Sub
    
    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    On Error Resume Next
    Dim str As String
    'Now we will store data that has came into this string
    Winsock1(Index).GetData str, vbString
    For I = 1 To LastSocket
    Winsock1(I).SendData str
    Next I
    
    'And Display that data in the listbox
    lstMessages.AddItem str
    End Sub
    any help greatly appreciated
    Thanks in advance
    Last edited by Phil Weber; 04-09-2005 at 03:59 PM. Reason: Added [code] tags

  2. #2
    Join Date
    Apr 2004
    Location
    Lafayette, LA
    Posts
    200
    Quote Originally Posted by alicecooper1201
    'Now accept the Request
    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim I As Integer
    On Error Resume Next
    For I = 1 To LastSocket
    If Winsock1(I).State = sckClosed Then Exit For
    Next I
    If I > LastSocket Then
    LastSocket = LastSocket + 1: I = LastSocket
    Load Winsock1(I)
    End If
    'First Check if the Winsock Control is Connected or not If connected then Close it
    'If Winsock1.State <> sckClosed Then Winsock1.Close
    'Now accept the Request
    Winsock1(I).LocalPort = 1412
    Winsock1(I).Accept requestID
    End Sub
    Change to:
    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim I As Integer
    On Error Resume Next
    If Index = 0 then
    For I = 1 To LastSocket
    If Winsock1(I).State = sckClosed Then Exit For
    Next I
    If I > LastSocket Then
    LastSocket = LastSocket + 1: I = LastSocket
    Load Winsock1(I)
    End If
    'First Check if the Winsock Control is Connected or not If connected then Close it
    'If Winsock1.State <> sckClosed Then Winsock1.Close
    'Now accept the Request
    Winsock1(I).LocalPort = 0
    Winsock1(I).Accept requestID
    End If
    End Sub


    You need to be sure that socket zero(0) is receiving the request.
    You also need to set the LocalPort property to 0. Setting to zero will cause Winsock to select an available port.
    If you do not set the port to zero, you will get an error message. You are not getting the error because of the 'On Error Resume Next'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center