我制作了一个小程序,允许用户从任何明显有互联网连接的计算机远程控制他们的家用计算机,但是每当我尝试向我的笔记本电脑发送测试命令时,我都会收到这个错误......
System.ArgumentNullException:值不能为空。
参数名称:缓冲区
在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小)
在 C:\Users\Samuel\documents\visual studio\2012\Projects\svchost\svchost\Form1.vb:line 40 中的 svchost.Form1.accept_Client()
这是我的代码...
Imports STE
Public Class Form1 'PS, client_info is a rich text box...
Private serverListener As New Net.Sockets.TcpListener(9653) 'list for incoming connections
Private serverSender As New Net.Sockets.TcpClient 'send data back to client
Private byteFromClient() As Byte 'Allows max of 1024 characters from client.
Private byteFromServer() As Byte 'Allows max of 1024 characters from server.
Private stringFromServer As String = ""
Private stringFromClient As String = ""
Private streamFromServer As Net.Sockets.NetworkStream
Private streamFromClient As Net.Sockets.NetworkStream
Private pendingClient As New Net.Sockets.TcpClient 'client to handle
Private secure As New STE.Encryption 'encrypt data
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Recive.CancelAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Recive.RunWorkerAsync() 'Start listening thread
End Sub
Private Sub Recive_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles Recive.DoWork
serverListener.Start() 'start listening to incoming information from clients
Do While True
If serverListener.Pending = True Then
Call accept_Client()
End If
If Recive.CancellationPending = True Then
Exit Do
End If
Loop
End Sub
Private Sub accept_Client()
Try
pendingClient = serverListener.AcceptTcpClient
pendingClient.GetStream.Read(byteFromClient, 0, pendingClient.ReceiveBufferSize) 'Error here...
stringFromClient = System.Text.Encoding.ASCII.GetString(byteFromClient)
client_info.Text = stringFromClient & vbNewLine
Catch ex As Exception
client_info.Text = ex.ToString
End Try
End Sub
Private Sub Transmit_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles Transmit.DoWork
Try
Do While serverSender.Connected = False
serverSender.Connect("127.0.0.1", 9653) 'connect to local pc (temp)...
Loop
byteFromServer = System.Text.Encoding.ASCII.GetBytes(commandTB.Text)
streamFromServer.Write(byteFromServer, 0, byteFromServer.Length) 'write data to stream
streamFromServer.Flush() 'send data
Catch ex As Exception
MsgBox(ex.ToString & vbNewLine & ex.InnerException.Data.ToString)
End Try
End Sub
Private Sub send_command_Click(sender As Object, e As EventArgs) Handles send_command.Click
Transmit.RunWorkerAsync() 'start thread to transmit data to client
End Sub
End Class
网络编程目前对我来说是一个薄弱环节,所以我什至不确定我的代码是否具有正确的逻辑。任何帮助是极大的赞赏。