一)客户端开发

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace mys
{
    public partial class Form1 : Form
    {
        public delegate void deleg(string netmessage);//声明一个委托
        public Form1()
        {
           
            InitializeComponent();
        }
        public void setText(string netmessage)
        {
           
            richTextBox1.AppendText(netmessage);

}
        private void receive() {     //网络中接受数据
            try
            {
                while (check)
                {
                    Byte[] rec = new Byte[64];
                    NetworkStream netstream = new NetworkStream(connectsock);
                    netstream.Read(rec, 0, rec.Length);
                    string recmessage = System.Text.Encoding.BigEndianUnicode.GetString(rec);
                   // MessageBox.Show(recmessage);
                    Invoke(new deleg(setText), recmessage);
                }
            }
            catch (Exception ee) { MessageBox.Show(ee.Message); }
}
        private void button1_Click(object sender, EventArgs e)  //获得连接,并启动线程
        {
            try
            {
                myip = IPAddress.Parse(textBox1.Text);
            }
            catch { MessageBox.Show("ip error"); }
            try {
                myserver = new IPEndPoint(myip,Int32.Parse(textBox2.Text));
                connectsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                connectsock.Connect(myserver);
                textBox3.AppendText("success");
                Thread thread = new Thread(new ThreadStart(receive));
                thread.Start();

}
            catch (Exception ee) { MessageBox.Show(ee.Message); }
        }

private void button3_Click(object sender, EventArgs e)    //关闭连接
        {
            try {
                connectsock.Close();
                textBox3.AppendText("stop");
            }
            catch { MessageBox.Show("invalid connect"); }
           
        }

private void button2_Click(object sender, EventArgs e)   //发送信息
        {
            try
            {
                Byte[] sendbyte = new Byte[64];
                string send = richTextBox2.Text + "/r/n";
                NetworkStream netstream = new NetworkStream(connectsock);
                sendbyte = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
                netstream.Write(sendbyte, 0, sendbyte.Length);
                netstream.Flush();
            }
            catch { MessageBox.Show("invalid connect"); }

}
      
    }
}

二)服务器开发

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace myc
{
    public partial class Form1 : Form
    {
        private IPAddress myip = IPAddress.Parse("127.0.0.1");//ip
        private IPEndPoint myserver; //ip+port
        private Socket sock; //socket
        private bool check = true;
        private Socket accsock;
        public delegate void deleg(string netmessage);//声明一个委托
        public delegate void deleg2();//声明一个委托
        public Form1()
        {
            InitializeComponent();
        }
        public void setText(string netmessage)   //deleg委托触发的事件
        {
            richTextBox1.AppendText(netmessage + "/r/n");

}
        public void setText2() {                //deleg2委托触发的事件
            textBox3.AppendText("ip:"+textBox1.Text+"port:"+textBox2.Text);
        }
         private void accp() {                //线程调用的方法,包括接收、监听触发;
             try
             {
                 myserver = new IPEndPoint(myip, Int32.Parse(textBox2.Text));
                 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//socket定义
                 sock.Bind(myserver);//socket绑定服务器
                 sock.Listen(50);//最大连接数

button1.BeginInvoke(new deleg2(setText2));//监听按钮触发事件
                
                 while (true)
                 {
                     accsock = sock.Accept();
                     if (accsock.Connected)
                     {
                         //textBox3.AppendText("build  connect ");
                         while (check)              //从网络中接受数据
                         {
                             Byte[] rec = new Byte[64];
                             NetworkStream netstream = new NetworkStream(accsock);
                             netstream.Read(rec, 0, rec.Length);
                             string recmessage = System.Text.Encoding.BigEndianUnicode.GetString(rec);
                             Invoke(new deleg(setText), recmessage);
                            
                         }
                     }
                 }
             }
             catch(Exception e){MessageBox.Show(e.Message);}
                 }
        private void button1_Click(object sender, EventArgs e)
        {
          
            try
            {
                myip = IPAddress.Parse(textBox1.Text);
            }
            catch { MessageBox.Show("ip error");}
            try {
                Thread thread = new Thread(new ThreadStart(accp));//开启线程
                thread.Start();
            }
            catch(Exception ee){textBox3.AppendText(ee.Message);}
        }

private void button2_Click(object sender, EventArgs e)   //发送数据
        {
            try {
            Byte[] sendbyte=new Byte[64];
            string send = richTextBox2.Text + "/r/n";
            NetworkStream netstream = new NetworkStream(accsock);
            sendbyte = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
            netstream.Write(sendbyte,0,sendbyte.Length);
        }
        catch { MessageBox.Show("not build connect"); }
        }

private void button3_Click(object sender, EventArgs e)//停止连接
        {
            try { sock.Close();
            textBox3.AppendText("stop");

}
        catch { MessageBox.Show("invalid stop"); };
        }

}
}

转载于:https://www.cnblogs.com/sunxiangfu/archive/2008/12/03/4261139.html

sever client相关推荐

  1. C# TCP sever client

    TCP协议是一个基本的网络协议,基本上所有的网络服务都是基于TCP协议的,如HTTP,FTP等等,所以要了解网络编程就必须了解基于TCP协议的编程.然而TCP协议是一个庞杂的体系,要彻底的弄清楚它的实 ...

  2. OPC UA Sever+Client下载教程

    登录Home - Unified Automation (unified-automation.com)​​​​​​ 自动化统一网站(此网站注册登陆时需要链接VPN) 点击DOWLAND如下图红框所示 ...

  3. Memcached、Redis OR Tair

    为什么80%的码农都做不了架构师?>>>    一.前言 非关系型数据库(NoSQL = Not Only SQL)的产品非常多,常见的有Memcached.Redis.MongoD ...

  4. python自动化测试xpath_Selenium自动化测试实例基于python

    一.Selenium介绍 Selenium是一个Web开源自动化测试框架,具有页面级操作.模拟用户真实操作.API从系统层面触发事件等特点. 1.版本 Selenium 1.0  Sever/Clie ...

  5. 网络编程—网络基础概览、socket,TCP/UDP协议

    网络基础概览 socket概览 socket模块-TCP/UDP的实现 TCP/UDP总结 网络基础概览 osi七层协议各层主要的协议 # 物理层传输电信号1010101010 # 数据链路层,以太网 ...

  6. 大熊君大话NodeJS之------Net模块

    一,开篇分析 从今天开始,我们来深入具体的模块学习,这篇文章是这个系列(大熊君大话NodeJS)文章的第三篇,前两篇主要是以理论为主,相信大家在前两篇的学习中, 对NodeJS也有一个基本的认识,没事 ...

  7. rac节点时间同步方法

    author:skate time:2010-05-06 在rac环境中,会要求各个节点之间的时间差不能超时.一般如果超过30秒,节点很可能会重启. 所以要同步各节点的时间.例如,我们需要配置一个nt ...

  8. 某商品交易所期货大厦时间同步系统

    一.时间同步的重要性 将各种通信设备或计算机设备的时间信息(年月日时分秒)基于UTC(协调世界时)时间偏差限定在足够小的范围内(如100ms),这种同步过程叫做时间同步.电脑.交换机等设备时间不准是人 ...

  9. 蓝牙Mesh 灯控案例

    1.开发芯片:低功耗蓝牙芯片PHY6212 2.软件开发套件:低功耗蓝牙芯片PHY6212提供的 3.开发套件特点:以YOC平台为基础,对蓝牙MESH协议栈做了深度优化和整合,为开发者提供了通用的ME ...

  10. 交换机的 VTP sever 与 client设置

    如果有3台交换机,其中一台为VTP SEVER  另外2台为VTP CLIENT  设置如下: 服务端 进入VLAN  database VLAN  号        创建VLAN 号 VTP   s ...

最新文章

  1. mod php是什么意思,mod函数是什么意思
  2. es重建字段类型_关于elasticsearch中更新数据的几种方式
  3. 雨中赶班车 2019-12-19
  4. C++小项目-吃豆子游戏
  5. jQuery 3D圆盘旋转焦点图 支持鼠标滚轮
  6. AcWing1083. Windy数(数位DP)题解
  7. CentOS6.5 webserver---网络配置
  8. 【压缩感知OMP算法】OMP算法的Matlab版本
  9. docker部署springboot父子级项目+mysql+redis菜鸟教程
  10. 阿里云5大基础产品简介(ECS、RDS、OSS、SLB、CDN)
  11. Python-XML文件读取/添加/修改方法记录(一)
  12. 轻量锁volatile
  13. 2021-06-16 srm平台电子化采购的优势
  14. 5000元档投影仪挑选指南,当贝F3与极米H3两款旗舰级投影到底怎么选?
  15. 实现对光网络的监控和光路切换 - MEMS 光开关
  16. 付费获取密码平台原理
  17. Linux terminal支持中文设置
  18. CobaltStrike 部署
  19. 【转】Java8 函数式编程详解
  20. Safe.Software.FME.Desktop/Server..v2017.0.17291+DNV Phast Safeti v6.51 DNV Technical公司的安全计算软件

热门文章

  1. 【Flutter】基础组件【03】Scaffold
  2. vs2013 命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”。是否缺少程序集引用?...
  3. Nginx负载均衡与健康检查
  4. [实变函数]2.2 聚点 (cluster point), 内点 (interior point), 界点 (boundary point)
  5. nginx 优化配置
  6. HTTP Get与Post的区别
  7. poj 1001 Exponentiation java解决!!!
  8. 适用于苹果Mac的 5 个 SSH 客户端软件
  9. 斯坦福与苹果基于Apple Watch检测心率异常,0.5%人群被检出,其中84%患有房颤...
  10. canvas 绘制贪吃蛇游戏 1