ADVERTISEMENT
ADVERTISEMENT

VB.Net program to implement socket client

Here, we are going to learn how to implement socket client in VB.Net?
Submitted by Nidhi, on January 18, 2021

Here, we will implement a socket client that will connect to the socket server according to the given IP address and port number. Then we can communicate to the server and send and receive data.

Program/Source Code:

The source code to implement the socket client is given below. The given program is compiled and executed successfully.

'VB.Net program to implement the socket client.

Imports System.Text
Imports System.Net.Sockets
Imports System.IO

Module Module1
    Sub Main()
        Dim client As New TcpClient()
        Dim Encoding As New ASCIIEncoding()

        Dim strm As Stream
        Dim sendStr As String = "Hello"

        Dim recvLen As Integer = 0

        Dim sendByteArray() As Byte
        Dim recvByteArray(256) As Byte

        Console.WriteLine("Connecting.....")
        client.Connect("127.0.0.1", 15001)

        Console.WriteLine("Connected to server")
        strm = client.GetStream()

        sendByteArray = Encoding.GetBytes(sendStr)

        strm.Write(sendByteArray, 0, sendByteArray.Length)
        Console.WriteLine("Data send...")

        recvLen = strm.Read(recvByteArray, 0, 100)

        For i = 0 To recvLen - 1 Step 1
            Console.Write(Convert.ToChar(recvByteArray(i)))
        Next

        client.Close()
        Console.WriteLine()
    End Sub
End Module

Output:

Connecting.....
Connected to server
Data send...
data received
Press any key to continue . . .
implement socket client

Explanation:

In the above program, we created a socket server on the localhost with a 15001 port. Here, the server is listening to accept client connections and receive data from the server and send data to the server. To test the program, here we used the most common network test utility (Hercules), then we connect to our server using Hercules and send data "Hello" using Hercules then our server program sends acknowledge data "I have received data" message to the Hercules tool.

VB.Net Socket Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.