wisock

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • basti42
    New Member
    • Jun 2007
    • 18

    wisock

    hello!
    how can i send data? (server to all clients)
    1. open 3 clients
    2. close client1 (of course use for loop 1 to 3 and send to all)
    3. i have condition in clents if recieved ("Close") then End/unload
    4. open again client1
    5. you can's use for loop again because 1 can't find because i closed it before

    how to use my index then?
    in the server winsock(0)

    tnx
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Can you post your code for reference of our experts please.

    Comment

    • basti42
      New Member
      • Jun 2007
      • 18

      #3
      'client side
      [code=vb]
      Private Sub Winsock1_Close( )
      On Error Resume Next
      Label3.Caption = ""
      Winsock1.Close
      Winsock1.Connec t txtIP.Text, 1007
      End Sub

      Private Sub Winsock1_Connec t()
      Label4.Caption = "Connected"
      Winsock1.SendDa ta txtName.Text + "|" + Winsock1.LocalI P + "|" + Winsock1.LocalH ostName
      End Sub

      Private Sub Winsock1_DataAr rival(ByVal bytesTotal As Long)
      Dim sData As String

      Winsock1.GetDat a sData, vbString
      Winsock1.SendDa ta "from " + txtName.Text + " - " + sData + "|" + "|"
      'Label1.Caption = sData
      txtPrice.Text = sData
      Label3.Caption = "Received Data"
      shpGo.Visible = True
      shpWait.Visible = False
      shpError.Visibl e = False
      If sData = txtName.Text Then
      Winsock1.Close
      End
      ElseIf sData = "Disconnect ed" Then
      Winsock1.Close
      Label4.Caption = "Disconnect ed"
      End If
      End Sub

      Private Sub Winsock1_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)
      Winsock1.Close
      Winsock1.Connec t txtIP.Text, 1007
      'Label4.Caption = "Disconnect ed1 " + err.Description
      End Sub


      Private Sub Timer1_Timer()
      On Error GoTo err
      If Winsock1.State <> 7 Then
      Winsock1.Close
      Winsock1.Connec t txtIP.Text, 1007
      Label4.Caption = "Disconnect ed2 " '+ Winsock1.State
      End If
      Exit Sub
      err:
      Winsock1.Close
      Label4.Caption = "Disconnect ed " '+ CStr(Winsock1.S tate)
      'MsgBox Winsock1.State
      End Sub
      [/code]

      'server side
      [code=vb]
      Private Sub cmdPC_MouseUp(I ndex As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
      If Button = vbRightButton Then
      cmdPC(Index).Se tFocus
      sClientName = cmdPC(Index).Ca ption
      idxPC = Index
      idxSocket = Index
      PopupMenu mnu
      End If
      End Sub

      Private Sub mnuClose_Click( )
      Dim i As Long

      'For i = 1 To iSockets
      If Socket(idxPC).S tate = sckConnected Then
      If cmdPC(idxPC).En abled = True Then
      Socket(idxPC).S endData sClientName
      cmdPC(idxPC).En abled = False
      List1.AddItem "send to: " + CStr(Socket(idx PC)) + "-" + sClientName
      Else
      MsgBox "client is not connected"
      End If
      End If
      'Next
      End Sub

      Private Sub Form_Load()
      lblHostID.Capti on = Socket(0).Local HostName
      lblAddress.Capt ion = Socket(0).Local IP
      Socket(0).Local Port = 1007
      sServerMsg = "Listening to port: " & Socket(0).Local Port
      List1.AddItem (sServerMsg)
      Socket(0).Liste n
      End Sub

      Private Sub socket_Close(In dex As Integer)
      sServerMsg = "Connection closed: " & Socket(idxPC).R emoteHostIP
      List1.AddItem (sServerMsg)
      Socket(idxPC).C lose
      Unload Socket(idxPC)
      cmdPC(idxPC).En abled = False
      iSockets = iSockets - 1
      lblConnections. Caption = iSockets
      End Sub

      Private Sub socket_Connecti onRequest(Index As Integer, ByVal requestID As Long)
      On Error GoTo err
      sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).R emoteHostIP

      If Index = 0 Then
      'List1.AddItem (sServerMsg)
      sRequestID = requestID
      If err.Number = 360 Then
      Socket(0).Close
      Socket(0).Liste n
      End If
      iSockets = iSockets + 1
      lblConnections. Caption = iSockets
      Load Socket(iSockets )
      'Load Socket(Socket.U Bound + 1)
      Socket(iSockets ).LocalPort = 1007
      Socket(iSockets ).Accept requestID
      Label1.Caption = ""
      End If

      Exit Sub
      err:
      Socket(0).Close
      Socket(0).Liste n
      iSockets = 0
      'iSockets = iSockets - 1
      Label1.Caption = CStr(err.Number ) + " " + err.Description + "-" + sClientName
      End Sub

      Private Sub socket_DataArri val(Index As Integer, ByVal bytesTotal As Long)
      Socket(Index).G etData strReceived
      strSplit() = Split(strReceiv ed, "|")
      sClientName = strSplit(0)
      strIP = strSplit(1)
      strHostName = strSplit(2)
      txtClientName.T ext = sClientName
      txtHostName.Tex t = strHostName
      txtIP.Text = strIP
      List1.AddItem (sServerMsg + "-" + sClientName)
      Select Case (sClientName)
      Case "PC1"
      Index = 1
      cmdPC(Index).En abled = True
      Case "PC2"
      Index = 2
      cmdPC(Index).En abled = True
      Case "PC3"
      Index = 3
      cmdPC(Index).En abled = True
      End Select
      End Sub
      [/code]

      hope it will help, with simple server and 1 client there is no problem
      but server and many clients kinda tricky...
      Last edited by debasisdas; Feb 27 '08, 05:47 AM. Reason: added code=vb tags

      Comment

      Working...