Results 1 to 11 of 11

Thread: Problem In Winsock Connection

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Resolved Problem In Winsock Connection

    Hello friends I have wrote some code for connecting through winsock to send data, but there is problem in connecting, whenever i connect it is connected for 1 second after that its state is clossing. I dont how this problem occures.
    So please try to help me




    Private Sub cmdConn_Click()
    If Not txtPort = "" Or Not txtIp.Text = "" Then

    wskSend.Close
    DoEvents
    wskSend.RemoteHost = txtIp.Text
    wskSend.RemotePort = Val(txtPort.Text)
    'MsgBox wskSend.State
    wskSend.Connect
    'MsgBox wskSend.State
    End If
    End Sub

    Private Sub cmdSend_Click()
    On Error GoTo sol
    Label4.Caption = wskSend.State
    If Not wskSend.State = 7 Then
    wskSend.Connect
    End If
    wskSend.SendData txtMsg

    Exit Sub
    sol:
    MsgBox Err.Description
    End Sub

    'Close connection button
    Private Sub Command1_Click()
    wskSend.Close
    End Sub

    Private Sub Form_Load()
    wskSend.Protocol = sckTCPProtocol
    End Sub

    Private Sub Timer1_Timer()
    If wskSend.State = 6 Then
    Label4.Caption = "Connecting"
    ElseIf wskSend.State = 7 Then
    Label4.Caption = "Connected"
    MsgBox "connected"
    ElseIf wskSend.State = 0 Then
    Label4.Caption = "Closed"
    ElseIf wskSend.State = 8 Then
    Label4.Caption = "Clossing"
    'MsgBox "closing"
    'wskSend.Close
    ElseIf wskSend.State = 9 Then
    Label4.Caption = "Error"
    ElseIf wskSend.State = 2 Then
    Label4.Caption = "Listening"
    ElseIf wskSend.State = 1 Then
    Label4.Caption = "Open"
    ElseIf wskSend.State = 7 Then
    Label4.Caption = "Connected"
    End If
    End Sub

    Private Sub wskSend_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 "Error in connection" & Number & Description
    End Sub

    Private Sub wskSend_SendComplete()
    Label4.Caption = "data sent"
    End Sub

    Private Sub wskSend_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
    Label4.Caption = "sending"
    End Sub
    Last edited by ishrar; Mar 20th, 2007 at 01:02 AM.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem In Winsock Connection

    I don't see a problem in the client code, show us your server.

    BTW doesn't the .Connect method require the RemoteHost and RemotePort?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Problem In Winsock Connection

    Yes,
    Let me know have you checked it by runing it or not.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem In Winsock Connection

    Without your servercode?
    By the look at your client code I found no problems, and therefore I'm asking to see your server code!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Problem In Winsock Connection

    Private Sub cmdListen_Click()
    wskReceive.Close
    Label2 = wskReceive.State
    wskReceive.LocalPort = Val(txtPortNo)
    wskReceive.Listen
    If wskReceive.State = 1 Then
    cmdListen.Caption = "Listening"
    txtMsg.Text = "Listening" & vbNewLine
    Else
    'cmdListen.Caption = "Connecting"
    End If
    End Sub

    'close connection......
    Private Sub Command1_Click()
    wskReceive.Close
    End Sub

    Private Sub Timer1_Timer()
    If wskReceive.State = 6 Then
    Label2.Caption = "Connecting"
    ElseIf wskReceive.State = 7 Then
    Label2.Caption = "Connected"
    ElseIf wskReceive.State = 0 Then
    Label2.Caption = "Closed"
    ElseIf wskReceive.State = 8 Then
    Label2.Caption = "Clossing"
    wskReceive.Connect
    ElseIf wskReceive.State = 9 Then
    Label2.Caption = "Error"
    ElseIf wskReceive.State = 2 Then
    Label2.Caption = "Listening"
    ElseIf wskReceive.State = 1 Then
    Label2.Caption = "Open"
    ElseIf wskReceive.State = 7 Then
    Label2.Caption = "Connected"
    End If
    End Sub

    Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
    'MsgBox "Request received from client"
    If Not wskReceive.State = 2 Then wskReceive.Accept requestID Else Label2.Caption = "Listening"


    End Sub

    Private Sub wskReceive_DataArrival(ByVal bytesTotal As Long)
    wskReceive.GetData txtMsg.Text, vbString
    End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Problem In Winsock Connection

    Now say what is wrong>>>>>>>>>

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem In Winsock Connection

    In your cmdConn_Click (Client) I changed:
    Code:
    wskSend.Connect
    to:
    Code:
    wskSend.Connect wskSend.Remotehost, wsksend.RemotePort
    (this one might not be neccessary)
    In your wskReceive_ConnectionRequest (Server) I changed:
    Code:
    Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
    'MsgBox "Request received from client"
    If wskReceive.State <> sckClosed Then wskReceive.Close
    wskReceive.Accept requestID 
    Label2.Caption = "Listening"
    End Sub
    In your wskReceive_DataArrival (Server) I changed:
    Code:
    Private Sub wskReceive_DataArrival(ByVal bytesTotal As Long)
    Dim Stream as String
    wskReceive.GetData Stream
    txtMsg.Text= Stream
    End Sub
    You should be able to send from clinet to server using those changes.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Resolved Re: Problem In Winsock Connection

    fantastic man.....
    Very good exlnt.
    It is working properly, but i m eager to know what was mstk in my prog.
    while line was wrong...
    I want to know where i ws wrong?
    Plz tell
    thnk you very much

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem In Winsock Connection

    You had:
    Code:
    Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
    'MsgBox "Request received from client"
    If Not wskReceive.State = 2 Then wskReceive.Accept requestID Else Label2.Caption = "Listening"
    End Sub
    Your code did the .Accept only when the state of wskReceive was NOT listening (=2), but the connection shouldbe listening to get the ConnectionRequest!

    When this Event fires wskReceive is in the .Listening State (=2). In order to use this Winsock, I would reset the connection (using .Close). For applications with more connections, you should revert to a another Winsock to do the actual connect and keep the "listening" connection in the listening-state.
    First, .State=2 is the Listening-State.

    The reason for the change in the DataArrival Sub isn't obvious to me either, but this way it works!

    BTW: Ratings are welcome!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Problem In Winsock Connection

    Quote Originally Posted by opus
    You had:
    Code:
    Private Sub wskReceive_ConnectionRequest(ByVal requestID As Long)
    'MsgBox "Request received from client"
    If Not wskReceive.State = 2 Then wskReceive.Accept requestID Else Label2.Caption = "Listening"
    End Sub
    Your code did the .Accept only when the state of wskReceive was NOT listening (=2), but the connection shouldbe listening to get the ConnectionRequest!

    When this Event fires wskReceive is in the .Listening State (=2). In order to use this Winsock, I would reset the connection (using .Close). For applications with more connections, you should revert to a another Winsock to do the actual connect and keep the "listening" connection in the listening-state.
    First, .State=2 is the Listening-State.

    The reason for the change in the DataArrival Sub isn't obvious to me either, but this way it works!

    BTW: Ratings are welcome!
    Thank you very muchh

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Problem In Winsock Connection

    Quote Originally Posted by opus
    First, .State=2 is the Listening-State.
    Which is why it's better to use constants. It makes the code much easier to debug.
    Code:
    sckClosed             0 
    sckOpen               1 
    sckListening          2 
    sckConnectionPending  3 
    sckResolvingHost      4 
    sckHostResolved       5 
    sckConnecting         6 
    sckConnected          7 
    sckClosing            8 
    sckError              9
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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