网络编程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)
        {

        }
    }
}

 

posted @ 2015-06-24 11:52  softimagewht  阅读(305)  评论(0编辑  收藏  举报