• 同一网段的两台电脑通信(转)


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;
    
    namespace TCPIPtest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //初始化控件
                txtSendMssg.Text = "测试数据";
                //打开Listerner开始监听
                Thread thrListener = new Thread(new ThreadStart(Listen));
               thrListener.Start();  
    
    
            }
            //监听数据
            private void Listen()
            {
                int port = 8000;
                string host = "";
    
                Socket listener = new Socket(AddressFamily .InterNetwork ,
                    SocketType .Stream ,ProtocolType.Tcp);
                listener.Bind(new IPEndPoint (IPAddress .Any ,8000));
                //不断监听
                while(true )
                {
                    listener.Listen(0);
                    Socket socket = listener.Accept();
                    NetworkStream ntwStream = new NetworkStream(socket);
                    StreamReader strmReader = new StreamReader(ntwStream );
                    Invoke(new PrintRecvMssgDelegate(PrintRecvMssg),   
                    new object[] { strmReader.ReadToEnd() });  
                    socket.Close(); 
    
                }
    
                    
            }
            //线程内向文本框txtRecvMssg中添加字符串及委托  
           private delegate void PrintRecvMssgDelegate(string s);  
            private void PrintRecvMssg(string info)  
            {  
                txtRecvMssg.Text += string.Format("[{0}]:{1}
    ",   
                    DateTime.Now.ToLongTimeString(), info);  
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Environment.Exit(0);
            }
    
            private void btnSend_Click(object sender, EventArgs e)
            {
                TcpClient tcpClient = new TcpClient();  
               //tcpClient.Connect(IPAddress.Parse("170.0.0.78"), 2014);  
                tcpClient.Connect(IPAddress.Parse("192.168.1.105"), 8000);  
       
                NetworkStream ntwStream = tcpClient.GetStream();  
               if (ntwStream.CanWrite)  
                {  
                   Byte[] bytSend = Encoding.UTF8.GetBytes(txtSendMssg.Text);  
                    ntwStream.Write(bytSend, 0, bytSend.Length);  
                }  
               else  
                {  
                    MessageBox.Show("无法写入数据流");  
       
                    ntwStream.Close();  
                    tcpClient.Close();  
       
                    return;  
                }  
       
                ntwStream.Close();  
               tcpClient.Close();  
    
            }  
    
        }
    }
  • 相关阅读:
    Leetcode 242.有效的字母异位词 By Python
    Leetcode 344.反转字符串 By Python
    Leetcode 217.存在重复元素 By Python
    js 动态加载select触发事件
    MUI 里js动态添加数字输入框后,增加、减少按钮无效
    【 jquery 】常用
    MySql 常用语句
    CSS 选择器 知识点
    HTML 符号实体
    log4net 配置
  • 原文地址:https://www.cnblogs.com/mamiyiya777/p/6654447.html
Copyright © 2020-2023  润新知