In one of my previous post I had shown how to capture the Caller Id info of a modem. So what do I do this the information??
In my case I notify all the PC in the house a particular caller / number is ringing by sending data on a specific port. So they know who it is before picking up or even looking at the phone for the caller Id info. Below is the code on how to monitor a port for data.
In this case it will open and monitor port 81. Since there is no event for the TCPListener Im using the VB timer control, then check the port for any pending data ever 1000ms , if data is pending it will read it and show it in a pop-up in the icon tray. 🙂
To keep it simple here, and Not shown here, I actually FTP back via code (Will setup another post for this) to the server and grab the image of the person which is then displayed on the client on another window.
Like all code here, is all in VB 2005. For setup: Note that for this to work you do need to open port 81 (TCP) on the client.
Just create a basic form with the following controls (All are default names)
A Notify object called “NotifyIcon1”
A timer object called “Timer1”
‘—————— setup my Import variables, some may not be needed.
Imports System.Web Imports System.Net Imports System.Net.Sockets Imports System.IO Imports System Imports System.ComponentModel Imports System.Threading Imports Microsoft.VisualBasic '----------------------- Here is the main class which loads the form. Public Class Form1 Public WithEvents OpenPort As TcpListener '--------- I setup the TCPListner object, There really isnt no events for it so I have to check using a timer. '--------- GET THE CLIENT ip FROM THE ENVIRONMENT This will return ina array the IP of the client in the case of XP the first ip(0) should be fine, in the case '--------- vista you have to search for it. I just looked for the first octet to be the same as my internal network. Public ip() As System.Net.IPAddress = System.Net.Dns.GetHostEntry(Environment.MachineName).AddressList Public IPAdresstxt As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'ip(0) = IPAddress.Parse(Microsoft.VisualBasic.Interaction.Command()) '---- Un-comment for XP '------- for Vista the first item of the array can be anything, so I loop thru the values looking for the same network of my home network Dim ip_count As Integer or ip_count = 0 To ip.Length - 1 If ip(ip_count).ToString.Contains("192") = True Then OpenPort = New TcpListener(ip(ip_count), 81) '--------- Here we set the IP (My PC) and the port to monitor in this case 81 IPAdresstxt = ip(ip_count).ToString End If Next '------------------- OpenPort.Start() ' - Open and monitor NotifyIcon1.Visible = True NotifyIcon1.BalloonTipTitle = "Caller ID" NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info NotifyIcon1.BalloonTipText = "Caller ID Running, window will close in 3 seconds" NotifyIcon1.ShowBalloonTip(2500) '----------------- Hide my Icon popup after 2500 MS Me.Hide() '----------- This will hide my form and only leave the icon on the tray. :) End Sub '------------------ This is my main timer1 which is enabled and checked every 1 sec. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '-------------- CHECK PORT 81 IF GOT DATA If My.Computer.Network.IsAvailable = True Then '----- If data is present then read data and pharse it. If OpenPort.Pending = True Then Dim bytes(1024) As Byte Dim data As String = Nothing Dim client As TcpClient = OpenPort.AcceptTcpClient() Dim stream As NetworkStream = client.GetStream() Dim i As Int32 ' Loop to receive all the data sent by the client. i = stream.Read(bytes, 0, bytes.Length) While (i <> 0) data = data + System.Text.Encoding.ASCII.GetString(bytes, 0, i) data = data.ToUpper() Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data) i = stream.Read(bytes, 0, bytes.Length) End While data = LCase(data).Trim ShowCallerID(data) '---------------------- Call the function to update the Notify-Icon Msgbox "Data received is"+data End If End If End Sub '---------------------- Function to update the Notify-Icon Public Function ShowcallerID(number as Integer) As Boolean NotifyIcon1.BalloonTipText = "Incomming call from " + name + vbCrLf + " From # " + number.ToString NotifyIcon1.ShowBalloonTip(10) IncommingCallWindow.txtName.Text = name If number.ToString.StartsWith("1") = True Then number = Val(Mid$(number, 2, number.ToString.Length - 1)) End If End Function End Class Enjoy!