• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#同步方式实现SocketServer

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Reflection;

namespace ConsoleApplication1
{
    public class Program
    {
        Thread[] SocketThreadList = new Thread[100];

        static Socket TCPListen = null;

        static void Main(string[] args)
        {
            TCPListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            IPEndPoint LocalPort = new IPEndPoint(IPAddress.Any, 55000);

            TCPListen.Bind(LocalPort); TCPListen.Listen(100);

            Thread AcceptThread = new Thread(new ThreadStart(AcceptWorkThread));

            AcceptThread.Start();

            Console.WriteLine("****** 软创通信服务器程序 ******");

            Console.WriteLine(string.Format("开始监听 <{0}>", LocalPort));

            Console.WriteLine("单击回车键退出SOCKET");

            Console.ReadLine(); GC.Collect(); GC.WaitForPendingFinalizers();
        }

        // 接收连接线程
        [STAThread]
        static void AcceptWorkThread()
        {
            // 指示为后台线程
            Thread.CurrentThread.IsBackground = true;

            while (true)
            {
                // 为新建连接创建新的Socket实例
                Socket s_Accept = TCPListen.Accept();

                IPEndPoint remoteAddress = (IPEndPoint)s_Accept.RemoteEndPoint;

                Console.WriteLine(string.Format("接收到来自 {0} 的连接", remoteAddress));

                // 接收数据专用线程

                Thread ReceiveThread = new Thread(new ParameterizedThreadStart(ReceiveWorkThread));

                ReceiveThread.Start(s_Accept);

                Thread.Sleep(100);
            }
        }

        static void ReceiveWorkThread(object obj)
        {
            Thread.CurrentThread.IsBackground = true;   // 标识后台线程

            Socket s_Receive = (Socket)obj;

            byte[] buffer = new byte[1024];     // 创建接收缓冲

            IPEndPoint remoteAddress = (IPEndPoint)s_Receive.RemoteEndPoint;

            try
            {
                while (s_Receive.Connected)
                {
                    if (s_Receive.Connected)
                    {

                        int ReceiveCount = s_Receive.Receive(buffer);

                        Console.WriteLine(string.Format("{0}:{1}", remoteAddress, Encoding.Default.GetString(buffer)));

                        // 返回接收成功数据
                        s_Receive.Send(Encoding.Default.GetBytes("AAA"));

                        Thread.Sleep(100);
                    }
                    else { s_Receive.Close(); break; }
                }
            }
            catch { Console.WriteLine(string.Format("{0} 断开连接", remoteAddress)); }
        }
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
在C#环境中动态调用IronPython脚本(二)发布时间:2022-07-13
下一篇:
C#实现接口xml序列化与反序列化发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap