• 网络编程02


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    
    namespace readBook
    {
        public partial class Form1 : Form
        {
            private TcpListener _lis;
            private delegate void SetStringCallback(string str);
            private SetStringCallback setStringCallback;
    
            private TcpClient _clientLocal;
    
            public Form1()
            {
                InitializeComponent();
    
                setStringCallback = new SetStringCallback(setString);
                startServer();
                startClient();
            }
    
            private void btn_ClientSendInfo_Click(object sender, EventArgs e)
            {
                SendServerInfo(clientInfo.Text);
            }
    
            //启用客户端
            private void startClient()
            {
                _clientLocal = new TcpClient();
                _clientLocal.Connect("127.0.0.1", 8180);
            }
    
            //客户端发送消息
            private void SendServerInfo(string _infomation)
            {
                NetworkStream ns = _clientLocal.GetStream();
                //byte[] data = Encoding.ASCII.GetBytes(_infomation);
                byte[] data = Encoding.BigEndianUnicode.GetBytes(_infomation);//可发送中文
                ns.Write(data, 0, data.Length);
            }
    
            //启用服务端
            private void startServer()
            {
                IPAddress localAdd = IPAddress.Parse("127.0.0.1");
                _lis = new TcpListener(localAdd, 8180);
                _lis.Start();
    
                Thread threadAccept = new Thread(AcceptClientConnect);
                threadAccept.Start();
                
               
            }
    
            //服务端接受消息,支持多客户端链接
            private void AcceptClientConnect()
            {
                while (true)
                {
                    TcpClient _client = _lis.AcceptTcpClient();
    
                    Thread threadDealMessage = new Thread(new ParameterizedThreadStart(DealMessage));//支持多客户端链接
                    threadDealMessage.Start(_client);
                } 
            }
    
            protected void DealMessage(object client)
            {
                if (client != null)
                {
                    TcpClient _client = (TcpClient)client;
                    NetworkStream _str = _client.GetStream();
                    byte[] bytAry = new byte[_client.ReceiveBufferSize];
    
                    while (true)
                    {
                        int numBytesRead = _str.Read(bytAry, 0, System.Convert.ToInt32(_client.ReceiveBufferSize));
                        //string receiveInfoFromServer = Encoding.ASCII.GetString(bytAry, 0, numBytesRead);
                        string receiveInfoFromServer = Encoding.BigEndianUnicode.GetString(bytAry, 0, numBytesRead);//可发送中文
                        displayTXT.Invoke(setStringCallback, receiveInfoFromServer);
                    }
                }
            }
    
            private void setString(string str)
            {
                displayTXT.Text = str;
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void btn_StartServer_Click(object sender, EventArgs e)
            {
                //startServer();
            }
    
            private void labe_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
  • 相关阅读:
    HDU 5521 Meeting
    HDU 5170 GTY's math problem
    HDU 5531 Rebuild
    HDU 5534 Partial Tree
    HDU 4101 Ali and Baba
    HDU 5522 Numbers
    HDU 5523 Game
    ZUFE OJ 2301 GW I (3)
    POJ 2398 Toy Storage
    POJ 2318 TOYS
  • 原文地址:https://www.cnblogs.com/softimagewht/p/4597323.html
Copyright © 2020-2023  润新知