|
-
Sep 13th, 2010, 11:27 AM
#1
Thread Starter
Junior Member
[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
-
Sep 13th, 2010, 12:33 PM
#2
Lively Member
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
-
Sep 13th, 2010, 12:38 PM
#3
Re: WinSocket Advice
Please could you post your code
-
Sep 13th, 2010, 01:32 PM
#4
Thread Starter
Junior Member
Re: WinSocket Advice
ok here is the first bit of the code
When it loads this is call
vb Code:
FrmMainScreen.ToOcuis.Connect IPAddress, ToOcuisPort
FrmMainScreen.FromOucis.Connect IPAddress, FromOcuisPort
When a sale is requested the is called
vb Code:
FileNumber = FreeFile
sData = ""
Open PoleFolder & MySelectedFile For Input As #FileNumber
Line Input #FileNumber, sData
Close #FileNumber
Kill PoleFolder & MySelectedFile ' Deletes the request file from the Directory
Fields() = Split(sData, ",")
For i = 0 To UBound(Fields)
Fields(i) = ""
Next
Fields() = Split(sData, ",")
For i = 0 To UBound(Fields)
Trim (Fields(i))
Next
If Fields(0) = "Sale" Then
FrmMainScreen.List1.AddItem " Intergrated Sale transaction of £" & Fields(1) & " was recevied"
SendData = vbNullString
SendData = "T,,01,0000,,,,,,," & Format(Fields(1), "######0.00") & ",,,,,,,,,,,," & Fields(2) & "," & AccountID & ",,,," & Chr(13) & Chr(10)
End If
If Fields(0) = "Refund" Then
FrmMainScreen.List1.AddItem " Intergrated Refund transaction of £" & Fields(1) & " was recevied"
Kill PoleFolder & MySelectedFile
SendData = vbNullString
SendData = "T,,02,0000,,,,,,," & Format(Fields(1), "######0.00") & ",,,,,,,,,,,," & Fields(2) & "," & AccountID & ",,,," & Chr(13) & Chr(10)
End If
With FrmMainScreen.ToOcuis
If .State = 8 Then
.Connect IPAddress, ToOcuisPort
End If
Sleep 1500
If .State = 7 Then
.SendData SendData
End If
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
-
Sep 13th, 2010, 01:38 PM
#5
Re: WinSocket Advice
Please do not create duplicate threads for the same question.
Thank you.
-
Sep 13th, 2010, 01:45 PM
#6
Thread Starter
Junior Member
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
-
Sep 13th, 2010, 06:24 PM
#7
Lively Member
Re: WinSocket Advice
I told you your problem in my first post.
-
Sep 13th, 2010, 09:02 PM
#8
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.
-
Sep 14th, 2010, 09:29 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|