Winsocket with more than one client

I made a winsocket server application and an other application, works as a client. I want to make a chatroom. But if I run twice or more the client application, the lastone gets all the messages(it works well) and the other ones can only send. What do I have to do?
I want to send all the messages for all clients.
Psycho1Asked:
Who is Participating?
 
mcriderConnect With a Mentor Commented:
Here is a complete example of a winsock chat server, and it's tiny...

1) create a project

2) add the winsock control to your project

3) add a winsock control to your form an set it's INDEX property to 0

4) add a listbox to your form and set the SORTED Property to TRUE

5) add a command button to your form

6) Paste the following code into the DECLARATION section of the form and run it.  Anyone can then telnet into this server on port 1001 and chat or you can create your own client that connects to port 1001 and chats.


Private Sub Command1_Click()
    If List1.ListIndex <> -1 Then Winsock1_Close CInt(Mid$(List1.List(List1.ListIndex), 12, 4))
End Sub

Private Sub Form_Load()
    Command1.Caption = "Disconnect"
    Winsock1(0).LocalPort = 1001
    Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
    Dim iVal As Long
     
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected Then
            Winsock1(iVal).SendData Winsock1(Index).RemoteHostIP + " DISCONNECTED" + vbCrLf
            DoEvents
        End If
    Next iVal
    Winsock1(Index).Close
    For iVal = 0 To List1.ListCount - 1
        If InStr(1, List1.List(iVal), "Connection " + Format$(Index, "0000")) <> 0 Then
            List1.RemoveItem iVal
            Exit For
        End If
    Next iVal
    If Index = Winsock1.UBound Then
        Unload Winsock1(Index)
    End If
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim iVal As Long
    Dim TotalConnect As Long
    Dim Acceptor As Long
     
    Acceptor = 0
    TotalConnect = 1
    For iVal = 1 To Winsock1.UBound
        Select Case Winsock1(iVal).State
            Case sckConnected
                TotalConnect = TotalConnect + 1
            Case sckClosed
                Acceptor = iVal
        End Select
    Next iVal
    If Acceptor = 0 Then Acceptor = Winsock1.Count
    If Acceptor > Winsock1.UBound Then Load Winsock1(Acceptor)
    With Winsock1(Acceptor)
        .Accept requestID
        List1.AddItem "Connection " + Format$(Acceptor, "0000") + " - " + .RemoteHostIP
        .SendData "Welcome to Telnet Chat Server" + vbCrLf + "Server hosted on " + _
            Winsock1(0).LocalIP + vbCrLf + "Total sessions connected: " + CStr(TotalConnect) + vbCrLf + vbCrLf
        DoEvents
    End With
    'ANNOUNCE THE NEW CONNECTION TO EVERYONE
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected And iVal <> Acceptor Then
            Winsock1(iVal).SendData "From SERVER: " + Winsock1(Acceptor).RemoteHostIP + " has joined session." + vbCrLf
            DoEvents
        End If
    Next iVal
     
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strData As String
    Dim iVal As Long
     
    Winsock1(Index).GetData strData, vbString
    Winsock1(Index).Tag = Winsock1(Index).Tag + strData
    If InStr(1, Winsock1(Index).Tag, vbCr) Then
        Winsock1(Index).SendData vbCrLf
        DoEvents
        'SEND THE BUFFER TO EVERYONE
        For iVal = 1 To Winsock1.UBound
            If Winsock1(iVal).State = sckConnected And iVal <> Index Then
                Winsock1(iVal).SendData "From " + Winsock1(Index).RemoteHostIP + ": " + Winsock1(Index).Tag
                DoEvents
            End If
        Next iVal
        Winsock1(Index).Tag = "" 
    Else
        'ECHO THE TEXT BACK TO THE LOCAL TERMINAL
        Winsock1(Index).SendData strData
    End If
End Sub



Cheers!
0
 
VBGuruCommented:
I think you while transmitting the message to the clients you are transmitting the message to the currently opened client. You got to go through all the controls in the control array and send the message to each one of them
0
 
Psycho1Author Commented:
Thanks very much! I did not tried it but i think it have to works fine! Thanks again!
0
 
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
 
Psycho1Author Commented:
I've tried it. Cool. What I wrote had only one problem. A DoEvents... Funny. Anyway, if I don't get your code, I'm in trouble. Thanks once again and Your Welcome for the points!
0
All Courses

From novice to tech pro — start learning today.