zoukankan      html  css  js  c++  java
  • (转)winform下TCP通信的简单应用

    本文转载自:http://blog.csdn.net/wanlong360599336/article/details/7557064

    先看效果图:

    TCP比较繁琐的就是三次握手定理,每次再发送数据前都要先建立连接确认。

    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;  
    using System.Net.Sockets;  
    using System.Threading;  
    using System.IO;  
      
    namespace TCP  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                  
                InitializeComponent();  
            }  
            //启动服务端  
            TcpListener listener;  
            delegate void SetTextCallBack(string text);  
            private void button1_Click(object sender, EventArgs e)  
            {  
                //try  
                //{  
                    label2.Text = "服务端已开启..";  
                    button1.Enabled = false;  
                    listener = new TcpListener(IPAddress.Any, 3000);  
                    listener.Start();  
                    Thread th = new Thread(new ThreadStart(ReceiveMsg));  
                    th.Start();  
                    th.IsBackground = true;  
                //}  
                //catch (Exception ex)   
                //{   
                //    Console.WriteLine(ex.Message);   
                //}  
                  
            }  
              
            public void ReceiveMsg()  
            {  
      
                while (true)  
                {  
                    TcpClient client = listener.AcceptTcpClient();  
                    byte[] buffer = new byte[9899];  
                    NetworkStream stream = client.GetStream();   
                    int len = stream.Read(buffer, 0, buffer.Length);  
                    string msg = Encoding.Unicode.GetString(buffer, 0, len);  
                    SetText(msg);  
      
      
                    stream.Flush();  
                    stream.Close();  
                    client.Close();  
      
                }  
            
      
            }  
            public void SetText(string text)  
            {  
                try  
                {  
                    if (this.richTextBox1.InvokeRequired)  
                    {  
                        SetTextCallBack d = new SetTextCallBack(SetText);  
                        this.Invoke(d, new object[] { text });  
                    }  
                    else  
                    {  
      
                        this.richTextBox1.Text += DateTime.Now.ToString() + "
    " + text + "
    ";  
                    }  
      
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show(ex.Message);  
                }  
            }  
      
            private void button2_Click(object sender, EventArgs e)  
            {  
                try  
                {  
                    //FileStream fs = new FileStream(textBox2.Text,FileMode.OpenOrCreate,FileAccess.Read);  
                    //byte[] buff=new byte[fs.Length];  
                    //int rea = fs.Read(buff,0,buff.Length);  
                    string ip = textBox1.Text;  
                    string msg = richTextBox2.Text;  
                    //msg = string.Format("{0}:{1}:{2}:{3}:{4}:{5}",1,DateTime.Now.Ticks,"007","www","32",msg);  
                    TcpClient client = new TcpClient();  
                    client.Connect(IPAddress.Parse(ip), 3000);  
                    NetworkStream stream = client.GetStream();  
                    byte[] buffer = Encoding.Unicode.GetBytes(msg);  
                    stream.Write(buffer, 0, buffer.Length);  
                    //stream.Write(buff,0,rea);  
                    //label6.Text = "文件发送成功!";  
                    MessageBox.Show("发送成功!");  
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show("服务端未开启!");  
                }  
            }  
      
            private void button2_KeyDown(object sender, KeyEventArgs e)  
            {  
                if (e.KeyCode == Keys.Enter)  
                {  
                    this.button2_Click(sender,e);  
                }  
            }  
            //存放的目录  
           // private void button4_Click(object sender, EventArgs e)  
           // {  
            //    FolderBrowserDialog fbd = new FolderBrowserDialog();  
            //    if(fbd.ShowDialog()==DialogResult.OK)  
            //    {  
             //       textBox3.Text = fbd.SelectedPath;  
              //  }  
           // }  
            //发送的文件  
           /// private void button3_Click(object sender, EventArgs e)  
           //{  
           //     OpenFileDialog ofd = new OpenFileDialog();  
           //     if(ofd.ShowDialog()==DialogResult.OK)  
           //     {  
           //         textBox2.Text = ofd.FileName;  
           //     }  
           // }  
           //文件发送  
            //private void button5_Click(object sender, EventArgs e)  
            //{           
            //    FileStream fs = new FileStream(textBox2.Text,FileMode.Open,FileAccess.Read);  
            //    byte[] buffer = new byte[fs.Length];  
            //    int rea=fs.Read(buffer,0,buffer.Length);  
            //    TcpClient client = new TcpClient();  
            //    string ip = textBox1.Text;  
            //    client.Connect(IPAddress.Parse(ip),3000);  
            //    NetworkStream ns = client.GetStream();  
            //    ns.Write(buffer,0,rea);  
            //    MessageBox.Show("文件发送成功!");  
            //    fs.Flush();  
            //    ns.Flush();  
            //    fs.Close();  
            //    ns.Close();  
            //}    }  
    }  
  • 相关阅读:
    Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏
    Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
    Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏
    Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏
    Who's in the Middle 分类: POJ 2015-06-12 19:45 11人阅读 评论(0) 收藏
    IP Address 分类: POJ 2015-06-12 19:34 12人阅读 评论(0) 收藏
    Doubles 分类: POJ 2015-06-12 18:24 11人阅读 评论(0) 收藏
    The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
    Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏
    Maya Calendar 分类: POJ 2015-06-11 21:44 12人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/3919205.html
Copyright © 2011-2022 走看看