zoukankan      html  css  js  c++  java
  • .NET简陋服务器

    服务器是干啥子用滴?监听客户端,响应客户端用滴!

    虽然是世界上最简陋的服务器,但是也有几个注意点:

    1.因为要一个死循环监听客户端响应,也就是说无法和用户进行交互了,所以防止前台假死就要为监听新起一个线程;

    2.要把新起的线程td.IsBackground = true;设置为后台线程,这样的话线程才会随着应用程序的关闭而关闭,不然的话关了窗体,它还在运行;

    3.跨线程问题,在新的线程里改变UI会报跨线程改变UI的错误,所以要用Invoke;

    4.在监听的死循环中加入  Thread.Sleep(1);这样可以提高应用程序性能,不要担心当服务的线程正在Sleep的时候来了个请求服务器会收不到,其实是收得到的。

     

    加入Thread.Sleep(1)的CPU如图:

    不加Thread.Sleep(1)的CPU如图:

    所以一定要加!

    所以服务器端代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 using System.Threading;
    11 using System.Net;
    12 using System.Net.Sockets;
    13 
    14 
    15 namespace Server
    16 {
    17     public partial class Form1 : Form
    18     {
    19         public Form1()
    20         {
    21             InitializeComponent();
    22         }
    23 
    24         private void button1_Click(object sender, EventArgs e)
    25         {
    26             Thread td = new Thread(Listen);
    27             td.IsBackground = true;
    28             td.Start();
    29             this.button1.Enabled = false;
    30             MessageBox.Show("服务器成功开启");
    31 
    32         }
    33         public delegate void ChangeRickBoxHandler(RichTextBox rtb,string str);
    34         public void ChangeRichTextBox(RichTextBox rtb,string str)
    35         {
    36             rtb.AppendText(str);
    37         }
    38         private void Listen()
    39         {
    40             IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[0];
    41             int port = Int32.Parse(this.textBox1.Text.Trim());
    42             TcpListener tl = new TcpListener(ip, port);
    43             tl.Start();
    44             while(true)
    45             {
    46                 if (tl.Pending())
    47                 {
    48                     Socket newSocket = tl.AcceptSocket();                    
    49                     byte[] buff = new byte[9];
    50                     int length=   newSocket.Receive(buff);
    51                     string command = Encoding.Default.GetString(buff);
    52                     if(command == "conServer")                  
    53                     {
    54                         Invoke(new ChangeRickBoxHandler(ChangeRichTextBox),this.richTextBox1,"有客户端连接了我");
    55                     }
    56                 }
    57               Thread.Sleep(1);
    58             }
    59         }
    60     }
    61 }
    62 

     

    客户端代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 using System.Net;
    11 using System.Net.Sockets;
    12 using System.Threading;
    13 
    14 namespace Client
    15 {
    16     public partial class Form1 : Form
    17     {
    18         public Form1()
    19         {
    20             InitializeComponent();
    21         }
    22 
    23         private void button1_Click(object sender, EventArgs e)
    24         {
    25          TcpClient tcpClient = new TcpClient();
    26          tcpClient.Connect(  Dns.GetHostAddresses(Dns.GetHostName())[0], Convert.ToInt32(this.textBox1.Text.Trim().ToString()));
    27          NetworkStream   nwStream = tcpClient.GetStream();
    28          string cmd = "conServer" ;
    29          Byte[] bytes = Encoding.Default.GetBytes(cmd.ToCharArray());
    30          nwStream.Write(bytes, 0, bytes.Length);         
    31         }
    32     }
    33 }
    34 

     

    源码下载===>/Files/zhanglei644213943/世界上最简陋的服务器.rar

     

  • 相关阅读:
    1027 Colors in Mars (20 分)
    1025 PAT Ranking (25 分)
    1024 Palindromic Number (25 分)
    JVM调优之jstack找出最耗cpu的线程并定位代码
    kill -3 获取threaddump信息
    java单例支持高并发
    yum出现的“UnicodeDecodeError: 'ascii' codec”问题解决
    命令行参数
    vyatta常用操作
    mysql5.8安装指南
  • 原文地址:https://www.cnblogs.com/iamzhanglei/p/1642411.html
Copyright © 2011-2022 走看看