• Welcome to Valhalla Legends Archive.
 

Winsock Class

Started by FuZe, May 04, 2004, 07:57 PM

Previous topic - Next topic

FuZe

Language: vb 6
Is there any way that I can create a winsock object without actually placing it on the form? Something like

Public BNET As new Winsock


For objects like Winsock, I can only set a reference variable that points to a winsock on a form.  I know you can create an array using the winsock object by setting the index to 0, and loading new winsocks, but is there a way with code only? (and not using the windows API).  Thanks

Eli_1

#1
You could create a class that uses winsocks API calls to create a socket, connect, send, and receive data. Then place it all in a class.

Then, you could do:

Dim CSocket as clsWinsock
Form_Load()
   Set CSocket = New clsWinsock
End Sub


If your serious, I suggest you look up some of these:
WSAStartup
WSAGetLastError
WSACleanup
socket
send
recv
connect
inet_addr
htons

struct (type in VB) sockaddr_in
const AF_INET
const SOCK_STREAM
const IPPROTO_TCP

www.allapi.net has an example that uses all of these (IIRC).

[Edit] I just saw this
Quote
(and not using the windows API)
No. That's all the winsock control does. It's a class with functions, which uses these API calls, compiled into .ocx form. The winsock control is just a level of abstraction.

K

Look at the WithEvents keyword.

shadypalm88

But if you wanted to create a Winsock control that's not on a form, sorry, but VB won't let you.  IIRC, this is by design, something like it needs to be on a form so VB knows you have the appropriate license to use it.

Eli_1

#4
No. You can create a new 'Class Module' and duplicate all the function the 'Winsock Control' uses, using the winsock API calls I listed above.  :o

Then you can just use it like you would the control.

Dim CWinsock as new clsWinsock '<- Class mod you made

Sub Form_Load()
   Set CWinsock = New clsWinsock
   CWinsock.Connect "useast.battle.net", 6112
End Sub

Private Sub CWinsock_Connect()
   'YAY CONNECTED! :P
End Sub

Private Sub CWinsock_Error(ByVal ErrorNumber, ByVal Description)
'poop -.-
End Sub


Just an example (somwhere in clsWinsock).

'clsWinsock
Public Event Connect()
Public Event Error(byval ErrorNumber as long, _
byval Description as string)

...

Public Sub Connect(Optional Server as string, Optional Port as long)
   dim i as integer

   if Server = "" Then
       Server = GetServer
       Port = GetPort
   End If

   If SocketHandle& = 0 Then
       Call Startup()
       Call CreateSocket()
   End If
   
   i = connect(SocketHandle, sin,  Len(sin))
   if i = SOCKET_ERROR Then
       RaiseEvent Error(WSAGetLastError, someothercrap)
   else
       RaiseEvent Connect
   end if
End Sub
...

Just an example, half of it's probably wrong but that's besides the point. I think what he wants to do is very possible.

K

WithEvents!


Dim WithEvents myWinsock As Winsock

'...
Set myWinsock = New Winsock 'this might not work; you may have to use CreateObject()
myWinsock.Connect( ... )


'...
Private Sub myWinsock_Connected()
MsgBox "Connected!"

End Sub

Eli_1

#6
Oh wow. I knew about WithEvents, but I didn't know you could use it like that.

You would still have to include the .OCX with your program though right (assuming the person your giving your program to doesn't already have it)?