Making a Connection & Safely Sending a String All in VisualBasic
Results 1 to 10 of 10

Thread: Making a Connection & Safely Sending a String All in VisualBasic

  1. #1

    Post Making a Connection & Safely Sending a String All in VisualBasic

    This is a very short tutorial and isn't much of a highly thought out one, but I will be making short tut's on neat VisualBasic tricks. Hope you enjoy!

    Making a Connection Code Snippet

    This is the common way to make a connection in any VisualBasic project. It is used widely in p2p applications, and most internet related VisualBasic projects.

    Private Sub cmdConnect_Click()
    ' set up the Winsock properties
    ' and start the connect sequence
    Winsock.RemoteHost = ""
    Winsock.RemotePort = "12345"
    End Sub

    Private Sub Winsock_Connect()
    ' we're connected
    MsgBox "Connected to: " & Winsock.RemoteHostIP
    End Sub

    Private Sub sckMain_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)
    ' alert the user to the error
    MsgBox "Error: " & Description
    End Sub

    Easy, Eh?

    Safely Sending a String Snippet

    Now, to safely send a string through the VisualBasic language. This is also used in p2p applications and internet based programs for sending and recieving strings or data.

    Public Function WinsockSend(pSock As Winsock, ByVal Data As String) As Boolean
    ' send the data on the passed winsock
    If pSock.State = sckConnected Then
    ' send the data and return true
    pSock.SendData Data
    WinsockSend = True
    ' return false because we're not connected
    WinsockSend = False
    End If
    End Function

    Or, for sckMain:

    ' instead of using this to send data
    If sckMain.State = sckConnected Then
    sckMain.SendData strData
    End If

    ' i can use this
    blnRetVal = WinsockSend(sckMain, strData)

    Simple, and to the point. That's it! Next issue will be basic p2p chat creating in VisualBasic. Enjoy!

  2. #2
    Gray Haired Old Fart aeallison's Avatar
    Join Date
    Jul 2002
    Buffalo, Missouri USA
    Thanks JC this is why I came to AO, I will keep looking for more of these.
    I have a question; are you the bug, or the windshield?

  3. #3
    Thanks, Im going to have more VisualBasic tutorials in the future.

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Very cool. I look forward to the rest of them you make.

  5. #5
    Thanks, I'll have prolly around two more VisualBasic related one's that should come out within this week.

  6. #6
    Senior Member
    Join Date
    Apr 2002
    Thanks JC another little bit of code to but in the ol' tool kit. If I were not so busy looking for a job I'd look over my CD's and the Win tools there anyway great short tut. Now back to my login tuts after looking a few more hours for a job.
    I believe that one of the characteristics of the human race - possibly the one that is primarily responsible for its course of evolution - is that it has grown by creatively responding to failure.- Glen Seaborg

  7. #7
    No Problem, the next installment of the little VisualBasic set of tut's by me should be out in 2 days.

    Anyone have suggestion's toward what VisualBasic tutorials I should make? I wanna make tutorials that will benefit people's needs. My next VisualBasic tutorial will be on p2p chats.

  8. #8
    Join Date
    Jun 2002
    Nice tutorial, JC. It's refreshing to finally find an interesting VB tut. The VB tuts that i usually run across usually have something to do with "The ins and outs to making a calculator". lol

    I will be keeping an eye out for your next tutorial.

  9. #9
    Kool, Thanks Gurl. My newest one is out and is called P2P Chat's In VisualBasic. Check it out!

  10. #10
    Senior Member
    Join Date
    Sep 2001
    Yup, that winsock control is quite handy for quick and dirty sockets...
    Make sure you instal sp4 and up for vb though if you use an array of server winsock controls because there was a nasty bug before sp4 that screwd up the events from receives...
    (If multiple sockets in the array received data at the same time, only one would get noticed until data stopped flowing in to the other sockets...)


    I got bite by this when I was developping a final project for school work! Urgh! So maddening, I was looking everywhere in my code for days... then I found the KB article..! heh..

    Credit travels up, blame travels down -- The Boss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts