通过网络发送数据时出错?

时间:2013-04-18 10:42:17

标签: .net vb.net sockets networking network-programming

我制作了一个小程序,允许用户从任何显然具有互联网连接的计算机远程控制他们的家用计算机,但每当我尝试向笔记本电脑发送测试命令时,我都会收到此错误...

  

System.ArgumentNullException:值不能为空。

     

参数名称:buffer

     

at System.Net.Sockets.NetworkStream.Read(Byte [] buffer,Int32 offset,Int32 size)

     

位于C:\ Users \ Samuel \ documents \ visual studio \ 2012 \ Projects \ svchost \ svchost \ Form1.vb:第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

网络编程目前对我来说是一个薄弱环节,所以我甚至不确定我的代码是否具有正确的逻辑。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

Dim byteFromClient(1024) As Byte