|
-
August 5th, 2002, 06:24 AM
#1
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 = "127.0.0.1"
Winsock.RemotePort = "12345"
Winsock.Connect
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
DoEvents
WinsockSend = True
Else
' 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
DoEvents
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|