C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » VB.NET Syntax »

Winsock Component


Posted Date: 11 Nov 2008    Resource Type: Code Snippets    Category: VB.NET Syntax
Author: Abraham KuriakoseMember Level: Gold    
Rating: 1 out of 5Points: 10



1. In Visual Studio.NET create new Visual Basic Windows Application.
2. Click on Project menu and select "Add Reference".
3. In "Add Reference" dialog box click on COM tab and select OSWinsck. Click OK to save a new reference.
4. Enter the following code:


Public Class frmMain
Inherits System.Windows.Forms.Form

Dim sPage As String
Dim WithEvents wsTCP As New OSWINSCK.Winsock

#Region " Windows Form Designer generated code "

Private Sub cmdView_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdView.Click
On Error GoTo ErrHandler
Dim sServer As String
Dim nPort As Long

nPort = 80
sServer = Trim(txtURL.Text)
If InStr(sServer, "://") > 0 Then _
sServer = Mid(sServer, InStr(sServer, "://") + 3)
If InStr(sServer, "/") > 0 Then
sPage = Mid(sServer, InStr(sServer, "/") + 1)
sServer = Strings.Left(sServer, InStr(sServer, "/") - 1)
End If
If InStr(sServer, ":") > 0 Then
nPort = Mid(sServer, InStr(sServer, ":") + 1)
sServer = Strings.Left(sServer, InStr(sServer, ":") - 1)
End If
If sServer = "" Then Err.Raise(12001, , "Invalid URL")

wsTCP.Connect(sServer, CInt(nPort))
Exit Sub

ErrHandler:
MsgBox("Error " & Err.Number & ": " & Err.Description)
End Sub

Private Sub wsTCP_OnClose() Handles wsTCP.OnClose
wsTCP.CloseWinsock()
End Sub

Private Sub wsTCP_OnConnect() Handles wsTCP.OnConnect
wsTCP.SendData("GET /" & sPage & " HTTP/1.0" & vbCrLf & vbCrLf)
End Sub

Private Sub wsTCP_OnDataArrival(ByVal bytesTotal As Integer) _
Handles wsTCP.OnDataArrival
Dim sBuffer As String
wsTCP.GetData(sBuffer)
txtSource.Text = txtSource.Text & sBuffer
End Sub

Private Sub wsTCP_OnError(ByVal Number As Short, _
ByRef Description As String, ByVal Scode As Integer, _
ByVal Source As String, ByVal HelpFile As String, _
ByVal HelpContext As Integer, ByRef CancelDisplay As Boolean) _
Handles wsTCP.OnError
MsgBox(Number & ": " & Description)
End Sub

Private Sub wsTCP_OnStatusChanged(ByVal Status As String) _
Handles wsTCP.OnStatusChanged
Console.WriteLine(Status)
End Sub
End Class




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Winsock  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Color Dialog
Previous Resource: Adding filters to the Open File dialog and Selecting Multiple Files
Return to Discussion Resource Index
Post New Resource
Category: VB.NET Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use