加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#实现的Socket服务器端、客户端代码分享

发布时间:2020-12-15 05:43:47 所属栏目:百科 来源:网络整理
导读:服务端: 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,So

服务端:

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);
        //监听本地端口
        System.Console.WriteLine("开始监听本地端口:55555");
        while (true)
        {
          Socket sock = server.Accept();
          byte[] buffer = new byte[1024 * 1024];
          int n = sock.Receive(buffer);
          String cmd = Encoding.UTF8.GetString(buffer,n);
          String result = execCmd(cmd);
          byte[] bytes = Encoding.UTF8.GetBytes(result);
          sock.Send(bytes);
        }
 
 
      }
      catch (Exception ex)
      {
        System.Console.WriteLine(ex.Message);
        return;
      }
    }
 
    //重定向输入输出流,并且用cmd执行命令
    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,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 = "开始连接服务端:" + remoteip + ":" + port.Text + "n";
        client.Connect(point);
        this.recvmsg.Text="连接服务端!n给服务端发送指令:"+command;
        byte[] buffer = Encoding.UTF8.GetBytes(command);
 
        //讲输入的指令发送到服务端
        client.Send(buffer);
 
        //接受服务端返回的数据
        recvmsgs(client);
 
        client.Close();
 
      }
      catch (Exception ex)
      {
        this.recvmsg.Text = ex.Message;
        MessageBox.Show(ex.Message);
        return;
      }
    }
 
    //接受服务端发送来的消息
    private void recvmsgs(Socket client)
    {
        try
        {
          byte[] buffer = new byte[1024 * 1024];
          int size = client.Receive(buffer);
          String recv = Encoding.UTF8.GetString(buffer,size);
          this.recvmsg.Text = "n" + recv;
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message);
          return;
        }
    }
  }
}

您可能感兴趣的文章:

  • C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
  • C#在Unity游戏开发中进行多线程编程的方法
  • C#使用Socket发送和接收TCP数据实例
  • c#(Socket)异步套接字代码示例
  • c#(Socket)同步套接字代码示例
  • C#中Socket通信用法实例详解
  • C#实现Socket通信的解决方法
  • C# Socket网络编程实例
  • C#之Socket操作类实例解析
  • C#中Socket与Unity相结合示例代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读