• tcp/ip客户端与服务器


    单击“发送数据”把数据发送到指定IP地址的指定端口号

    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 tcpClient
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                txtSendMssg.Text = "ceshifasong";
                Thread thrListener = new Thread(new ThreadStart(Listen));
                thrListener.Start();
            }
    
            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("127.0.0.1"), 2014);
                //tcpClient.Connect(IPAddress.Parse("192.168.1.111"), 9300);
                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();
               
            }
    
            private void Listen()
            {
                Socket listener = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(IPAddress.Any, 9300));
    
                //不断监听端口
                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();
                }
    
                //程序的listener一直不关闭
                //listener.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 button1_Click(object sender, EventArgs e)
            {
               
            }
    
        }
    }
  • 相关阅读:
    JAVA设计模式---总述篇
    Java中对象创建时的内存分配
    for循环打印空心菱形的新方法
    springcloud2.X通过actuator加载配置无效问题
    golang-错误处理
    golang-字符串
    golang-方法和接口
    golang-结构体与指针
    golang-数组、切片、映射
    golang-流程控制
  • 原文地址:https://www.cnblogs.com/zhaixing/p/6222857.html
Copyright © 2020-2023  润新知