本文转载自: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() + "\n" + text + "\n"; } } 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(); //} } }
(转)winform下TCP通信的简单应用
时间: 2024-11-09 00:42:16