天天看点

tcp助手类编程

tcp助手类是在socket层上建立的,可以直接通过client或server属性设置或获取。

tcp助手类编程

#region usings

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.Net.Sockets;

using System.Threading;

#endregion

namespace Server

{

    public partial class TalkServer : Form

    {

        #region Parameters

        IPEndPoint remoteIpep;

        TcpClient tcpc,tcpcb;

        TcpListener tcpl;

        Thread threadL;

        NetworkStream ns;

        delegate void AppendItem(object o);

        AppendItem AddItem;

        Socket connectSocket;

        void addItem(object o)

        {

            listBox.Items.Add(o);

        }

        #endregion

        #region Events

        public TalkServer()

            InitializeComponent();

            AddItem = new AppendItem(addItem);

        private void btServerStart_Click(object sender, EventArgs e)

            tcpl = new TcpListener(IPAddress.Any, 2008);

            try

            {

                tcpl.Start(10);

                threadL = new Thread(new ThreadStart(acceptCnns));

                threadL.IsBackground = true;

                threadL.Start();

                listBox.Items.Add("Server start success");

            }

            catch (Exception exc)

                listBox.Items.Add(exc.Message);

                throw;

        #region Methods

        private void acceptCnns()

            string welcome = "welcome to server!";

            byte[] buf = Encoding.Unicode.GetBytes(welcome);

            while (true)

                try

                {

                    tcpc = tcpl.AcceptTcpClient();

                    remoteIpep = (IPEndPoint ) tcpc.Client.RemoteEndPoint;

                    ns = tcpc.GetStream();

                    ns.Write(buf, 0, buf.Length);

                    ThreadPool.QueueUserWorkItem(new WaitCallback(receiveData));

                }

                catch (Exception)

                    throw;

        private void receiveData(object o)

            int len = tcpc.Available;

            byte[] buf = new byte[1024];

            string data = string.Empty;

                len = tcpc.Available;

                if (len != 0)

                    ns.Read(buf, 0, len);

                    data = Encoding.Unicode.GetString(buf, 0, len);

                    listBox.Invoke(AddItem, data);

        private void btConnect_Click(object sender, EventArgs e)

            tcpcb = new TcpClient();

            tcpcb.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                tcpcb.Connect(remoteIpep);

                //ns = tcpcb.GetStream(); 

                listBox.Invoke(AddItem, "Connected!");

                MessageBox.Show(exc.Message);

    }

}

tcp助手类编程

namespace Client

    public partial class TalkClient : Form

        TcpClient tcpc,tcpc2,tcpcb,tcpcr;

        Thread threadR;

        public TalkClient()

        private void btListen_Click(object sender, EventArgs e)

        private void TalkClient_Load(object sender, EventArgs e)

        private void btCnnect_Click(object sender, EventArgs e)

            string target = txtTarget.Text.Trim();

            IPAddress ipa = Dns.GetHostAddresses(target)[0];

            IPEndPoint ipep = new IPEndPoint(ipa, 2008);

            tcpc = new TcpClient();

                tcpc.Connect(ipep);

                listBox.Items.Add("connected!");

                ns = tcpc.GetStream();

                tcpcr = tcpc;

                threadR = new Thread(new ThreadStart(receiveData));

                threadR.IsBackground = true;

                threadR.Start();

                listBox.Items.Add("thread receive started!");

            tcpc2 = new TcpClient();

            tcpc2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                tcpc2.Connect(ipep);

                listBox.Items.Add("Connection 2 connected!");

        private void btSend_Click(object sender, EventArgs e)

            string data = txtMessage.Text.Trim();

                byte[] buf = Encoding.Unicode.GetBytes(data);

                ns.Write(buf, 0, buf.Length);

        void receiveData()

            int len = 0;

                len = tcpcr.Available;

                if ( len != 0)

本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2008/08/11/1265309.html,如需转载请自行联系原作者

继续阅读