C# TCP 通讯
//接收
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TcpReceiver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Thread th = new Thread(new ThreadStart(RecvData));
th.Start();
}
private void RecvData()
{
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
int port = 10001;
TcpListener listener = new TcpListener(localAddr, port);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
StreamReader sr = new StreamReader(ns);
string result = sr.ReadToEnd();
Invoke(new UpdateDisplayDelegate(UpdateDisplay), new Object[] { result });
client.Close();
listener.Stop();
}
delegate void UpdateDisplayDelegate(string text);
void UpdateDisplay(string text)
{
txtRecv.Text = text;
}
}
}
//发送
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TcpExam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSend_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient(txtHost.Text, int.Parse(txtPort.Text));
NetworkStream ns = client.GetStream();
FileStream fs = File.Open("Form1.cs",FileMode.Open);
int t = fs.ReadByte();
while (t != -1)
{
ns.WriteByte((byte)t);
t = fs.ReadByte();
}
fs.Close();
ns.Close();
client.Close();
}
}
}
最新文章
- Atitit  自动化gui 与 发帖机 技术
- 介绍几种SSIS部署方式
- webform 光棒效果,删除操作弹出确定取消窗口
- hdu 1455 Sticks(dfs+剪枝)
- 在网页中显示CHM (c# csharp .net asp.net winform)
- Linux 系统中用户切换
- [转] DBCP 的validationQuery
- UBUNTU12.4 安装磊科无线网卡驱动
- Spring Data JPA Tutorial Part Nine: Conclusions(未翻译)
- C#编写 ";Hello,Word!"; 您的第一个程序。
- vmware虚拟机上linux操作系统进行tty1~tty6切换方法和具体步骤
- ALV双击单元格事件处理
- (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。
- 关于String的对象创建
- HIVE扩展GIS函数
- nodejs 箭头函数
- 金蝶PDA金蝶盘点机金蝶仓库条码管理方案-采购入库单教程
- eclipse + python + pydev
- element 表格无法绑定服务返回数据
- iOS中使用RegexKitLite来试用正则表达式