//接收

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();
        }
    }
}

最新文章

  1. Atitit  自动化gui 与 发帖机 技术
  2. 介绍几种SSIS部署方式
  3. webform 光棒效果,删除操作弹出确定取消窗口
  4. hdu 1455 Sticks(dfs+剪枝)
  5. 在网页中显示CHM (c# csharp .net asp.net winform)
  6. Linux 系统中用户切换
  7. [转] DBCP 的validationQuery
  8. UBUNTU12.4 安装磊科无线网卡驱动
  9. Spring Data JPA Tutorial Part Nine: Conclusions(未翻译)
  10. C#编写 "Hello,Word!" 您的第一个程序。
  11. vmware虚拟机上linux操作系统进行tty1~tty6切换方法和具体步骤
  12. ALV双击单元格事件处理
  13. (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。
  14. 关于String的对象创建
  15. HIVE扩展GIS函数
  16. nodejs 箭头函数
  17. 金蝶PDA金蝶盘点机金蝶仓库条码管理方案-采购入库单教程
  18. eclipse + python + pydev
  19. element 表格无法绑定服务返回数据
  20. iOS中使用RegexKitLite来试用正则表达式

热门文章

  1. ITFriend创业阶段的服务器环境搭建手册
  2. [Compose] 11. Use Task for Asynchronous Actions
  3. android 之修改图片的某一颜色值
  4. Ajax方法
  5. Fragment使用LocalBroadcastManager接收广播消息
  6. sublime-1 sublime设置到鼠标右键
  7. Web前端实践经验总结
  8. Tomcat下部署SpringBoot
  9. Delphi Base64编码/解码及ZLib压缩/解压
  10. 对Java字符串的探究