|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Hi all...
i'm currently doing a chat server program with winsock.. it can accept mutiple connections and broadcast messages... the server is using 2 winsocks. 1 is a active listener... the other is a control arrays of winsock that connects the clients and server..... dynamically created when new connections come.. the problem is : (muti-accepts are ok)... however, when a chat client disconnects and a new client attempts to connect... i get this run-time 360 error object already loaded.. i know why this happens.. its due to my array of winsock.. if a connection is accepted.. a global variable increases... and decreases when connections come n go... example: 5 clients connect... no. 3 decides.. to go... another new client(x) attempts to connect... the error comes on!! HELP... can someone PLEASE help me with this bug??? here goes... this is my array of winsock that accepts a client.... ------------------------------------------- ------------------------------------------ Private Sub tcpServerArray_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim strData As String Dim strHostname As String Dim strIpAddress As String Dim strCommand As String Dim i As Long If Len(tcpServerArray(Index).RemoteHostIP) Then strHostname = GetHostNameFromIP(tcpServerArray(Index).RemoteHostIP) strIpAddress = tcpServerArray(Index).RemoteHostIP Else strIpAddress = GetIPFromHostName(tcpServerArray(Index).RemoteHost) strHostname = tcpServerArray(Index).RemoteHost End If tcpServerArray(Index).GetData strData If Left$(strData, 1) = Chr(47) Then strCommand = ServerCommandSystem(strData) If Len(strCommand) Then strData = strCommand tcpServerArray(Index).SendData strData End If Else For i = 0 To tcpServerArray.UBound tcpServerArray(i).SendData strData Next End If End Sub Private Sub tcpServerArray_SendComplete(Index As Integer) sCommandComplete = True End Sub Private Sub tcpServerArray_Close(Index As Integer) If tcpServerArray(Index).State <> sckClosed Then Call ServerArraySocketClose(Index) End If End Sub Private Sub tcpServerArray_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) If tcpServerArray(Index).State <> sckClosed Then Call ServerArraySocketClose(Index) End If MsgBox ("Error Occured : " & vbCrLf & Description), vbCritical, ("WinSock Error " & Number) End Sub here is my winsock that "listens for new connections.. -------------------------------------- ------------------------------------ Private Sub tcpServer_ConnectionRequest(ByVal RequestID As Long) Dim strHostname As String Dim strIpAddress As String Dim CheckClient As Boolean 'Check if the control's State is listening. If tcpServer.State = sckListening Then If Len(tcpServer.RemoteHostIP) Then strHostname = GetHostNameFromIP(tcpServer.RemoteHostIP) strIpAddress = tcpServer.RemoteHostIP Else strIpAddress = GetIPFromHostName(tcpServer.RemoteHost) strHostname = tcpServer.RemoteHost End If 'txtConnectStatus.Text = "Incoming Connection from: " & strHostname _ & " [" & strIpAddress & "]" 'txtDisplay.Text = txtDisplay.Text & Time() & " >> " _ & "Incoming Connection from: " & strHostname _ & " [" & strIpAddress & "]" _ & vbCrLf Connections = Connections + 1 If Connections >= 1 Then Load tcpServerArray(Connections) 'The port you chose above tcpServerArray(Connections).LocalPort = tcpServer.LocalPort 'Accept the request with the requestID parameter. tcpServerArray(Connections).Accept RequestID cnt = cnt + 1 'txtConnectStatus.Text = "Connection accepted from: " & strHostname _ & " [" & strIpAddress & "]" txtDisplay.Text = txtDisplay.Text & Time() & " >> " _ & "Connection accepted from: " & strHostname _ & " [" & strIpAddress & "]" _ & vbCrLf 'lstClients.AddItem strHostname & " [" & strIpAddress & "]" CheckClient = ArrayExists(ConnectedClients) If CheckClient = False Then cnt = 1 Else cnt = cnt + 1 End If ReDim Preserve ConnectedClients(0 To cnt) As Clients ConnectedClients(cnt).ConnID = cnt ConnectedClients(cnt).hostname = strHostname ConnectedClients(cnt).IPAddress = strIpAddress 'Add Connected Client Information lstClients.AddItem ConnectedClients(cnt).hostname & _ vbTab & _ " [" & ConnectedClients(cnt).IPAddress & "]", Connections lstClients.ListIndex = 0 End If End Sub Private Sub tcpServer_Close() If tcpServer.State <> sckClosing Then Call ServerSocketClose End If End Sub Private Sub tcpServer_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) If tcpServer.State <> sckClosed Then Call ServerSocketClose End If MsgBox ("Error Occured : " & vbCrLf & Description), vbCritical, ("WinSock Error " & Number) End Sub |
|
#2
|
|||
|
|||
|
Could you also include the "ServerSocketClose" sub.
I think I know the problem, but I need to be sure. |
|
#3
|
|||
|
|||
|
yes!!
finally some help!! ![]() i think the prob is becoz of my winsock array no. and the listbox's index is wrong.... lets say.. there are 4 connections... so my listbox will have 4 items now... if no.2 leaves... it is cleared.. then the rest (3 winsock) will be "re-indexed"... to 0~3.. thats why is i close lets say connection 2... the index is now 1.. where it shouldn't be .. but becoz of the listbox' wrong inddex.. i get those errors.... am i right about this.???? but i cant seem to be able to solve this sickening bug.... thanks alot for helping.. once again.... let me know abt your results too, asap.. ok?? |
|
#4
|
|||
|
|||
|
i have 2 functions that are "controling" the closing of sockets..... i need them to be called frequently... that why i used 2 functions to close the sockets....
Private Function ServerSocketClose() Dim strHostname As String Dim strIpAddress As String If tcpServer.State <> sckClosed Then If Len(tcpServer.RemoteHostIP) Then strHostname = GetHostNameFromIP(tcpServer.RemoteHostIP) strIpAddress = tcpServer.RemoteHostIP Else strIpAddress = GetIPFromHostName(tcpServer.RemoteHost) strHostname = tcpServer.RemoteHost End If txtDisplay.Text = txtDisplay.Text & Time() & " >> " _ & "Connection from: " & strHostname _ & " [" & strIpAddress & "]" _ & " on port " & tcpServer.LocalPort _ & " Closed" _ & vbCrLf & vbCrLf tcpServer.Close End If End Function Private Function ServerArraySocketClose(ByVal Index As Integer, _ ByVal sCommand As Integer) Dim strHostname As String Dim strIpAddress As String Dim i As Long If tcpServerArray(Index).State <> sckClosed Then If Len(tcpServerArray(Index).RemoteHostIP) Then strHostname = GetHostNameFromIP(tcpServerArray(Index).RemoteHostIP) strIpAddress = tcpServerArray(Index).RemoteHostIP Else strIpAddress = GetIPFromHostName(tcpServerArray(Index).RemoteHost) strHostname = tcpServerArray(Index).RemoteHost End If Select Case sCommand Case 0: txtDisplay.Text = txtDisplay.Text & Time() & " >> " _ & "Connection from " _ & strHostname _ & " [" & strIpAddress & "]" _ & " dropped from server " _ & tcpServer.LocalHostName _ & " [" & tcpServer.LocalIP & "]" _ & " on port " _ & tcpServer.LocalPort _ & vbCrLf & vbCrLf Case Else: txtDisplay.Text = txtDisplay.Text & Time() & " >> " _ & "Connection from: " _ & strHostname _ & " [" & strIpAddress & "]" _ & " on port " _ & tcpServer.LocalPort _ & " Closed" _ & vbCrLf & vbCrLf End Select tcpServerArray(Index).Close Unload tcpServerArray(Index) 'Connections = Connections - 1 ActiveConn = ActiveConn - 1 End If End Function ***NOTE... the serversocketclose() closes the main winsock that does the listening (only this job) the serverarrayclose() closes the "active" (real) connections between the chat clients.... |
|
#5
|
|||
|
|||
|
Well, theres still code missing. Either that or you "ActiveConn = ActiveConn - 1" for absolutely no reason. However, I will still give my opinion of what is going on.
I assume that the only object you are loading is the winsock control. The problem is that you are trying to load something that has already previously been loaded, as the error says. You have said that everytime a user connects the connections counter goes up, and everytime someone leaves it goes down again. You also say that the array is reshuffled everytime someone leaves. However, as far as I know (correct me if I'm wrong), objects can't just be reshuffled. Arrays can certainly, but I'm definately not sure about objects. Take this scenario: - Client 1 logs in, taking Index 1 and moving the connection variable to 1. - Client 2 logs in, taking Index 2 and moving the connection variable to 2. - Client 3 logs in, taking Index 3 and moving the connection variable to 3. The Winsock object has been loaded 3 times, with index 1 through 3. - Client 3 logs out, closing Index 3 and moving the connection variable to 2. - Client 4 logs in, and attempts to take Index 2, which has already been taken by Client 2. This procures the object has already been loaded error. To get around this you need to have a loop from 0 to infinity using a counter, to check if the winsock(counter) is currently closed or not loaded. If the winsock is already loaded then check if it is closed, it is closed then let the client use that one, and dont load a new one. If the winsock is already loaded but is open then move on to the next index. If the winsock isn't loaded then load it and let the client use it. I hope this helps you. |
|
#6
|
|||
|
|||
|
DONE
thanks alot
|
|
#7
|
|||
|
|||
|
Winsock is a trouble thing as sometime,somewhere!U need some 3th component to do it..That is the best way to reduce wrong..
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Winsock Chat Server Error |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|