Solved

How to make Winsock Control ... ??

Posted on 2001-06-18
15
435 Views
Last Modified: 2013-11-13
I'm in need of a user control that is exactly same in functionality to that of Winsock.ocx So, It might be using API Functions of winsock.dll

I want the source code for that user control. Please send it to me at ajay@chadhaajay.net or at chadhaajay@hotmail.com

It should be same in all the cases to winsock.ocx

For example, these events must be same as that of Winsock.ocx

.Connect

.Close

.SendData

I mean to say that the parameters passed to these functions must be exactly same.

The following events must also be there in the control as these are in Winsock.ocx

Private Sub Winsock_Connect()

Private Sub Winsock_SendComplete()

Private Sub Winsock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

Good Luck...

Ajay Chadha .. :-)
0
Comment
Question by:AJAY CHADHA
  • 5
  • 3
  • 2
  • +4
15 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Why don't you want to use the Winsock Control itself?
You might ask Microsoft to give you the code! (good luck)
Cheers
0
 
LVL 8

Author Comment

by:AJAY CHADHA
Comment Utility
Actually, My projet manager told me to complete a usercontrol that have the same functionality of Winsock Control.

Ajay Chadha .. :-)
0
 
LVL 4

Expert Comment

by:PBuck
Comment Utility
The only lead I can give you is to download the free control from www.catalyst.com - called SocketWrench.  It is basically a wrapper control for Winsock (but with no memory leaks), so all the calls are the same.

ie. .Connect, .SendData, .etc ...

The install provides excellent source code samples from Telnet to UDP.
- but, sorry to say, no source code for the control itself.

The reason I suggest this is I have created a TCP Port listener DLL and a seperate Telnet DLL (all from referencing this control) - very cool.

Private WithEvents TSocket As SocketWrenchCtrl.Socket 'Create your own DLL or ActiveX control.

Good Luck!
0
 
LVL 8

Author Comment

by:AJAY CHADHA
Comment Utility
I need the source code for the control

Ajay Chadha .. :-)
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 70 total points
Comment Utility
I found this code (I believe on PSC)
Check it out

' Name: Winsock ocx without a form
' Description:This class wraps the winsock.ocx methods and properties. This allows you to use winsock
functions without putting the ocx on a form. This class can be compiled into a dll if wanted.

' Assumes:Make sure that the toolbar does not have the winsock control on it,
otherwise this code will not work.
'
'Make sure to have a reference to the winsock.ocx in the project where you're going
' to use this class or this won't work.

'     ---------------------------------
Option Explicit
Private WithEvents objSocket As Winsock
Public Event DataInStream(ByVal lngSocketNumber As Long, ByVal strData As String)
Public Event SocketClosed(ByVal lngSocketNumber As Long)
Public Event ConnectionRequested(ByVal lngSocketNumber As Long)
Public Event AcceptedSocket(ByVal lngSocketNumber As Long)
Private mvarPortNumber As Long
Private mvarCurrDataStream As String
Private mvarCurrentID As Long
Private blnSoftReturn As Boolean

'***************************************
'* Property: CurrentSocketID
'* Notes: Returns the current socket number.
'***************************************
Public Property Get CurrentSocketID() As Long
    CurrentSocketID = mvarCurrentID
End Property

'***************************************
'* Property: CurrDataStream
'* Notes: Returns the raw input from thecurrent socket.
'***************************************
Private Property Let CurrDataStream(ByVal vData As String)
    mvarCurrDataStream = vData
End Property

Public Property Get CurrDataStream() As String
    CurrDataStream = mvarCurrDataStream
End Property

'***************************************
'* Property: LocalPort
'* Notes: Returns/Sets the port where the socket will be listening on.
'***************************************
Public Property Let LocalPort(ByVal vData As Long)
    mvarPortNumber = vData
    objSocket.LocalPort = vData
End Property

Public Property Get LocalPort() As Long
    LocalPort = mvarPortNumber
End Property

Private Sub Class_Initialize()
    Set objSocket = New Winsock
End Sub

Private Sub Class_Terminate()
    If objSocket.State <> sckClosed Then objSocket.Close
    Set objSocket = Nothing
End Sub

'---------------------------------------
Private Sub objSocket_ConnectionRequest(ByVal requestID As Long)
    If objSocket.State <> sckClosed Then objSocket.Close
    mvarCurrentID = requestID
    RaiseEvent ConnectionRequested(requestID)
End Sub
'---------------------------------------
Private Sub objSocket_DataArrival(ByVal bytesTotal As Long)
    Dim strIncoming As String
    Static strInputBuffer As String
    Dim strOutBuffer As String
    Dim intPos As Integer
    objSocket.GetData strIncoming
    CurrDataStream = strIncoming
    mvarCurrentID = objSocket.SocketHandle
    ' Replace Carriage Returns/Line Feeds or
    '     just Line Feeds with
    ' a Carriage Return for consistant handl
    '     ing.
    strIncoming = Replace$(strIncoming, vbCrLf, vbCr)
    strIncoming = Replace$(strIncoming, vbLf, vbCr)
    ' Check for Carriage Returns in the incoming stream, and mark
    ' the position where it's found, if any.
    '    
    intPos = InStr(1, strIncoming, vbCr)

    ' Make sure that the Carriage Return is  not at the beginning of the stream.
    ' If the Carriage Return is at position  1 then it means that it belongs to the
    ' previous stream.


    If intPos > 1 Then
        ' Grab a string including the Carriage R
        '     eturn for processing.
        strOutBuffer = Left$(strIncoming, intPos)
        strOutBuffer = StripCRLF(strIncoming)
        RaiseEvent DataInStream(mvarCurrentID, strOutBuffer)
        ' Flush the buffers so that data won't get added to the next stream.
        strOutBuffer = ""
        strInputBuffer = ""
    Else
        ' Add to the input buffer if there is no
        '     Carriage Return.
        strInputBuffer = strInputBuffer & strIncoming
    End If

    ' The code below handles broken packets,
    '     meaning that all the data did not come in one stream.
    '***************************************

    If Right$(strIncoming, 1) = vbCr Then'check last character
        blnSoftReturn = True
    End If


    If blnSoftReturn = True Then


        If Left$(strIncoming, 1) = vbCr Then
            strOutBuffer = Mid$(strInputBuffer, 1)
            strOutBuffer = StripCRLF(strOutBuffer)
            RaiseEvent DataInStream(mvarCurrentID, strOutBuffer)
            ' Flush the buffers so that data won't g
            '     et added to the next stream.
            strOutBuffer = ""
            strInputBuffer = ""
        End If
        blnSoftReturn = False
    End If
End Sub


Private Sub objSocket_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    'Error handling.
    ' and error usually means that the socked was disconnected.
    If objSocket.State <> sckClosed Then objSocket.Close
    MsgBox "Something happened To socket #" & CStr(Number)
End Sub
'---------------------------------------
Private Function StripCRLF(strData As String)
    Dim strHold As String
    strHold = Replace(strData, vbCr, "")
    strHold = Replace(strHold, vbLf, "")
    StripCRLF = strHold
End Function
'---------------------------------------
Public Sub SocketListen()
    objSocket.Listen
End Sub
'---------------------------------------
Public Sub SocketClose()
    objSocket.Close
End Sub
'---------------------------------------
Public Sub AcceptRequest(ByVal lngSocketNumber As Long)
    objSocket.Accept lngSocketNumber
    RaiseEvent AcceptedSocket(lngSocketNumber)
End Sub
'---------------------------------------
Public Sub SendData(ByVal sDataToSend As String)
    objSocket.SendData sDataToSend
End Sub
0
 
LVL 3

Expert Comment

by:daffyduck14mil
Comment Utility
Chadha,

Try the TTOSocket. That is a control that uses winsock api, and comes with complete source code. You can obtain the source code at http://ttosock.2y.net

The only thing you might object to is the fact that not all the events have the same names, but allas.

Grtz.?

D.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
it might be silly, but i repeat my question:
Why don't you/your project manager don't want to use the Winsock Control itself?
CHeers
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:ameba
Comment Utility
Where do I send the full code?
0
 
LVL 8

Author Comment

by:AJAY CHADHA
Comment Utility
Ok, Experts.

Finally My Project Manager told me that he want to have this code without winsock.ocx and with API of winsock dll

http://www.vbcode.com/asp/showzip.asp?ZipFile=http%3A%2F%2Fwww%2Evbcode%2Ecom%2Fcode%2Ficqp%2Ezip&theID=2283

Can you please do it as I've no experinec with Winsock API.

Ajay Chadha .. :-)
0
 
LVL 8

Author Comment

by:AJAY CHADHA
Comment Utility
ThanX Ameba at ajay@chadhaajay.net

or at chadhaajay@hotmail.com

Good Luck..

Ajay Chadha .. :-)
0
 
LVL 3

Expert Comment

by:daffyduck14mil
Comment Utility
In that case, I _repeat_ my statement about TTOSock. This comes with full source code, and it uses the API.

The control is stable, and is "ready to use right out of the zip".

Grtz.?

D.
0
 
LVL 8

Author Comment

by:AJAY CHADHA
Comment Utility
0
 

Expert Comment

by:garthmiller
Comment Utility
daffyduck14mil:

I tried going to the link you gave for TTOSock, but there
is no such page. Did you somehow give us the wrong link?

0
 

Expert Comment

by:garthmiller
Comment Utility
daffyduck14mil:

I tried going to the link you gave for TTOSock, but there
is no such page. Did you somehow give us the wrong link?

0
 
LVL 3

Expert Comment

by:daffyduck14mil
Comment Utility
The site itself has died, I don't know if the person that made the control has moved it elsewhere, or if the project has been abandoned.

Grtz.&copy;

D.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now