博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)winform下TCP通信的简单应用
阅读量:6569 次
发布时间:2019-06-24

本文共 5078 字,大约阅读时间需要 16 分钟。

本文转载自: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();          //}    }  }

 

你可能感兴趣的文章
喜得千金,升级做爸爸喽
查看>>
Ubuntu 17 安装 tensorflow
查看>>
红外协议之NEC协议
查看>>
【转】python包导入细节
查看>>
数据结构 i_love(我喜欢)
查看>>
LIN通讯
查看>>
Cocos Creator 为Button添加事件的两种方法
查看>>
the sentiments when install labelimage
查看>>
list、dict、tuple的一些小操作总结
查看>>
UVa 10055 - Hashmat the Brave Warrior
查看>>
Two Sum
查看>>
sudo日志审计
查看>>
【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
查看>>
编程之美初赛第一场
查看>>
安卓APK瘦身
查看>>
将jsp页面转pdf
查看>>
python 字典的系列操作
查看>>
递归函数中清空静态变量
查看>>
Gdb+gdbserver无源码调试Android 动态链接库的技巧
查看>>
Windows中将javac和java两个命令集成到UltraEdit工具栏
查看>>