以下是我的程序,运行的时候出现错误:“Form1”是“TcpApplication1”中的类型,不能用作表达式。
是怎么回事啊,请高手指点啊,我的程序里面好像没有用到Form1的表达式啊(Form1是窗体名称,TcpApplication1是项目名称)Imports System.Windows.Forms
Imports System.Threading
Imports System.Net.Sockets
Imports System.Net
Imports System.IOPublic Class Form1 Delegate Sub SetTextCallback(ByVal s As String)
Delegate Sub SetReadCallback()
Delegate Sub SetReadOutputCallback() Private connection As Socket
Private readThread As Thread Private socketStream As NetworkStream Private writer As BinaryWriter
Private reader As BinaryReader Private Sub New()
MyBase.new() ' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent() ' 在 InitializeComponent() 调用之后添加任何初始化。 readThread = New Thread(AddressOf RunServer)
readThread.Start() End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing System.Environment.Exit(System.Environment.ExitCode)
End Sub Private Sub TxtInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtInput.KeyDown Try If (e.KeyCode = Keys.Enter AndAlso Not connection Is Nothing) Then writer.Write("SERVER>>> " & TxtInput.Text) TxtOutput.Text &= vbCrLf & " Server>>> " & TxtInput.Text If TxtInput.Text = "TERMINATE" Then connection.Close()
End If TxtInput.Clear()
End If Catch exception As SocketException TxtOutput.Text &= vbCrLf & "Error writing object" End Try
End Sub Public Sub RunServer() Dim listener As TcpListener
Dim counter As Integer = 1 Try listener = New TcpListener(IPAddress.Parse("192.168.99.112"), 5000)
listener.Start() While True settext("Waiting for connection" & vbCrLf) connection = listener.AcceptSocket socketStream = New NetworkStream(connection) writer = New BinaryWriter(socketStream)
reader = New BinaryReader(socketStream) settext("Connection " & counter & " received." & vbCrLf) writer.Write("SERVER>>> Connection successful") setRead() Dim theReply As String = "" Try Do
theReply = reader.ReadString settext(vbCrLf & theReply) Loop While (theReply <> "Client>>> TREMINATE" AndAlso connection.Connected) Catch inputOutputException As IOException
MessageBox.Show("Client application closing") Finally settext(vbCrLf & "User terminated connection")
setReadOutput()
writer.Close()
reader.Close()
socketStream.Close()
connection.Close()
counter += 1 End Try End While Catch inputOutputException As IOException
MessageBox.Show("Server application closing") End Try
End Sub Private Sub settext(ByVal s As String) If TxtOutput.InvokeRequired Then Dim d As New SetTextCallback(AddressOf settext)
Me.Invoke(d, New Object() {s})
Else
Me.TxtOutput.Text &= s
End If End Sub Private Sub setRead() If TxtInput.InvokeRequired Then Dim d As New SetReadCallback(AddressOf setRead)
Me.Invoke(d, New Object() {})
Else
Me.TxtInput.ReadOnly = False
End If End Sub Private Sub setReadOutput() If TxtOutput.InvokeRequired Then Dim d As New SetReadOutputCallback(AddressOf setReadOutput)
Me.Invoke(d, New Object() {})
Else
Me.TxtOutput.ReadOnly = False End If End SubEnd Class
是怎么回事啊,请高手指点啊,我的程序里面好像没有用到Form1的表达式啊(Form1是窗体名称,TcpApplication1是项目名称)Imports System.Windows.Forms
Imports System.Threading
Imports System.Net.Sockets
Imports System.Net
Imports System.IOPublic Class Form1 Delegate Sub SetTextCallback(ByVal s As String)
Delegate Sub SetReadCallback()
Delegate Sub SetReadOutputCallback() Private connection As Socket
Private readThread As Thread Private socketStream As NetworkStream Private writer As BinaryWriter
Private reader As BinaryReader Private Sub New()
MyBase.new() ' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent() ' 在 InitializeComponent() 调用之后添加任何初始化。 readThread = New Thread(AddressOf RunServer)
readThread.Start() End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing System.Environment.Exit(System.Environment.ExitCode)
End Sub Private Sub TxtInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtInput.KeyDown Try If (e.KeyCode = Keys.Enter AndAlso Not connection Is Nothing) Then writer.Write("SERVER>>> " & TxtInput.Text) TxtOutput.Text &= vbCrLf & " Server>>> " & TxtInput.Text If TxtInput.Text = "TERMINATE" Then connection.Close()
End If TxtInput.Clear()
End If Catch exception As SocketException TxtOutput.Text &= vbCrLf & "Error writing object" End Try
End Sub Public Sub RunServer() Dim listener As TcpListener
Dim counter As Integer = 1 Try listener = New TcpListener(IPAddress.Parse("192.168.99.112"), 5000)
listener.Start() While True settext("Waiting for connection" & vbCrLf) connection = listener.AcceptSocket socketStream = New NetworkStream(connection) writer = New BinaryWriter(socketStream)
reader = New BinaryReader(socketStream) settext("Connection " & counter & " received." & vbCrLf) writer.Write("SERVER>>> Connection successful") setRead() Dim theReply As String = "" Try Do
theReply = reader.ReadString settext(vbCrLf & theReply) Loop While (theReply <> "Client>>> TREMINATE" AndAlso connection.Connected) Catch inputOutputException As IOException
MessageBox.Show("Client application closing") Finally settext(vbCrLf & "User terminated connection")
setReadOutput()
writer.Close()
reader.Close()
socketStream.Close()
connection.Close()
counter += 1 End Try End While Catch inputOutputException As IOException
MessageBox.Show("Server application closing") End Try
End Sub Private Sub settext(ByVal s As String) If TxtOutput.InvokeRequired Then Dim d As New SetTextCallback(AddressOf settext)
Me.Invoke(d, New Object() {s})
Else
Me.TxtOutput.Text &= s
End If End Sub Private Sub setRead() If TxtInput.InvokeRequired Then Dim d As New SetReadCallback(AddressOf setRead)
Me.Invoke(d, New Object() {})
Else
Me.TxtInput.ReadOnly = False
End If End Sub Private Sub setReadOutput() If TxtOutput.InvokeRequired Then Dim d As New SetReadOutputCallback(AddressOf setReadOutput)
Me.Invoke(d, New Object() {})
Else
Me.TxtOutput.ReadOnly = False End If End SubEnd Class
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货