Results 1 to 9 of 9

Thread: [RESOLVED] WinSocket Advice

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2010
    Location
    Northampton, United Kingdom
    Posts
    21

    Resolved [RESOLVED] WinSocket Advice

    Hi all,

    I am using Vb6 and I am trying to make my application talk to a .net app via Socket (and it does this)

    I can connect to it and send data the first time BUT once the .net app is done with the data it closes the socket.

    When I try to send data to it again my Vb6 app dies with errors
    > 40006
    > 40020

    How do I make it so the Vb6 app can open a new link and send the data when ever needed?

    it works if I exit the application and re run it

    Thanks

    Andy
    Last edited by JasonPoS; Sep 13th, 2010 at 12:20 PM. Reason: spelling error

  2. #2
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: WinSocket Advice

    It seems to me you are trying to send data after the socket has closed your connection.

    You should verify the state of the socket before sending data. You can do so in this manner:

    Code:
    If Winsock.SocketState = 7 Then Winsock.SendData whatever_here
    'if connected then send
    if you are constantly being disconnected perhaps you can try the code below. you will have to add a listbox to your project.

    Code:
    Whenever you want to send something to your server you just do
    AddData What_to_send_here
    
    Sub AddData(byref theData as string)
      List1.Additem theData
      SendData
    End Sub
    
    Sub SendData()
      Do Until List1.ListCount = 0 or Winsock.State <> 7
        Winsock.SendData List1.List 0
        List1.Removeitem 0
      Loop
      'send available items if connected
      If List1.Listcount <> 0 Then
        Winsock.Close
        Winsock.Connect server, port
      End If
    End Sub
    'this sub will send data queued to be sent while the winsock is connected.
    'if the socket is not connected it will attempt to reconnect
    
    Sub Winsock_Connect
      SendData
      'connection made, start sending our data
    End Sub
    'a connection has been made so we will begin sending our data

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: WinSocket Advice

    Please could you post your code

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2010
    Location
    Northampton, United Kingdom
    Posts
    21

    Re: WinSocket Advice

    ok here is the first bit of the code

    When it loads this is call

    vb Code:
    1. FrmMainScreen.ToOcuis.Connect IPAddress, ToOcuisPort
    2.     FrmMainScreen.FromOucis.Connect IPAddress, FromOcuisPort
    When a sale is requested the is called
    vb Code:
    1. FileNumber = FreeFile
    2.     sData = ""
    3.    
    4.     Open PoleFolder & MySelectedFile For Input As #FileNumber
    5.       Line Input #FileNumber, sData
    6.     Close #FileNumber
    7.  
    8.     Kill PoleFolder & MySelectedFile    ' Deletes the request file from the Directory
    9.  
    10.     Fields() = Split(sData, ",")
    11.    
    12.     For i = 0 To UBound(Fields)
    13.         Fields(i) = ""
    14.     Next
    15.    
    16.     Fields() = Split(sData, ",")
    17.        
    18.     For i = 0 To UBound(Fields)
    19.         Trim (Fields(i))
    20.     Next
    21.    
    22.     If Fields(0) = "Sale" Then
    23.         FrmMainScreen.List1.AddItem " Intergrated Sale transaction of £" & Fields(1) & " was recevied"
    24.        
    25.         SendData = vbNullString
    26.         SendData = "T,,01,0000,,,,,,," & Format(Fields(1), "######0.00") & ",,,,,,,,,,,," & Fields(2) & "," & AccountID & ",,,," & Chr(13) & Chr(10)
    27.     End If
    28.    
    29.     If Fields(0) = "Refund" Then
    30.         FrmMainScreen.List1.AddItem " Intergrated Refund transaction of £" & Fields(1) & " was recevied"
    31.         Kill PoleFolder & MySelectedFile
    32.        
    33.         SendData = vbNullString
    34.         SendData = "T,,02,0000,,,,,,," & Format(Fields(1), "######0.00") & ",,,,,,,,,,,," & Fields(2) & "," & AccountID & ",,,," & Chr(13) & Chr(10)
    35.     End If
    36.    
    37.     With FrmMainScreen.ToOcuis
    38.     If .State = 8 Then
    39.         .Connect IPAddress, ToOcuisPort
    40.     End If
    41.    
    42.     Sleep 1500
    43.     If .State = 7 Then
    44.         .SendData SendData
    45.     End If
    46.     End With
    THIS works the first time but when ever I try to send another sale to the Socket after the first one that is when the error happens.

    Any idea guys? I can attach the project as a Zip file for you to view ALL the code if you want

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: WinSocket Advice

    Please do not create duplicate threads for the same question.

    Thank you.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2010
    Location
    Northampton, United Kingdom
    Posts
    21

    Re: WinSocket Advice

    Opps Sorry My internet connection is playing up today

    and I think my modem will be the first in orbit if it carrys on playing up

    Hopefully someone can help me with this as it is REALLY driving me crazy

  7. #7
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: WinSocket Advice

    I told you your problem in my first post.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: WinSocket Advice

    It would help if you told us on which line the error was happening. It looks as if you are only sending data if the socket is open (Line 43) I would expect an error at Line 39 as you'd be trying to Open the connection whilst it was in the process of closing.(a state of 8 = Socket Closing) Line 38 perhaps should be
    Code:
    If .State = sckClosed Then
    Your main problem is that you are trying to synchronise an asynchronous process and are making assumptions that may or may not be true (eg Sleep 1500 - which I am assuming you use to give the socket time to connect - How do you know that's long enough in every case ?)

    Also, there's a bit of a design issue as it looks as if the Socket is defined in one Form and your code is in another Form which gives you a 'disconnection', between the Events that will be generated in one Form and the dependency on those Events in the other Form. This can lead to very 'messy' code when you attempt to signal events to the other Form.

    You should be using the _Connect event which is triggered when connection has been established. Also you should be using the _Close event which is triggered when the Server closes the connection in order to close your socket gracefully.
    Code:
    Private Sub ToOcuis_Close()
    ToOcuis.Close
    End Sub
    One other possible problem is the .LocalPort setting. It should be set to 0 prior to connecting.

    EDIT: Another thought... Since the Server is closing the connection after it has received all the data, perhaps you could be pro-active and in the _SendComplete event, close the connection yourself
    Code:
    Private Sub ToOcuis_SendComplete()
    ToOcuis.Close
    End Sub
    You'd be then reasonably confident that the socket is in a closed state (and therefore know that you need to connect) the next time you want to send some data.
    Last edited by Doogle; Sep 13th, 2010 at 09:45 PM.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: WinSocket Advice

    If this is a UDP connection then communication should go in only one direction. If other client tries to reply using same connection then an error occurs and connection is closed, which in turn generates an error when another send is attempted.

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