• c# tcp协议


    Control.CheckForIllegalCrossThreadCalls = false;
    
                Thread th1 = new Thread(Soct);      //启用线程
    
                th1.Start();
    
    
    public void Soct()      //开始监听
    {
      TcpClient client = null;
      NetworkStream stream = null;
      byte[] buffer = null;
      string receiveString = null;
    
      IPAddress localIP = IPAddress.Parse("127.0.0.1");
      int localPort = 9001;
      TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口实例化Listener
      listener.Start();//开始监听
    
      while (true)
      {
        client = listener.AcceptTcpClient();//接受一个Client
        buffer = new byte[client.ReceiveBufferSize];
        stream = client.GetStream();//获取网络流
        stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据
        stream.Close();//关闭流
        client.Close();//关闭Client
    
        receiveString = Encoding.Default.GetString(buffer).Trim('');//转换成字符串
        textBox1.Text += receiveString + "
    ";
      }
    
    }
    Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);    //发送数据
                IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });
                EndPoint point = new IPEndPoint(ipAddress, 8001);
                tcpClient.Connect(point);
                string message2 = "dddd";
                tcpClient.Send(Encoding.UTF8.GetBytes(message2));

    完整的

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    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 WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });
                EndPoint point = new IPEndPoint(ipAddress, 8001);
                tcpClient.Connect(point);
                string message2 = "dddd";
                tcpClient.Send(Encoding.UTF8.GetBytes(message2));
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
               
    
                Control.CheckForIllegalCrossThreadCalls = false;
    
                Thread th1 = new Thread(Soct);      //保存时,按钮变灰
    
                th1.Start();
    
        
    
    
    
            }
        
            public void Soct()
            {
                TcpClient client = null;
                NetworkStream stream = null;
                byte[] buffer = null;
                string receiveString = null;
    
                IPAddress localIP = IPAddress.Parse("127.0.0.1");
                int localPort = 9001;
                TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口实例化Listener
                listener.Start();//开始监听
    
                while (true)
                {
                    client = listener.AcceptTcpClient();//接受一个Client
                    buffer = new byte[client.ReceiveBufferSize];
                    stream = client.GetStream();//获取网络流
                    stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据
                    stream.Close();//关闭流
                    client.Close();//关闭Client
    
                    receiveString = Encoding.Default.GetString(buffer).Trim('');//转换成字符串
                    textBox1.Text += receiveString + "
    ";
                }
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //接收数据
    
                
            }
        }
    }
  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/erph/p/8509822.html
Copyright © 2020-2023  润新知