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!

Winsock Help

Status
Not open for further replies.

Ablecken

Programmer
Jun 5, 2001
130
0
0
US
Hey everybody,

I'm just playing around with winsock.

I have made a client that sends a string to the server and then the server sends the string to all the clients including the one that sent it. here is the code.

Code:
Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim sItemData As String
    Dim strData As String
    Dim strOutData As String
    
    ' get data from client
    Socket(Index).GetData vtdata, vbString
    sServerMsg = "Received data from " & Socket(Index).RemoteHostIP & "(" & sRequestID & ")"
    List1.AddItem (sServerMsg)
    
    ' set data for client
    strOutData = vtdata
    
    'send data to client
    Dim Counter
    For Counter = 1 To lblConnections.Caption
        sServerMsg = "Sending data to " & Socket(Counter).RemoteHostIP
        List1.AddItem (sServerMsg)
        Socket(Counter).SendData strOutData
    Next
End Sub

the problem im having is when i put a break point at the beginning of the for counter the program works... meaning that everybody connected gets the string. now when i take the break point out only the person who sent it gets it. this is the whole code for the server below...
Code:
Dim iSockets As Integer
Dim sServerMsg As String
Dim sRequestID As String



Private Sub cmdExit_Click()
    End
End Sub

Private Sub Form_Load()
    lblHostID.Caption = Socket(0).LocalHostName
    lblAddress.Caption = Socket(0).LocalIP
    Socket(0).LocalPort = 1007
    sServerMsg = "Listening to port: " & Socket(0).LocalPort
    List1.AddItem (sServerMsg)
    Socket(0).Listen
End Sub

Private Sub socket_close(Index As Integer)
    sServerMsg = "Connection closed: " & Socket(Index).RemoteHostIP
    List1.AddItem (sServerMsg)
    Socket(Index).Close
    Unload Socket(Index)
    iSockets = iSockets - 1
    lblConnections.Caption = iSockets

End Sub


Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    If Index = 0 Then
        sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).RemoteHostIP
        List1.AddItem (sServerMsg)
        sRequestID = requestID
        iSockets = iSockets + 1
        lblConnections.Caption = iSockets
        Load Socket(iSockets)
        Socket(iSockets).LocalPort = 1007
        Socket(iSockets).Accept requestID
    End If
End Sub

Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim sItemData As String
    Dim strData As String
    Dim strOutData As String
    
    ' get data from client
    Socket(Index).GetData vtdata, vbString
    sServerMsg = "Received data from " & Socket(Index).RemoteHostIP & "(" & sRequestID & ")"
    List1.AddItem (sServerMsg)
    
    ' set data for client
    strOutData = vtdata
    
    'send data to client
    Dim Counter
    For Counter = 1 To lblConnections.Caption
        sServerMsg = "Sending data to " & Socket(Counter).RemoteHostIP
        List1.AddItem (sServerMsg)
        Socket(Counter).SendData strOutData
    Next
End Sub

Any help would be appreciated...
thanx

able
 
First off, never use End to finish your program. It will not correctly unload and remove from memory all your objects, forms etc, leaving some of them unaccesible but resident. This is called a memory leak, and is not good.

Second, you have not declared vtData in your DataArrival sub. Put Option Explicit as the first line in all your code windows, and then you will be forcoed to declare everything. It is good practice.

To fix your sending problem, put a DoEvents on a line on its own immediately after you call the Winsock.SendData. This command passes control back to windows and allows it to finish whatever its doing comletely - in this case, sending your strings.
 
Private Sub Form_Unload(Cancel As Integer)

'if winsock open then close it
If tcpServer(0).State <> sckClosed Then tcpServer(0).Close
Set frmMain = Nothing
End

End Sub

works every time..

Rick Leske
 
hi ablecken,

i am also having one problem.
my database is on server side.when two clients login to thjat server simultaneously it gives the concurrency problem.
i.e.
it overwrites the first file

can u give me some sol.

akshita
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top