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

 更新时间:2014年08月06日 10:44:58   投稿:junjie  
这篇文章主要介绍了C#实现的Socket服务器端、客户端代码分享,2个非常简单的入门例子,需要的朋友可以参考下

服务端:

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, 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执行命令
    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 = "开始连接服务端:" + 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, 0, size);
          this.recvmsg.Text = "\n" + recv;
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message);
          return;
        }
    }
  }
}

相关文章

  • 基于WPF实现IP输入控件

    基于WPF实现IP输入控件

    这篇文章主要介绍了如何基于WPF实现简单的IP输入控件,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的小伙伴可以参考一下
    2023-08-08
  • C#实现由四周向中心缩小的窗体退出特效

    C#实现由四周向中心缩小的窗体退出特效

    这篇文章主要介绍了C#实现由四周向中心缩小的窗体退出特效,通过简单的C#窗口调用参数设置实现该退出特效功能,非常简单实用,需要的朋友可以参考下
    2015-08-08
  • C#温故而知新系列教程之闭包

    C#温故而知新系列教程之闭包

    闭包是将一些执行语句的封装,可以将封装的结果像对象一样传递,在传递时,这个封装依然能够访问到原上下文。下面这篇文章主要给大家介绍了关于C#中闭包的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-05-05
  • C#多线程之线程池ThreadPool用法

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

    这篇文章介绍了C#多线程之线程池ThreadPool的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#中调用DLL时未能加载文件或程序集错误的处理方法(详解)

    C#中调用DLL时未能加载文件或程序集错误的处理方法(详解)

    下面小编就为大家带来一篇C#中调用DLL时未能加载文件或程序集错误的处理方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • C#向无窗口的进程发送消息

    C#向无窗口的进程发送消息

    这篇文章主要介绍了C#向无窗口的进程发送消息 的相关资料,需要的朋友可以参考下
    2016-05-05
  • 基于WPF实现用户头像选择器的示例代码

    基于WPF实现用户头像选择器的示例代码

    这篇文章主要为大家详细介绍了如何基于WPF实现用户头像选择器,文中的示例代码简洁易懂,对我们学习WPF有一定帮助,感兴趣的可以了解一下
    2022-07-07
  • 用C#编写ActiveX控件(一)

    用C#编写ActiveX控件(一)

    用C#编写ActiveX控件(一)...
    2007-03-03
  • C#之Socket操作类实例解析

    C#之Socket操作类实例解析

    这篇文章主要介绍了C#的Socket操作类用法,需要的朋友可以参考下
    2014-08-08
  • WinForm通过操作注册表实现限制软件使用次数的方法

    WinForm通过操作注册表实现限制软件使用次数的方法

    这篇文章主要介绍了WinForm通过操作注册表实现限制软件使用次数的方法,结合实例形式分析了WinForm操作注册表的原理、步骤与相关实现技巧,需要的朋友可以参考下
    2017-06-06

最新评论