(转)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, );
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[];
NetworkStream stream = client.GetStream();
int len = stream.Read(buffer, , buffer.Length);
string msg = Encoding.Unicode.GetString(buffer, , 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), );
NetworkStream stream = client.GetStream();
byte[] buffer = Encoding.Unicode.GetBytes(msg);
stream.Write(buffer, , 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通信的简单应用的更多相关文章
- (转)winform下UPD通信的简单应用
本文转载自:http://blog.csdn.net/wanlong360599336/article/details/7557046 先看效果图: 使用UDP的好处就是不需要三次握手,但是缺点就是存 ...
- java 网络编程之TCP通信和简单的文件上传功能
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- JAVA基础知识之网络编程——-基于TCP通信的简单聊天室
下面将基于TCP协议用JAVA写一个非常简单的聊天室程序, 聊天室具有以下功能, 在服务器端,可以接受客户端注册(用户名),可以显示注册成功的账户 在客户端,可以注册一个账号,并用这个账号发送信息 发 ...
- linux 服务器/客户端 tcp通信的简单例子
昨天弄了sublime之后没有弄输入中文的了,学生党来着,刚好可以练练英语(英语渣渣,还要考六级),所以注释都写英文的(语法什么的就别太深究了) 服务器端: /*start from the very ...
- 基于Tcp协议的简单Socket通信实例(JAVA)
好久没写博客了,前段时间忙于做项目,耽误了些时间,今天开始继续写起~ 今天来讲下关于Socket通信的简单应用,关于什么是Socket以及一些网络编程的基础,这里就不提了,只记录最简单易懂实用的东西. ...
- Linux下TCP网络编程与基于Windows下C#socket编程间通信
一.linux下TCP网络编程基础,需要了解相关函数 Socket():用于套接字初始化. Bind():将 socket 与本机上的一个端口绑定,就可以在该端口监听服务请求. Listen():使s ...
- C#网络编程TCP通信实例程序简单设计
C#网络编程TCP通信实例程序简单设计 采用自带 TcpClient和TcpListener设计一个Tcp通信的例子 只实现了TCP通信 通信程序截图: 压力测试服务端截图: 俩个客户端链接服务端测试 ...
- 上位机面试必备——TCP通信灵魂二十问【下】
上篇文章跟大家介绍了TCP通信常见的前10个面试题,没看过的小伙伴可以点击下方链接进行查看: 上位机面试必备——TCP通信灵魂二十问[上] 今天就后面的10个面试题接着做下说明:欢迎关注[dotNet ...
- TCP通信简单梳理
一.什么是TCP协议 TCP协议是一种面向连接的可靠的通信协议,最重要的两个特点:连接.可靠. 二.TCP是如何进行通信的 TCP通过三次握手建立连接后客户端服务端的内核都分别开辟资源,这时候开始进行 ...
随机推荐
- Linux 通过进程Pid与端口互查
ps -aux 状态详解 https://blog.csdn.net/whatday/article/details/54409387. linux下通过进程名查看其占用端口: https://www ...
- lucene学习-1 - 准备工具
需要准备的内容: jdk 1.7 eclipse Kepler lucene 4.10.2 luke 4.10.2 以及一些txt文档 其他的不需多说,只简要介绍下luke. 以下内容来自百度百科: ...
- cssrem 比例适配理解
cssrem只是帮你自动计算,省去了你在切图时,从设计稿拿到的px再根据比例转换成rem的中间过程. 这个40我无法猜测, 以前设计稿给的都是按640px(iphone5s的宽)来的,我们就按照这个比 ...
- xml、json的序列化与反序列化
xml数据 : XmlSerializer.Serialize 与 XmlSerializer.Deserialize,使用起来稍有些复杂,需要对 “实体模型” 的“对应属性” 进行 节点特 ...
- 分分享知识-快乐自己: @Component注解的使用
@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 @service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 @repositor ...
- 连接mysql报错:error 2003 (hy000):can't connect to mysql server on 'localhost' (10061)
一.mysql 的bin目录下有个MySQLInstanceConfig.exe,运行就可以进行创建数据库实例,创建实例时也可以生成windows 服务,把服务设置成自动启动就可以了 二.安装在D盘的 ...
- 如何拿到半数面试公司Offer——我的Python求职之路(转载)
从八月底开始找工作,短短的一星期多一些,面试了9家公司,拿到5份Offer,可能是因为我所面试的公司都是些创业性的公司吧,不过还是感触良多,因为学习Python的时间还很短,没想到还算比较容易的找到了 ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 交叉编译工具链介绍《Building Embedded Linux Systems》
1.前言 配置和编译一个合适的GNU工具链是相对复杂的并且需要很精细的操作,包括你需要对不同软件库之间的依赖关系.它们的各自的任务,不同软件库版本情况都有比较好的了解,编译工具链是一个乏味的工作. 2 ...
- html的meta总结,html标签中meta属性使用介绍(转)
html的meta总结,html标签中meta属性使用介绍 2014年11月5日 5928次浏览 引子 之前的我的博客中对于meta有个介绍,例如:http://www.haorooms.com/po ...