17.4.11

Working with TCP IP Ports

Hi Everybody,
I would like to explain most common question within this essay , how we can work TCP Port with VB.NET 2010 console application? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-) We are creating a new Console Application Project (tcpip_port)



2-) We are writing this code

Imports System.Net.Sockets
Imports System.Text

Module Server

'define the port number
Const Port_num As Integer = 1024

Sub Main()

'write port listener to the screen
Console.WriteLine("Port Listener")

'create a tcplistiner
Dim port_Listener As New TcpListener(Port_num)

'write information the screen
Console.WriteLine("Starting to Port Listening...")

'start to listen to the port
port_Listener.Start()


Try

'accept the listener
Dim my_Client As TcpClient = port_Listener.AcceptTcpClient()

'give information to the screen
Console.WriteLine("Port Connection Accepted.")

'get stream
Dim net_s As NetworkStream = my_Client.GetStream()


Try

'define byte variable
Dim bytes(1024) As Byte

'get data
Dim bytesRead As Integer = net_s.Read(bytes, 0, bytes.Length)


'write data to the screen
Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead))

Catch e As Exception

'any problem show to the screen
Console.WriteLine(e.message)

End Try


'stop listener
port_Listener.Stop()

Catch
Console.WriteLine("Error")
Console.ReadLine()
End Try

End Sub

End Module

3-) Lets run the application ( give permission for the programme)





Have a great day


Yener Turkeli
yenerturkeli@v-basic.net


No comments:

Post a Comment