-2

ユーザーが明らかにインターネットに接続されている任意のコンピューターから自宅のコンピューターをリモート制御できるようにする小さなプログラムを作成しましたが、ラップトップにテストコマンドを送信しようとすると常にこのエラーが発生します...

System.ArgumentNullException: 値を null にすることはできません。

パラメータ名:バッファ

System.Net.Sockets.NetworkStream.Read (Byte[] バッファー、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

ネットワーク プログラミングは現在、私にとって苦手分野であるため、コードに正しいロジックが含まれているかどうかさえわかりません。どんな助けでも大歓迎です。

4

1 に答える 1