Author | Message | Time |
---|---|---|
FuZe | Language: vb 6 Is there any way that I can create a winsock object without actually placing it on the form? Something like [code] Public BNET As new Winsock [/code] 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 | May 5, 2004, 12:57 AM |
Eli_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: [code] Dim CSocket as clsWinsock Form_Load() Set CSocket = New clsWinsock End Sub [/code] 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)[/quote] 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. | May 5, 2004, 1:15 AM |
K | Look at the WithEvents keyword. | May 5, 2004, 1:17 AM |
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. | May 5, 2004, 1:27 AM |
Eli_1 | 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. [code] 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 [/code] Just an example (somwhere in clsWinsock). [code] '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 ... [/code] 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. | May 5, 2004, 1:33 AM |
K | WithEvents! [code] 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 [/code] | May 5, 2004, 2:06 AM |
Eli_1 | 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)? | May 5, 2004, 2:22 AM |