标签:.sh datetime intern reader div protoc component length init
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}\r\n", 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(); } } }
标签:.sh datetime intern reader div protoc component length init
原文地址:http://www.cnblogs.com/mamiyiya777/p/6654447.html