Opening Socket Connection in VB

How do you open a socket connection in VB?  Is there a socket object or something?  

Thanks, and any acceptable answer with sample code with get an A.
LVL 1
RadfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
Yes, there is a winsock control that you can add to your project...

Right-click the toolbar and select "Components..." then select "Microsoft Winsock Control" from the list...


Add the control to a form and then press F1... It will give you help on the control.

Cheers!
0
mcriderCommented:
By the way, here is a chat server written with the winsock control...

Cheers!


------------------------------------------------------------

1) create a project

2) add the winsock control to your project

3) add a winsock control to your form an set it's INDEX property to 0

4) add a listbox to your form and set the SORTED Property to TRUE

5) add a command button to your form

6) Paste the following code into the DECLARATION section of the form and run it.  Anyone can then telnet into this server on port 1001 and chat.


Private Sub Command1_Click()
    If List1.ListIndex <> -1 Then Winsock1_Close CInt(Mid$(List1.List(List1.ListIndex), 12, 4))
End Sub

Private Sub Form_Load()
    Command1.Caption = "Disconnect"
    Winsock1(0).LocalPort = 1001
    Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
    Dim iVal As Long
       
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected Then
            Winsock1(iVal).SendData Winsock1(Index).RemoteHostIP + " DISCONNECTED" + vbCrLf
            DoEvents
        End If
    Next iVal
    Winsock1(Index).Close
    For iVal = 0 To List1.ListCount - 1
        If InStr(1, List1.List(iVal), "Connection " + Format$(Index, "0000")) <> 0 Then
            List1.RemoveItem iVal
            Exit For
        End If
    Next iVal
    If Index = Winsock1.UBound Then
        Unload Winsock1(Index)
    End If
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim iVal As Long
    Dim TotalConnect As Long
    Dim Acceptor As Long
       
    Acceptor = 0
    TotalConnect = 1
    For iVal = 1 To Winsock1.UBound
        Select Case Winsock1(iVal).State
            Case sckConnected
                TotalConnect = TotalConnect + 1
            Case sckClosed
                Acceptor = iVal
        End Select
    Next iVal
    If Acceptor = 0 Then Acceptor = Winsock1.Count
    If Acceptor > Winsock1.UBound Then Load Winsock1(Acceptor)
    With Winsock1(Acceptor)
        .Accept requestID
        List1.AddItem "Connection " + Format$(Acceptor, "0000") + " - " + .RemoteHostIP
        .SendData "Welcome to Telnet Chat Server" + vbCrLf + "Server hosted on " + _
            Winsock1(0).LocalIP + vbCrLf + "Total sessions connected: " + CStr(TotalConnect) + vbCrLf + vbCrLf
        DoEvents
    End With
    'ANNOUNCE THE NEW CONNECTION TO EVERYONE
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected And iVal <> Acceptor Then
            Winsock1(iVal).SendData "From SERVER: " + Winsock1(Acceptor).RemoteHostIP + " has joined session." + vbCrLf
            DoEvents
        End If
    Next iVal
       
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strData As String
    Dim iVal As Long
       
    Winsock1(Index).GetData strData, vbString
    Winsock1(Index).Tag = Winsock1(Index).Tag + strData
    If InStr(1, Winsock1(Index).Tag, vbCr) Then
        Winsock1(Index).SendData vbCrLf
        DoEvents
        'SEND THE BUFFER TO EVERYONE
        For iVal = 1 To Winsock1.UBound
            If Winsock1(iVal).State = sckConnected And iVal <> Index Then
                Winsock1(iVal).SendData "From " + Winsock1(Index).RemoteHostIP + ": " + Winsock1(Index).Tag
                DoEvents
            End If
        Next iVal
        Winsock1(Index).Tag = "" 
    Else
        'ECHO THE TEXT BACK TO THE LOCAL TERMINAL
        Winsock1(Index).SendData strData
    End If
End Sub
0
RadfordAuthor Commented:
Thanks for the help, but what if I am not using a form?  I tried:

Dim MySock As New Winsock

and

Dim MySock As Winsock
Set MySock = CreateObject("Winsock")


Neither of them worked.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mcriderCommented:
I don't know, I've never used the winsock control without a form....

However, you can add a form to your project with the winsock control on it, and just not show the form...


Other than that, I think you'll have to use APIs


Cheers!
0
RadfordAuthor Commented:
Ok thanks, but I'm going to wait and see if I can get an answer on how to use Winsocket without a form.  If I don't get a better answer in a few days, please post again and I'll give you the points.
0
VbmasterCommented:
You can call the winsock.dll file directly using a couple of API's. Since I don't know what you want to do I can just point you to a "server-client" chat program using winsock.dll and NOT that ocx file.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3976

There's some additional codes using winsock.dll at www.planet-source-code.com, it has a search feature that works occasionally. ;)
0
RadfordAuthor Commented:
I am writing an Active-X DLL that will open a socket connection to a server.  All I need to do is send a few bytes in and I'll be closing the connection.  I do not need to have a form, but I can't figure out how to instantiate a winsock object without a form.
0
mcriderCommented:
Here's a microsoft example that uses the winsock API...

Cheers!


FILE: VBFTP.EXE: Implementing FTP Using WinInet API from VB
http://support.microsoft.com/support/kb/articles/Q175/1/79.ASP

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.