Client Sever Network Programming Using VB.NET
Rate this article
0 out of 5
In this video tutorial, network programming using VB.NET is explained using a simple client server example. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.
Source Code
Module1.vb
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Module Module1

    Sub Main()
        Try
            Dim ipAddress As IPAddress = ipAddress.Parse("127.0.0.1") 'storing the IPAddress
            Dim tcpListener As New TcpListener(ipAddress, 8000) 'creating the listener
            tcpListener.Start() 'starting the listener
            System.Console.WriteLine("the server is running at port 8000...")
            Dim myendpoint As IPEndPoint
            myendpoint = CType(tcpListener.LocalEndpoint, IPEndPoint)
            System.Console.WriteLine("The local end point address is " + myendpoint.Address.ToString + "and port is " + myendpoint.Port.ToString)
            System.Console.WriteLine("Waiting for Connection....")
            Dim soc As Socket = tcpListener.AcceptSocket() 'creating the socket and accepting the socket from listener
            myendpoint = CType(soc.RemoteEndPoint(), IPEndPoint)
            System.Console.WriteLine("Connection Accepted From " + myendpoint.Address.ToString())
            Dim byteData(100) As Byte
            Dim size As Integer = soc.Receive(byteData) 'receiving the data from socket and reading its size
            Console.WriteLine("Received")
            For i As Integer = 0 To size
                Console.Write(Convert.ToChar(byteData(i))) 'Convert.ToChar will convert the byte value into character
            Next
            Console.WriteLine()
            Dim ascenc As New ASCIIEncoding
            Console.WriteLine("Sending Acknoledgement")
            soc.Send(ascenc.GetBytes(("The string was recieved by the server."))) 'sending the acknoledgement message
            soc.Close() 'Closing the socket
            tcpListener.Stop() 'Stopping the listener
        Catch ex As Exception
            Console.WriteLine("Error ..." + ex.ToString()) 'Printing the exception if any
        End Try
    End Sub

End Module

Client.vb
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Module Module1

    Sub Main()
        Try
            Dim tcpClient As New TcpClient 'creating the client
            Console.WriteLine("Connecting....")
            tcpClient.Connect("127.0.0.1", 8000) 'connecting the client the server
            'port is same as in the server
            Console.WriteLine("Connected")
            Console.Write("Enter the string to be transmitted : ")
            Dim strMessage As String
            strMessage = Console.ReadLine() 'reading the message from console to send
            Dim stm As Stream = tcpClient.GetStream() 'getting the stream of the client
            Dim ascenc As New ASCIIEncoding
            Dim byteData() As Byte = ascenc.GetBytes(strMessage) 'converting the data into bytes
            Console.WriteLine("Transmitting")
            stm.Write(byteData, 0, byteData.Length()) 'writing/transmitting the message
            Dim replymsg(100) As Byte
            Dim size As Integer = stm.Read(replymsg, 0, 100) 'reading the reply message and getting its size
            Console.WriteLine("Acknoledgement from Server")
            For i As Integer = 0 To size
                Console.Write(Convert.ToChar(replymsg(i))) 'writing the reply into the console
            Next
            tcpClient.Close() 'closing the connection
        Catch ex As Exception
            Console.WriteLine("Error..." + ex.StackTrace.ToString()) 'writing the exception into the console
        End Try
    End Sub

End Module



Download Source Code of Server

Download Source Code of Client

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.



Number Of articles Posted
60
Number Of Messages Posted
3
Joined Date
14/11/2012
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
   
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android