Results 1 to 18 of 18

Thread: Winsock: "Connection is forcefully rejected" error

  1. #1

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439

    Winsock: "Connection is forcefully rejected" error

    I have written two programs, a client and server program. The client connects to the server, the server sends data and then closes the connection, then goes back to listen for more connections. The client connects fine, and everything goes like it should, but when I try to connect again (without resetting the server) I get a "Connection is forcefully rejected" error. Why? The server's state is 2 (listening).

    Disiance

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Server Code
    Code:
    Private Sub Form_Load()
        Set WinSCK = New Winsock
        WinSCK.LocalPort = 2890
        WinSCK.Listen
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        WinSCK.Close
    End Sub
    Private Sub WinSCK_DataArrival(ByVal bytesTotal As Long)
        MsgBox bytesTotal
    End Sub
    Private Sub WinSCK_ConnectionRequest(ByVal requestID As Long)
        WinSCK.Close
        WinSCK.Accept requestID
        WinSCK.SendData "P 1190"
        DoEvents
        WinSCK.Close
        Do Until WinSCK.State = 0
            DoEvents
        Loop
        WinSCK.Listen
    End Sub

  4. #4

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Client Code (Right now its just a server testing program, so nothing's automated)
    Code:
    Private Sub Command1_Click()
        WinSCK.RemoteHost = "127.0.0.1"
        WinSCK.RemotePort = 2890
        WinSCK.Connect
        Do Until WinSCK.State = 7
          DoEvents
        Loop
    End Sub
    Private Sub Command2_Click()
        WinSCK.Close
        Do Until WinSCK.State = 0
            DoEvents
        Loop
        End
    End Sub
    Private Sub Command3_Click()
        WinSCK.SendData "Test"
    End Sub
    Private Sub Command4_Click()
        MsgBox WinSCK.State
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        WinSCK.Close
        End
    End Sub
    Private Sub WinSCK_DataArrival(ByVal bytesTotal As Long)
        Dim MyData As String
        WinSCK.GetData MyData
        MsgBox MyData
    End Sub
    Private Sub WinSCK_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)
        MsgBox Description
        MsgBox "Closing Winsock"
        WinSCK.Close
    End Sub

  5. #5
    Member
    Join Date
    May 2004
    Posts
    44
    Are you sure its closed? Maybe the loop is still going when it tries to connect. Sometimes after closing a connection i've noticed that it sits there on "Waiting to Close" (state: 9). I usually implement a timer checking all winsock controls for that state and calling the .Close method again if detected.

    Just a thought..

  6. #6

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    That's why I put in the Command4 button (I know, I should name the things, but its just a testing app so ). It tells me the state of the winsock, and the state is 0 (closed). I know the server state is 2 (listening) because I have a button on that one too that tells me the state.

    Disiance

  7. #7

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Oh, also I know its a problem with the server because when I close it and reopen it the client can connect again. Then I have to reset it again, and again, and again, but each time I reopen it the client can connect (without resetting the client).

    Disiance

  8. #8

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    anyone??

    Disiance

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    maybe it needs time to process the Close event before accepting data? I notice that you have two ways to do this already, but nothing after the first line of the WinSCK_ConnectionRequest

  10. #10
    Registered User HellRaider's Avatar
    Join Date
    Sep 2003
    Posts
    70
    That happend to me once but it was because of my firewall, its just a thought but maybe it may help.

  11. #11

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Well, I tried adding the other Do Loop in, didn't help. I thought about it being my firewall but I'm bypassing it (I use 127.0.0.1 for IP right now). I have even altered the code so that after a connection the server would unload and reload the connection form, didn't help.

    Disiance

  12. #12
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Move this (server code):

    DoEvents
    WinSCK.Close
    Do Until WinSCK.State = 0
    DoEvents
    Loop
    WinSCK.Listen

    to the Winsock_SendComplete event (removing the do loop and the doevents.

    You don't need this in the client code:
    Do Until WinSCK.State = 7
    DoEvents
    Loop
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  13. #13

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Originally posted by BuggyProgrammer
    Move this (server code):

    DoEvents
    WinSCK.Close
    Do Until WinSCK.State = 0
    DoEvents
    Loop
    WinSCK.Listen

    to the Winsock_SendComplete event (removing the do loop and the doevents.

    You don't need this in the client code:
    Do Until WinSCK.State = 7
    DoEvents
    Loop
    Ok, havn't tried it so I'm not sure it works, but eventually I do want to send more than one message to the client before disconnecting, should I add a counter to count the number of messages sent?

    Disiance

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    When does the error occur exactly? Does it connect at all?
    It may be that you have a Firewall which is not allowing the app to make a connection. That situation would produce a "...connection forcefully rejected" error.

  15. #15

    Thread Starter
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Originally posted by baja_yu
    When does the error occur exactly? Does it connect at all?
    It may be that you have a Firewall which is not allowing the app to make a connection. That situation would produce a "...connection forcefully rejected" error.
    The client connects just fine, receives the data, and then disconnects. I get the error when it tries to connect a 2nd time. The only way to fix it that I've found is to close the server program and start it again.

    Disiance

  16. #16
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    176

    Re: Winsock: "Connection is forcefully rejected" error

    Quote Originally Posted by Disiance View Post
    The client connects just fine, receives the data, and then disconnects. I get the error when it tries to connect a 2nd time. The only way to fix it that I've found is to close the server program and start it again.

    Disiance
    I know it's an old post, but I got the same error with same description.

    Did you manage to solve it?

    Thanks!

  17. #17
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    616

    Re: Winsock: "Connection is forcefully rejected" error

    Quote Originally Posted by beic View Post
    I know it's an old post, but I got the same error with same description.

    Did you manage to solve it?

    Thanks!
    As their last activity (check their profile) was 10 years ago, you probably won't get a response. The right thing to have done here is starting a new post describing your own issue and referencing this one so people know you've looked here.

  18. #18
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,167

    Re: Winsock: "Connection is forcefully rejected" error

    Quote Originally Posted by beic View Post
    I know it's an old post, but I got the same error with same description.

    Did you manage to solve it?

    Thanks!
    The server implementation in this thread is bonkers. You don’t accept incomming connections on the listening socket they do.

    Better start a new thread titled “How to write simple TCP server using Winsock control - samples” or similar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width