|
-
May 12th, 2012, 09:31 PM
#1
Thread Starter
New Member
Error?? TcpListener
Hi There,
I have nearly finished my network file transfer program, but I am getting the following error when I try to send a file to the server,
Invalid Cast Exception was handled
Unable to cast object of type "System.Int32" to type "System/net/Sockets.TCPListener".
Here is my code:
Code:
Imports System.Net
Imports System.Net.Sockets
Imports Microsoft.Win32
Public Class Main
Private listen As Threading.Thread
Private Sub Status_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Status.Tick
If ToolStripStatusLabel1.Text = "Server Running" Then
ToolStripStatusLabel1.ForeColor = Color.Green
ElseIf ToolStripStatusLabel1.Text = "Server Error!" Then
ToolStripStatusLabel1.ForeColor = Color.Red
End If
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset1.txt")
TextBox2.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset2.txt")
TextBox3.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset3.txt")
TextBox4.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset4.txt")
End Sub
Sub listener()
Dim l As New TcpListener(Net.IPAddress.Any, 8080)
l.Start()
Do
If l.Pending Then l.BeginAcceptTcpClient(AddressOf accepting, 1)
Threading.Thread.Sleep(10000)
Loop
End Sub
Sub accepting(ByVal ar As IAsyncResult)
'receives filename, filelength, filebytes
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
Dim filename, filepath As String, filelen As Long
'using binaryreader (wrapped networkstream) to read a String and a Long
Dim br As New IO.BinaryReader(clientSocket.GetStream)
filename = br.ReadString
filelen = br.ReadInt64
'filepath = IO.Path.Combine(Application.StartupPath, filename)
If filename.Contains(".wav") Then
filepath = ("C:\remote Audio\Audio\Audio.wav")
Else
filepath = IO.Path.Combine("C:\remote Audio\Audio", filename)
End If
Dim buffer(8092) As Byte, readstotal As Long = 0
Dim reads As Integer = -1
'using filestream to write read filebytes directly from networkstream
Using fs As New IO.FileStream(filepath, IO.FileMode.Create, IO.FileAccess.Write)
Do Until readstotal = filelen
reads = clientSocket.GetStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, reads)
readstotal += reads
Loop
End Using
End Sub
Private Sub Main_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
System.Diagnostics.Process.GetCurrentProcess.Kill()
End Sub
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listen = New Threading.Thread(AddressOf listener)
listen.Start()
ToolStripStatusLabel1.Text = "Server Running"
Try
Catch ex As Exception
ToolStripStatusLabel1.Text = "Server Error!"
End Try
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset1.txt")
TextBox2.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset2.txt")
TextBox3.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset3.txt")
TextBox4.Text = My.Computer.FileSystem.ReadAllText("C:\Remote Audio\Data\Preset4.txt")
End Sub
Private Sub Audio_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Audio.Tick
Try
If My.Computer.FileSystem.FileExists("C:\remote Audio\Audio\Audio.wav") Then
Audio_Play.start()
End If
Catch ex As Exception
Audio_Error.SetError(Me, "Invalid Audio File!")
End Try
End Sub
Private Sub Audio_Play_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Audio_Play.Tick
Try
My.Computer.Audio.Play("C:\Remote Audio\Audio\Audio.wav", AudioPlayMode.WaitToComplete)
My.Computer.FileSystem.DeleteFile("C:\remote Audio\Audio\Audio.wav")
Catch ex As Exception
MsgBox("Invalid Audio file!", MsgBoxStyle.Critical, "Error")
Audio_Error.SetError(Me, "Invalid Audio File!")
End Try
End Sub
Private Sub RunInBackgroundToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunInBackgroundToolStripMenuItem.Click
Me.Hide()
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Me.Show()
End Sub
Private Sub Preset_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Preset_Status.Tick
If My.Computer.FileSystem.FileExists("C:\remote audio\audio\preset1.txt") Then
Label1.ForeColor = Color.Red
Label1.Text = "Preparing to Play Preset 1"
Preset1.Start()
ElseIf My.Computer.FileSystem.FileExists("C:\remote audio\audio\preset2.txt") Then
Label1.ForeColor = Color.Red
Label1.Text = "Preparing to Play Preset 2"
Preset2.Start()
ElseIf My.Computer.FileSystem.FileExists("C:\remote audio\audio\preset3.txt") Then
Label1.ForeColor = Color.Red
Label1.Text = "Preparing to Play Preset 3"
Preset3.Start()
ElseIf My.Computer.FileSystem.FileExists("C:\remote audio\audio\preset4.txt") Then
Label1.ForeColor = Color.Red
Label1.Text = "Preparing to Play Preset 4"
Preset4.Start()
Else
Label1.ForeColor = Color.Green
Label1.Text = "Waiting for Client"
End If
End Sub
Private Sub Preset1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Preset1.Tick
Try
My.Computer.Audio.Play(My.Resources.vchord_g7, AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(TextBox1.Text, AudioPlayMode.WaitToComplete)
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset1.txt")
Preset1.Stop()
Catch ex As Exception
If My.Computer.FileSystem.FileExists("C:\Remote Audio\Audio\Preset2.txt") = True Then
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset2.txt")
MsgBox("Error! Cannot Play Preset 2!" & vbNewLine & vbNewLine & "File does not exist or is invalid.", MsgBoxStyle.Critical, "Error")
End If
End Try
End Sub
Private Sub PresetAudioSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PresetAudioSettingsToolStripMenuItem.Click
Preset.ShowDialog()
End Sub
Private Sub Preset2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Preset2.Tick
Try
My.Computer.Audio.Play(My.Resources.vchord_g7, AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(TextBox2.Text, AudioPlayMode.WaitToComplete)
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset2.txt")
Preset2.Stop()
Catch ex As Exception
If My.Computer.FileSystem.FileExists("C:\Remote Audio\Audio\Preset2.txt") = True Then
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset2.txt")
MsgBox("Error! Cannot Play Preset 2!" & vbNewLine & vbNewLine & "File does not exist or is invalid.", MsgBoxStyle.Critical, "Error")
End If
End Try
End Sub
Private Sub Preset3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Preset3.Tick
Try
My.Computer.Audio.Play(My.Resources.vchord_g7, AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(TextBox3.Text, AudioPlayMode.WaitToComplete)
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset3.txt")
Preset3.Stop()
Catch ex As Exception
If My.Computer.FileSystem.FileExists("C:\Remote Audio\Audio\Preset3.txt") = True Then
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset3.txt")
MsgBox("Error! Cannot Play Preset 3!" & vbNewLine & vbNewLine & "File does not exist or is invalid.", MsgBoxStyle.Critical, "Error")
End If
End Try
End Sub
Private Sub Preset4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Preset4.Tick
Try
My.Computer.Audio.Play(My.Resources.vchord_g7, AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(TextBox4.Text, AudioPlayMode.WaitToComplete)
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset4.txt")
Preset4.Stop()
Catch ex As Exception
If My.Computer.FileSystem.FileExists("C:\Remote Audio\Audio\Preset4.txt") = True Then
My.Computer.FileSystem.DeleteFile("C:\Remote Audio\Audio\Preset4.txt")
MsgBox("Error! Cannot Play Preset 4!" & vbNewLine & vbNewLine & "File does not exist or is invalid.", MsgBoxStyle.Critical, "Error")
End If
End Try
End Sub
Private Sub StatusStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles StatusStrip1.ItemClicked
End Sub
End Class
Thanks!
Josh
-
May 12th, 2012, 10:00 PM
#2
Re: Error?? TcpListener
There's a lot of code there and there's no reason that we should have waste our time searching through it all looking for a possible place that that exception is thrown when you could simply tell us exactly where the exception is thrown. Help us to help and you're much more likely to get a solution.
-
May 12th, 2012, 10:04 PM
#3
Thread Starter
New Member
Re: Error?? TcpListener
Hi,
The error comes up here
Code:
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
Thanks,
Josh
-
May 12th, 2012, 10:28 PM
#4
Re: Error?? TcpListener
When you call BeginAcceptTcpClient, whatever you pass to the 'state' parameter is what you get back from the ar.AsyncState property inside the callback. Have a look at where you're calling BeginAcceptTcpClient and what you're passing as an argument. I think I can see why you made the mistake and, if I'm right, it should encourage you to use more descriptive variable names in future.
-
May 12th, 2012, 10:55 PM
#5
Thread Starter
New Member
Re: Error?? TcpListener
Can you please explain better?
-
May 12th, 2012, 11:08 PM
#6
Re: Error?? TcpListener
It's quite simple. You get get back (the value of ar.AsyncState) whatever you pass in (the second argument to BeginAcceptTcpClient). You are obviously expecting to get back a TcpListener so you would have to be passing in a TcpListener in the first place. Are you doing that? Obviously not, or you wouldn't be getting that error message. The error message says that it's failing to cast an Int32 as type TcpListener, so you must be passing in an Int32.
-
May 13th, 2012, 01:29 AM
#7
Thread Starter
New Member
Re: Error?? TcpListener
So what would the basic steps be to fixing this? 
As I'm only a student learning vb.
Thanks.
-
May 13th, 2012, 01:53 AM
#8
Re: Error?? TcpListener
Have you actually identified where the problem is? Have you done what I said? Can you actually show me where the error is, or are you just waiting for me to tell you what to do? If you're capable of writing a network file transfer program using asynchronous methods then you should be able to follow my instructions to identify where the problem is, what the problem is and what to do to fix it.
-
May 13th, 2012, 02:03 AM
#9
Thread Starter
New Member
Re: Error?? TcpListener
Hi,
Yes, and I think it might be fixed, after doing some checks.
I found that I had put a 1 instead of a L, as they look very simular in VB.
It was this part of the code:
Code:
If l.Pending Then l.BeginAcceptTcpClient(AddressOf accepting, l)
Thanks!
Josh
-
May 13th, 2012, 02:13 AM
#10
Re: Error?? TcpListener
 Originally Posted by josh133
I found that I had put a 1 instead of a L, as they look very simular in VB.
Exactly. Hopefully you feel better for having found it yourself, albeit with a little help. It is always my intention to get the poster to do as much as they can for themselves because they learn more that way. As for this:
if I'm right, it should encourage you to use more descriptive variable names in future.
The implication was that you would not have made that mistake if you had used a descriptive variable name like 'listener'.
-
May 13th, 2012, 04:58 AM
#11
Thread Starter
New Member
Re: Error?? TcpListener
Thanks Again!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|