|
-
Apr 15th, 2009, 05:27 PM
#1
Thread Starter
Addicted Member
Error When Adapting VB Program to WPF
I am adapting a VB program
(
Server: http://vb.net-informations.com/commu...hat_server.htm
Client: http://vb.net-informations.com/commu...hat_client.htm
)
to WPF but I am recieving an error (Parameter Count Mismatch) at the bold line:
Code:
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Threading.Thread
Public Class Window1
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Dim readData As String
Dim infiniteCounter As Integer
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
End Sub
Private Sub msg()
If TextBox1.Dispatcher.Thread.Equals(Thread.CurrentThread) Then
TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData
Else
TextBox1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New EventHandler(AddressOf msg))
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
readData = "Conected to Chat Server ..."
msg()
clientSocket.Connect("127.0.0.1", 8888)
'Label1.Text = "Client Socket Program - Server Connected ..."
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim ctThread As Thread = New Thread(AddressOf getMessage)
ctThread.Start()
End Sub
Private Sub getMessage()
For infiniteCounter = 1 To 2
infiniteCounter = 1
serverStream = clientSocket.GetStream()
Dim buffSize As Integer
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim returndata As String = _
System.Text.Encoding.ASCII.GetString(inStream)
readData = "" + returndata
msg()
Next
End Sub
End Class
The Server code is :
Code:
Imports System.Net.Sockets
Imports System.Text
Module Module1
Dim clientsList As New Hashtable
Sub Main()
Dim serverSocket As New TcpListener(8888)
Dim clientSocket As TcpClient
Dim infiniteCounter As Integer
Dim counter As Integer
serverSocket.Start()
msg("Chat Server Started ....")
counter = 0
infiniteCounter = 0
For infiniteCounter = 1 To 2
infiniteCounter = 1
counter += 1
clientSocket = serverSocket.AcceptTcpClient()
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
clientsList(dataFromClient) = clientSocket
broadcast(dataFromClient + " Joined ", dataFromClient, False)
msg(dataFromClient + " Joined chat room ")
Dim client As New handleClinet
client.startClient(clientSocket, dataFromClient, clientsList)
Next
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
Private Sub broadcast(ByVal msg As String, _
ByVal uName As String, ByVal flag As Boolean)
Dim Item As DictionaryEntry
For Each Item In clientsList
Dim broadcastSocket As TcpClient
broadcastSocket = CType(Item.Value, TcpClient)
Dim broadcastStream As NetworkStream = _
broadcastSocket.GetStream()
Dim broadcastBytes As [Byte]()
If flag = True Then
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
Else
broadcastBytes = Encoding.ASCII.GetBytes(msg)
End If
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
broadcastStream.Flush()
Next
End Sub
Public Class handleClinet
Dim clientSocket As TcpClient
Dim clNo As String
Dim clientsList As Hashtable
Public Sub startClient(ByVal inClientSocket As TcpClient, _
ByVal clineNo As String, ByVal cList As Hashtable)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Me.clientsList = cList
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
End Sub
Private Sub doChat()
Dim infiniteCounter As Integer
Dim requestCount As Integer
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
Dim sendBytes As [Byte]()
Dim serverResponse As String
Dim rCount As String
requestCount = 0
For infiniteCounter = 1 To 2
infiniteCounter = 1
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
msg("From client - " + clNo + " : " + dataFromClient)
rCount = Convert.ToString(requestCount)
broadcast(dataFromClient, clNo, True)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next
End Sub
End Class
End Module
-
Apr 16th, 2009, 01:59 AM
#2
Re: Error When Adapting VB Program to WPF
Are you saying you get that error at compile time or at run time? I pasted your code into a project and got no error there but I did get a compilation error here:
vb.net Code:
TextBox1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New EventHandler(AddressOf msg))
Method 'Private Sub msg()' does not have a signature compatible with delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.
which is quite correct. In WinForms you would have used a MethodInvoker delegate. You're not going to reference System.Windows.Forms.dll just for that though, so you should declare your own delegate:
vb.net Code:
Private Delegate Sub MethodInvoker()
Also, is this on purpose?
Code:
Dim bytesFrom(10024) As Byte
-
Apr 16th, 2009, 02:03 AM
#3
Thread Starter
Addicted Member
Re: Error When Adapting VB Program to WPF
My Error is at runtime. But that maybe be because Ive placed the code to work within a WPF project and a xaml form.
I dont know much programming.
To put it simply - I am attempting to make the program in the above-mentioned link work in wpf.
It works when downloaded and run in its windows application form
Should I post an attachment?
Last edited by quddusaliquddus; Apr 16th, 2009 at 02:09 AM.
-
Apr 16th, 2009, 02:51 AM
#4
Re: Error When Adapting VB Program to WPF
I got no such error:
Code:
Imports System.Net.Sockets
Imports System.Text
Public Class Window1
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Dim readData As String
Dim infiniteCounter As Integer
Private Delegate Sub MethodInvoker()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
End Sub
Private Sub msg()
'If Me.InvokeRequired Then
' Me.Invoke(New MethodInvoker(AddressOf msg))
If TextBox1.Dispatcher.Thread.Equals(System.Threading.Thread.CurrentThread) Then
TextBox1.Text = TextBox1.Text + _
Environment.NewLine + " >> " + readData
Else
TextBox1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New MethodInvoker(AddressOf msg))
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
readData = "Conected to Chat Server ..."
msg()
clientSocket.Connect("127.0.0.1", 8888)
'Label1.Text = "Client Socket Program - Server Connected ..."
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim ctThread As System.Threading.Thread = _
New System.Threading.Thread(AddressOf getMessage)
ctThread.Start()
End Sub
Private Sub getMessage()
For infiniteCounter = 1 To 2
infiniteCounter = 1
serverStream = clientSocket.GetStream()
Dim buffSize As Integer
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim returndata As String = _
System.Text.Encoding.ASCII.GetString(inStream)
readData = "" + returndata
msg()
Next
End Sub
End Class
What I did get was a whole bunch of non-visual characters displayed in the main text box, but that's for another thread if you can't get it sorted.
-
Apr 16th, 2009, 03:06 AM
#5
Thread Starter
Addicted Member
Re: Error When Adapting VB Program to WPF
Yeah i get the same aswell.
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
|