欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)的Socket服務(wù)器端、客戶端代碼分享

 更新時間:2014年08月06日 10:44:58   投稿:junjie  
這篇文章主要介紹了C#實現(xiàn)的Socket服務(wù)器端、客戶端代碼分享,2個非常簡單的入門例子,需要的朋友可以參考下

服務(wù)端:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace Server
{
  class Program
  {
    static void Main(string[] args)
    {
      Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"),55555);
 
      try
      {
        server.Bind(point);
        server.Listen(10);
        //監(jiān)聽本地端口
        System.Console.WriteLine("開始監(jiān)聽本地端口:55555");
        while (true)
        {
          Socket sock = server.Accept();
          byte[] buffer = new byte[1024 * 1024];
          int n = sock.Receive(buffer);
          String cmd = Encoding.UTF8.GetString(buffer, 0, n);
          String result = execCmd(cmd);
          byte[] bytes = Encoding.UTF8.GetBytes(result);
          sock.Send(bytes);
        }
 
 
      }
      catch (Exception ex)
      {
        System.Console.WriteLine(ex.Message);
        return;
      }
    }
 
    //重定向輸入輸出流,并且用cmd執(zhí)行命令
    private static String execCmd(String cmd)
    {
      System.Diagnostics.Process p = new System.Diagnostics.Process();
      p.StartInfo = new System.Diagnostics.ProcessStartInfo();
      p.StartInfo.FileName = "cmd.exe";
      p.StartInfo.Arguments ="/c "+cmd;
      //隱藏程序外殼
      p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
      //在這里重定向輸出即可,因為不是交互式的,如果需要交互式的直接反彈cmd即可
      p.StartInfo.RedirectStandardOutput = true;
      p.StartInfo.UseShellExecute = false;
      p.StartInfo.CreateNoWindow = true;
      p.Start();
      return p.StandardOutput.ReadToEnd();
    }
  }
}

客戶端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
 
/*
 *  Code By iswin 
 */
 
 
namespace Client
{
  public partial class main : Form
  {
    public main()
    {
      InitializeComponent();
      this.ip.Text="127.0.0.1";
      this.cmd.Text="ipconfig";
      this.port.Text = "55555";
    }
     
 
    private void send_Click(object sender, EventArgs e)
    {
      Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      String remoteip=this.ip.Text;
      String command = this.cmd.Text;
      IPAddress ip = IPAddress.Parse(remoteip);
      IPEndPoint point=new IPEndPoint(ip,int.Parse(port.Text));
      try
      {
        this.recvmsg.Text = "開始連接服務(wù)端:" + remoteip + ":" + port.Text + "\n";
        client.Connect(point);
        this.recvmsg.Text="連接服務(wù)端!\n給服務(wù)端發(fā)送指令:"+command;
        byte[] buffer = Encoding.UTF8.GetBytes(command);
 
        //講輸入的指令發(fā)送到服務(wù)端
        client.Send(buffer);
 
        //接受服務(wù)端返回的數(shù)據(jù)
        recvmsgs(client);
 
        client.Close();
 
      }
      catch (Exception ex)
      {
        this.recvmsg.Text = ex.Message;
        MessageBox.Show(ex.Message);
        return;
      }
    }
 
    //接受服務(wù)端發(fā)送來的消息
    private void recvmsgs(Socket client)
    {
        try
        {
          byte[] buffer = new byte[1024 * 1024];
          int size = client.Receive(buffer);
          String recv = Encoding.UTF8.GetString(buffer, 0, size);
          this.recvmsg.Text = "\n" + recv;
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message);
          return;
        }
    }
  }
}

相關(guān)文章

  • 基于WPF實現(xiàn)IP輸入控件

    基于WPF實現(xiàn)IP輸入控件

    這篇文章主要介紹了如何基于WPF實現(xiàn)簡單的IP輸入控件,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下
    2023-08-08
  • C#實現(xiàn)由四周向中心縮小的窗體退出特效

    C#實現(xiàn)由四周向中心縮小的窗體退出特效

    這篇文章主要介紹了C#實現(xiàn)由四周向中心縮小的窗體退出特效,通過簡單的C#窗口調(diào)用參數(shù)設(shè)置實現(xiàn)該退出特效功能,非常簡單實用,需要的朋友可以參考下
    2015-08-08
  • C#溫故而知新系列教程之閉包

    C#溫故而知新系列教程之閉包

    閉包是將一些執(zhí)行語句的封裝,可以將封裝的結(jié)果像對象一樣傳遞,在傳遞時,這個封裝依然能夠訪問到原上下文。下面這篇文章主要給大家介紹了關(guān)于C#中閉包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • C#多線程之線程池ThreadPool用法

    C#多線程之線程池ThreadPool用法

    這篇文章介紹了C#多線程之線程池ThreadPool的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#中調(diào)用DLL時未能加載文件或程序集錯誤的處理方法(詳解)

    C#中調(diào)用DLL時未能加載文件或程序集錯誤的處理方法(詳解)

    下面小編就為大家?guī)硪黄狢#中調(diào)用DLL時未能加載文件或程序集錯誤的處理方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • C#向無窗口的進(jìn)程發(fā)送消息

    C#向無窗口的進(jìn)程發(fā)送消息

    這篇文章主要介紹了C#向無窗口的進(jìn)程發(fā)送消息 的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • 基于WPF實現(xiàn)用戶頭像選擇器的示例代碼

    基于WPF實現(xiàn)用戶頭像選擇器的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何基于WPF實現(xiàn)用戶頭像選擇器,文中的示例代碼簡潔易懂,對我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下
    2022-07-07
  • 用C#編寫ActiveX控件(一)

    用C#編寫ActiveX控件(一)

    用C#編寫ActiveX控件(一)...
    2007-03-03
  • C#之Socket操作類實例解析

    C#之Socket操作類實例解析

    這篇文章主要介紹了C#的Socket操作類用法,需要的朋友可以參考下
    2014-08-08
  • WinForm通過操作注冊表實現(xiàn)限制軟件使用次數(shù)的方法

    WinForm通過操作注冊表實現(xiàn)限制軟件使用次數(shù)的方法

    這篇文章主要介紹了WinForm通過操作注冊表實現(xiàn)限制軟件使用次數(shù)的方法,結(jié)合實例形式分析了WinForm操作注冊表的原理、步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2017-06-06

最新評論