• 大小: 29KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签: C#  TCP服务器  

资源简介

C#写的TCP服务器端程序,支持多个TCP客户端连入,程序当中有一个监听进程不断监听来自客户端的TCP连接请求,请求建立后交由一个专门的处理进程来处理接收到的数据(在本程序当中只是简单的将收到的数据返回给客户端)。全部源代码。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;


namespace TCPMultilink
{
    public partial class Form1 : Form
    {
        Thread listenThread = null;//监听进程

        public Form1()
        {
            InitializeComponent();
        }
        private static void StartListening() //main listening thread
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any 5555);//绑定5555端口
            Socket newsock = new Socket(AddressFamily.InterNetworkSocketType.Stream ProtocolType.Tcp);//tcp协议
            newsock.Bind(ipep);
            newsock.Listen(10);
            while (true)
            {

                Socket client = newsock.Accept();//等待TCP客户端的链接请求

                ThreadClientProc p = new ThreadClientProc();
                p.tempSocket = client;
  
                Thread clientService = new Thread(new ThreadStart(p.ServiceClient));

                clientService.IsBackground = true;
                clientService.Start();
            }
        }

        private void Form1_FormClosing(object sender FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show(“确定要退出程序吗?“ “退出提示“ MessageBoxButtons.OKCancel);

            if (dr.Equals(DialogResult.OK))
            {
                // Application.Exit();
                if (listenThread != null)
                    listenThread.Abort();

                this.Dispose();
                this.Close();
            }
            else
            {
                e.Cancel = true;
            }  
        }

        private void Form1_Load(object sender EventArgs e)
        {
            //创建监听进程
            listenThread = new Thread(new ThreadStart(StartListening));
            listenThread.IsBackground = true;
            listenThread.Start();
        }
    }



    //客户端请求处理类
    public class ThreadClientProc
    {
        public Socket tempSocket;

        public ThreadClientProc()
        {

        }

        public void CloseThread()
        {
            Thread th = Thread.CurrentThread;
            th.Abort();
        }
     
      
        public void ServiceClient()
        {
            IPEndPoint newclient = (IPEndPoint)this.tempSocket.RemoteEndPoint;

            NetworkStream ns = new NetworkStream(tempSocket);
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            //string welcome = “Welcome to tcp server!“;
            //sw.WriteLine(welcome);
            //sw.Flush();
            sw.AutoFlush = true;

            char[] myReadBuffer = new char[1024];

            int count;
            while (true)
            {
                try
                {
                    count = 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5612  2009-09-05 21:36  TCPMultilink\Properties\Resources.resx

     文件        249  2009-09-05 21:36  TCPMultilink\Properties\Settings.settings

     文件       1196  2009-09-05 21:36  TCPMultilink\Properties\AssemblyInfo.cs

     文件       2878  2009-09-05 21:36  TCPMultilink\Properties\Resources.Designer.cs

     文件       1095  2009-09-05 21:36  TCPMultilink\Properties\Settings.Designer.cs

     文件       3231  2009-09-05 21:47  TCPMultilink\TCPMultilink.csproj

     文件       5632  2005-12-08 14:51  TCPMultilink\bin\Debug\TCPMultilink.vshost.exe

     文件      20480  2009-09-06 09:14  TCPMultilink\bin\Debug\TCPMultilink.exe

     文件      24064  2009-09-06 09:14  TCPMultilink\bin\Debug\TCPMultilink.pdb

     文件        180  2009-09-05 21:50  TCPMultilink\obj\Debug\TCPMultilink.Form1.resources

     文件        180  2009-09-05 21:47  TCPMultilink\obj\Debug\TCPMultilink.Properties.Resources.resources

     文件      20480  2009-09-06 09:14  TCPMultilink\obj\Debug\TCPMultilink.exe

     文件        842  2009-09-05 21:50  TCPMultilink\obj\Debug\TCPMultilink.csproj.GenerateResource.Cache

     文件      24064  2009-09-06 09:14  TCPMultilink\obj\Debug\TCPMultilink.pdb

     文件        303  2009-09-06 09:14  TCPMultilink\obj\TCPMultilink.csproj.FileList.txt

     文件        471  2009-09-05 21:36  TCPMultilink\Program.cs

     文件       1605  2009-09-05 21:50  TCPMultilink\Form1.Designer.cs

     文件       5814  2009-09-05 21:50  TCPMultilink\Form1.resx

     文件       3522  2009-09-06 09:14  TCPMultilink\Form1.cs

     文件        925  2009-09-05 21:36  TCPMultilink.sln

     目录          0  2009-09-05 21:36  TCPMultilink\obj\Debug\TempPE

     目录          0  2009-09-05 21:36  TCPMultilink\bin\Debug

     目录          0  2009-09-05 21:36  TCPMultilink\obj\Debug

     目录          0  2009-09-05 21:36  TCPMultilink\Properties

     目录          0  2009-09-05 21:36  TCPMultilink\bin

     目录          0  2009-09-05 21:36  TCPMultilink\obj

     目录          0  2009-09-05 21:36  TCPMultilink

----------- ---------  ---------- -----  ----

               122823                    27



............此处省略0个文件信息

评论

共有 条评论