2017-05-31 13 views
-1

私のプログラムは、ソケットプログラミングを使用してサーバーからデータを読み込んで、リモートPCのプロセスを終了させる必要があります。 ソケット接続からネットストリームを変換した後、ストリームをStringに変換しますが、入力文字列が文字列に変換されないため、プロセスを強制終了できません。 .remove()などの文字列メソッドを使用できるので困惑しますが、入力変数を関数killProcessに渡すとprocessessは削除されません。(バイト)データストリームをVb.netの文字列に変換する方法

'socket datastream 
If NetStream.DataAvailable Then 
     Dim bytes(TCPClient.ReceiveBufferSize) As Byte 
     Dim input As String 
     NetStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize)) 
     lblConnectStatus.Text = ("Server was disconnected ") 
     input = Encoding.ASCII.GetString(bytes) 

    If input.StartsWith("kill") Then 
     input = input.Remove(0, 4) 
      Try 
       KillProcess(input) 
      Catch ex As Exception 

      End Try 
    End if 
End if 

'kill process function 
'this function works when i write the program name manually eg.:"notepad" 
'but it doesn't work when i pass the parameter pid for the killProcess() function 
Private Sub KillProcess(ByVal pid As String) 
    For Each P As Process In System.Diagnostics.Process.GetProcessesByName(pid) 
     P.Kill() 
    Next 
End Sub 
+1

PIDは名前ではありませんので?あなたは 'Process.GetProcessById()'が必要です...私はまた、ネットワーク部分がここでどのように関連しているかはよく分かりません。 :) – CodeCaster

+1

NETに問題があると伝えようとしているかもしれませんが、例外を飲み込んでいる可能性があります – Plutonix

+1

あなたが送信しているものはわかりませんが、おそらくこのようなものです: '' notepad "'? '.Remove(0、4)'を使うと結果は '' notepad "'になります。何か気付く?最初はスペースがあります。あなたは 'input.Trim()'を使ってそれを取り除くことができます。 – MrPaulch

答えて

-2
Dim p() As Process 
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 

    p = Process.GetProcessesByName("taskmgr") 
    If p.Count > 0 Then 
     Dim pList() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("notepad") 
     For Each proc As System.Diagnostics.Process In pList 
      proc.Kill() 

     Next 

    End If 
End Sub 
+0

ありがとうございます。問題が解決しました。 –

+1

この回答はどういう意味ですか?タスクマネージャが実行中の場合、メモ帳のすべてのインスタンスが強制終了されます。私はこれがOPの質問にどのように答えるかは分かりません。 – CodeCaster

関連する問題