Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

chat server and client

Status
Not open for further replies.

alicecooper1201

Programmer
Apr 10, 2005
1
0
0
GB
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.

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
 
I think the problem is in how you are accepting connections. I have tested the following code and had 2 clients sending and receiving messages with no problems.

Code:
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
        
        'Now accept the Request
        Winsock1(I).Accept requestID
    End If
End Sub

Hope this helps.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top