• Tuesday, June 17, 2014

    [VB6] Using Winsock Basic

    Winsock is a socket component which is used for making a tcp connection as well as udp connection.
    This socket is used in many purpose....

    Let's Start


    Create a New Project and add a winsock component, command button, textbox.

    • Create a Standard EXE Project.
    • Goto Project Menu -> Components
    • Scroll Down and Select Microsoft Winsock Control and Click OK
    • Now you see the Winsock(two computer icon) icon on the toolbar.
    • Place a winsock to the form and rename it to "w"
    • Place a Command Button to the form and rename it to "cmdConnect"
    •  Place a Text Box to the form and rename it to "txtSend"
    • Place a Label and name it to lbl
    Write these code on the code window:
    • Goto Code by double clicking on form and remove the old code.
    • Now add this code
    Private Sub cmdConnect_Click()
    w.Connect "localhost", 111
    'This will connect on localhost at port 111
    cmdConnect.Enabled = False
    End Sub

    Private Sub cmdSend_Click()
    w.SendData txtSend.Text
    'Send the content of txtSend to connected socket.
    End Sub

    Private Sub Form_Load()
    Form2.Visible = True
    'Load the form 2
    cmdSend.Enabled = False 'Disable the cmdSend
    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    w.Close 
    'Closing the socket before form close. Its very important
    End Sub

    Private Sub w_Close() 

    w.Close 
    'Close the socket if connection close from other side
    MsgBox "Connection closed from one side..."
    End
    End Sub

    Private Sub w_Connect()
    cmdSend.Enabled = True
    w.SendData "Hi... This is a test"
    End Sub

    Private Sub w_DataArrival(ByVal bytesTotal As Long)
    Dim buff As String
    w.GetData buff, vbString 
    'Getting other side data in buff variable.
    lbl.Caption = buff
    End Sub


    Create a new form and add the controls
    • Create a new form (Project -> Add Form) 
    • Place a winsock to the form and rename it to "x"
    • Place a Command Button to the form and rename it to "cmdListen"
    • Place a Text Box to the form and rename it to "txtSend"
    • Place a Label and name it to lbl.
    • Place a Command Button and rename to "cmdSend"
    Add following code to form 2

    Private Sub cmdListen_Click()
    x.LocalPort = 111
    'Assign the localport 111 to winsock to listen on it
    x.Listen 
    'Start listning for connection on assigned port.
    cmdListen.Enabled = False
    End Sub

    Private Sub cmdSend_Click()
    x.SendData txtSend.Text
    txtSend.Text = ""
    End Sub

    Private Sub Form_Load()
    cmdSend.Enabled = False
    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    x.Close
    End Sub

    Private Sub x_ConnectionRequest(ByVal requestID As Long)
    x.Close 
    'Close listning for connection
    x.Accept requestID 
    'Accept the incoming request
    cmdSend.Enabled = True
    End Sub

    Private Sub x_DataArrival(ByVal bytesTotal As Long)
    Dim buff As String
    x.GetData buff, vbString
    lbl.Caption = buff
    End Sub

    Private Sub x_Close()
    x.Close
    MsgBox "Connection closed from one side..."
    End
    End Sub


    Now run the project.
    • Run the project. Both form will shown.
    • First click on Listen Button to strat listning.
    • Then click on connect button to connect with the listning socket.
    • Now write text in text box and send in any of form.
    For any query, problem or questions comment here or directly ask at: masoomrizvi786@gmail.com



    Syed Masoom Rizvi

    Author & Editor

    Hi, This is Syed Masoom Rizvi, who is continue exploring the world of programming ,programming and programming... :)

    0 comments:

    Post a Comment