Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock - working in .exe and not in .dll 2

Status
Not open for further replies.

wrbodine

Programmer
Aug 24, 2000
302
0
0
US
Hi,

I have a winsock control (VB6 SP5) that I'm using to connect to a server with the following lines of code:

with frmWin
.Winsock1.RemoteHost = strServer
.Winsock1.RemotePort = Val(strPort)
.Winsock1.Connect

If .Winsock1.State = 7 Then
' more code here...
return strMessage
Else
return .Winsock1.state
End If
end with

This works great in a standalone .exe (run from my desktop), but when I put it in a .dll and call it from another .exe, or from an .asp page the state is no longer 7 ("Connected to host") but 4 ("Looking up host server")

Any ideas why I'm getting this error and how to resolve it?

Thanks,
Ray
 
Well, the connection problem is resolved:

.Winsock1.Close
.Winsock1.RemoteHost = strServer
.Winsock1.RemotePort = Val(strPort)
.Winsock1.Connect
Do Until .Winsock1.State = 7 Or .Winsock1.State = 9 ' 9 for sckError
DoEvents
Loop

This took care of it.

Now I'm having problems with where to put the code that was in Winsock1_DataArrival. The Winsock control is on a separate form, and is referenced by frmWin.Winsock1 Would I put the code for the Winsock_DataArrival method in that form, and access the variable with the result in the class module?

Thanks,
Ray
 
You should be able to declare a private member in the class

Private WithEvents objSock As Winsock

Then in Class_Initialize

Set objSock = frmWin.Winsock1

After that you can put all the Winsock procedures in the class module and reference the private member. In Class_Terminate:

Set objSock = Nothing

Paul Bent
Northwind IT Systems
 
Just to clarify further about the DataArrival Event. You have an ActiveX dll with a class module and a form containing the Winsock conttol.

You would still put the Winsock1_DataArrival code in the form module but it can set form level variables which can be accessed from the class module.

In the form code you might have something like:
Code:
Const MAX_DATA = 1000
Private strFBuffer As String * MAX_DATA
Public strVBuffer As String
Public blnDataReady As Boolean

Private Sub Form_Load()
 blnDataReady = False
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

 If bytesTotal > MAX_DATA Then
  'handle error and exit
 End 
 Winsock1.GetData strFBuffer, vbString
 If Not blnDataReady Then
  strVBuffer = vbNullString
 End If
 strVBuffer = Mid$(strFBuffer, 1, bytesTotal)
 blnDataReady = True

End Sub
Then in the class module you might have something like:
Code:
Private msngTimeout As Single  'set it to 60 secs (say) in Initialize

Private Function fWaitFor(strTarget As String) As Boolean
 
 Dim sngEnd As Single

 sngEnd = Timer + msngTimeout
 Do Until frmWin.DataReady
  If Timer > sngEnd Then
   'handle timeout error and exit
  End If
  DoEvents
 Loop
 fWaitFor = (strTarget = Left$(frmWin.strVBuffer, Len(strTarget)))
 frmWin.blnDataReady = False

End Function
I hope that clarifies things. Winsock methods like Logon can be public class members and reference the private objSock object I described in my previous post.

Paul Bent
Northwind IT Systems
 
Thanks so much Paul!

That took care of things; really really helpful post!

Have a good one,
Ray
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top