|
-
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
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
|