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('\0');//转换成字符串
    textBox1.Text += receiveString + "\r\n";
  }

}

 

 

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('\0');//转换成字符串
                textBox1.Text += receiveString + "\r\n";
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //接收数据

            
        }
    }
}

 

posted @ 2018-03-05 16:43  唐尧  阅读(4004)  评论(0编辑  收藏  举报