| |

VerySource

 Forgot password?
 Register
Search
View: 666|Reply: 2

Why did I disconnect (winsock) as soon as I connected?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-1 09:30:02
| Show all posts |Read mode
code show as below:

Private Sub Command1_Click ()
    If Command1.Caption = "Start service" Then
        
        'txtip.Text = Winsock1.LocalPort
        List1.AddItem Time&"Starting service"
        On Error GoTo skip 'Note: If there is a communication program on the port, it will exit
            sckListen.LocalPort = txtport.Text
            sckListen.Listen
            Command1.Caption = "Stop service"
            List1.AddItem Time&"Service started successfully and is running ..."
            Label3.Caption = "Server Status: Running"
        Exit Sub
skip:
        If Err.Number = 10048 Then
            List1.AddItem Time&"Port is already occupied"
        End If

    Else
        Command1.Caption = "Start service"
        List1.AddItem Time&"Stop service"
        sckListen.Close
        List1.AddItem Time&"Successfully stopped service"
        Label3.Caption = "Server Status: Stopped"
    End If
End Sub

Private Sub Command2_Click ()
'Select directory
    Dim strResFolder As String
    strResFolder = BrowseForFolder (hWnd, "Please select a directory.")
    If strResFolder = "" Then
       'Call MsgBox ("You deselected the directory ...", vbExclamation)
    Else
       'Call MsgBox ("Directory"&strResFolder&"Selected!", VbExclamation)
       txtml.Text = strResFolder
    End If
End Sub

Private Sub Command3_Click ()
    i = WriteIni ("server", "mulu", txtml.Text, App.Path&"\server.ini")
    j = WriteIni ("server", "ip", txtip.Text, App.Path&"\server.ini")
    k = WriteIni ("server", "port", txtport.Text, App.Path&"\server.ini")
    If i <> 0 And j <> 0 And k <> 0 Then
        msg = MsgBox ("Saved successfully!", vbOKOnly, "Prompt")
        Unload Me
     Else
        msg = MsgBox ("Save failed! Please check"&App.Path&"Is\server.ini write permission?", vbInformation, "Prompt")
     End If
     Form1.Show
End Sub

Private Sub Form_Load ()
    txtip.Text = sckListen.LocalIP
    txtport.Text = getIni (App.Path&"\server.ini", "server", "port")
    txtml.Text = getIni (App.Path&"\server.ini", "server", "mulu")
End Sub

Private Sub sckListen_ConnectionRequest (ByVal requestID As Long)
    List1.AddItem Time&"Request number:"&requestID&"Requesting connection"
    
    Dim i As Long
    Dim sckServer () As Winsock
    If CurNumber <MaxNumber Then 'MaxNumber is the maximum number of connections, CurNumber is the current number of connections
        For i = 1 To CurNumber
          If sckServer (i) .State = 0 Then 'Determine if there is an idle Winsock control
             Exit For
          End If
        Next i
        If i = CurNumber Then
            CurNumber = CurNumber + 1
            i = CurNumber
        End If
        Load sckServer (i) 'Load Winsock control dynamically
        sckServer (i) .Protocol = sckTCPProtocol
        sckServer (i) .Accept requestID
        Exit Sub
    End If
    'sckBusy.Close
    'sckBusy.Accept requestID
    'If sckListen.State <> sckClosed Then
       'sckListen.Close
    'End If
    'sckListen.Accept requestID
    
    
End Sub


Private Sub sckServer_DataArrival (ByVal bytesTotal As Long)
 i = MsgBox (sckServer.GetData, vbOKCancel, "")
End Sub
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-9 23:00:01
| Show all posts
Can be received with only one winsock
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-26 13:30:01
| Show all posts
Still solved it by myself
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list