国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

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

2020-01-24 02:35:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

服務(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)聽(tīng)本地端口        System.Console.WriteLine("開(kāi)始監(jiān)聽(tīng)本地端口: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;      //在這里重定向輸出即可,因?yàn)椴皇墙换ナ降模绻枰换ナ降闹苯臃磸梒md即可      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 = "開(kāi)始連接服務(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ā)送來(lái)的消息    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;        }    }  }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遵义县| 房产| 资兴市| 从化市| 宣汉县| 隆回县| 新沂市| 鹤岗市| 辛集市| 蒙自县| 九江县| 宜兰市| 北川| 保山市| 祁门县| 龙岩市| 周口市| 诏安县| 灵丘县| 江津市| 黑水县| 英超| 邛崃市| 巨鹿县| 巴林右旗| 清水河县| 双流县| 五峰| 锦屏县| 兴安县| 进贤县| 桐乡市| 杭锦旗| 岱山县| 平江县| 新密市| 巨鹿县| 图木舒克市| 清远市| 泌阳县| 永宁县|