架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 23982|回复: 1

[资料] c# socket进行通信服务端

[复制链接]
发表于 2016-1-27 13:01:30 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;

  8. namespace Remote.Client.SocketClient
  9. {
  10.     /// <summary>
  11.     /// 同步socket
  12.     /// </summary>
  13.     public class TsyncSocket
  14.     {
  15.         private static Socket serviceSocketListener;
  16.         public static bool SocketStart(string _ip, int prot)
  17.         {
  18.             try
  19.             {
  20.                 IPAddress ip = IPAddress.Parse(_ip);
  21.                 serviceSocketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  22.                 serviceSocketListener.Bind(new IPEndPoint(ip, prot));  //绑定IP地址:端口  
  23.                 serviceSocketListener.Listen(1024);    //设定最多1024个排队连接请求  
  24.                 Thread myThread = new Thread(ListenClientConnect);
  25.                 myThread.IsBackground = true;
  26.                 myThread.Start();
  27.                 return true;
  28.             }
  29.             catch (Exception ex)
  30.             {
  31.                 return false;
  32.             }
  33.         }
  34.         /// <summary>  
  35.         /// 监听客户端连接  
  36.         /// </summary>  
  37.         private static void ListenClientConnect()
  38.         {
  39.             while (true)
  40.             {
  41.                 Socket clientSocket = serviceSocketListener.Accept();
  42.                 clientSocket.Send(Encoding.UTF8.GetBytes(Remote.Seeting.Msg.success.ToString()));
  43.                 #region 连接成功
  44.                 Thread recTh = new Thread(RecMsg);  //收消息线程
  45.                 recTh.IsBackground = true;
  46.                 recTh.Start(clientSocket);
  47.                 Thread sendTh = new Thread(SendMsg);    //发消息线程
  48.                 sendTh.IsBackground = true;
  49.                 sendTh.Start(clientSocket);
  50.                 #endregion
  51.             }
  52.         }
  53.         /// <summary>
  54.         /// 接受消息线程
  55.         /// </summary>
  56.         /// <param name="o"></param>
  57.         private static void RecMsg(object o)
  58.         {
  59.             Socket connSocket = o as Socket;
  60.             try
  61.             {
  62.                 while (true)
  63.                 {
  64.                     byte[] buffer = new Byte[1048576];
  65.                     int receiveLength = connSocket.Receive(buffer);
  66.                     string str = Encoding.UTF8.GetString(buffer, 0, receiveLength); //接受到的消息
  67.                     //先解密
  68.                     string publicMsg=null;
  69.                     try
  70.                     {
  71.                         publicMsg = Remote.Common.Security.RSACryptoProvider.SectionDecrypt(str, Setting.model.SecretKey);
  72.                     }
  73.                     catch (Exception ex)
  74.                     {
  75.                         connSocket.Send(Encoding.UTF8.GetBytes("消息不正确 msg error"));
  76.                         continue;
  77.                     }
  78.                     if (!string.IsNullOrEmpty(publicMsg))
  79.                     {
  80.                         string[] temp = publicMsg.Split('#');
  81.                         if (temp[0].ToLower().Equals(Setting.model.md5))
  82.                         {
  83.                             //秘钥正确
  84.                             //connSocket.Send(Encoding.UTF8.GetBytes(temp[1]));
  85.                             string msgtemp = null;
  86.                             switch (temp[1].ToUpper())
  87.                             {
  88.                                 case "CMD":
  89.                                     msgtemp=Function.ExeCmd.Cmd(temp[2]);
  90.                                     break;
  91.                             }
  92.                             connSocket.Send(Encoding.UTF8.GetBytes(msgtemp));
  93.                         }
  94.                     }
  95.                     else
  96.                     {
  97.                         connSocket.Send(Encoding.UTF8.GetBytes("秘钥不正确"));
  98.                     }
  99.                 }
  100.             }
  101.             catch (Exception ex)
  102.             {
  103.                 if (connSocket.Connected)
  104.                 {
  105.                     connSocket.Shutdown(SocketShutdown.Both);
  106.                     connSocket.Close();
  107.                 }
  108.             }
  109.         }
  110.         /// <summary>
  111.         /// 发送消息线程
  112.         /// </summary>
  113.         /// <param name="o"></param>
  114.         private static void SendMsg(object o)
  115.         {
  116.             Socket connSocket = o as Socket;
  117.             try
  118.             {
  119.                 while (true)
  120.                 {
  121.                     if (!string.IsNullOrEmpty(Setting.Msg))
  122.                     {
  123.                         lock (Setting.Msg)
  124.                         {
  125.                             connSocket.Send(Encoding.UTF8.GetBytes(Setting.Msg));
  126.                             Setting.Msg = null;
  127.                         }
  128.                     }
  129.                 }
  130.             }
  131.             catch (Exception ex)
  132.             {
  133.                 if (connSocket.Connected)
  134.                 {
  135.                     connSocket.Shutdown(SocketShutdown.Both);
  136.                     connSocket.Close();
  137.                 }
  138.             }
  139.         }
  140.     }
  141. }
复制代码



这个是服务端




上一篇:如何保存配置好的cisco路由器配置?
下一篇:c# Bitmap图片压缩算法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2023-5-4 18:18:52 | 显示全部楼层
有没有内容?模拟websocket客户端连接
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-2-19 09:19

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表