c# TcpClient簡易聊天工具
阿新 • • 發佈:2018-03-30
reg system 外網 lec text 取消 als tel ima
說明: TcpClient 鏈接是一個比較安全穩定的鏈接,作為聊天或者是數據穩定傳輸,是比較合適的,下面的代碼測試過,如果你吧他放在公網服務器上,也是可以用的
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Threading;using System.IO; using System.Net.Sockets; using System.Text.RegularExpressions; namespace ChatsTolls { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool b=true; private Thread td; private TcpListener listenter;private NetworkStream ns; private StreamWriter sw; private StreamReader sr; private Socket socket; private TcpClient client; private void Form1_Load(object sender, EventArgs e) { } private void Run() { b = false;while (true) { try { socket = listenter.AcceptSocket(); } catch (Exception) { return; } label1.Text = "已經連接"; ns = new NetworkStream(socket); sr = new StreamReader(ns); //讀取信息 sw = new StreamWriter(ns); //寫入信息 ClosingFrom(); } } private void button1_Click(object sender, EventArgs e) { if (sw != null && txtWrite.Text != "") { if (b) { sw.WriteLine("ci說:" + txtWrite.Text.Trim()); sw.Flush(); lisAdd.Items.Add("我說:" + txtWrite.Text.Trim()); txtWrite.Clear(); } else { sw.WriteLine("zhu說:" + txtWrite.Text.Trim()); sw.Flush(); lisAdd.Items.Add("我說:" + txtWrite.Text.Trim()); txtWrite.Clear(); } } } private void ClosingFrom() { try { while (1 == 1) { string line = null; if ((line = sr.ReadLine()) != null) { lisAdd.Items.Add(line); } else { label1.Text = "已經斷開"; break; } } } catch (Exception ex) { label1.Text = ""; } finally { if (td.ThreadState == ThreadState.Running) { sr.Close(); sw.Close(); ns.Close(); if (!b) { socket.Close(); } else { client.Close(); } listenter.Stop(); sw = null; td.Abort(); } } } private void btnGet_Click(object sender, EventArgs e) { if (td != null) { if (td.ThreadState == ThreadState.Running) { sr.Close(); sw.Close(); ns.Close(); if (!b) { socket.Close(); } else { client.Close(); } listenter.Stop(); sw = null; td.Abort(); } } this.Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (td != null) { if (td.ThreadState == ThreadState.Running) { sr.Close(); sw.Close(); ns.Close(); if (!b) { socket.Close(); } else { client.Close(); } listenter.Stop(); sw = null; td.Abort(); } } } private void button1_Click_1(object sender, EventArgs e) { if (btnZ.Text =="連 接") { if (string.IsNullOrEmpty(txtPort.Text)) { MessageBox.Show("請輸入端口號"); txtPort.Focus(); return; } //判斷端口號 if (string.IsNullOrEmpty(txtIp.Text)) { MessageBox.Show("請輸入Ip"); txtIp.Focus(); return; } b = true; int j = int.Parse(txtPort.Text.Trim()); string ip = txtIp.Text.Trim(); //w外網上不能用Dns //IPHostEntry ipHost = Dns.GetHostEntry(ip); try { client = new TcpClient(); client.Connect(IPAddress.Parse(ip), j); ns = this.client.GetStream(); sr = new StreamReader(ns); sw = new StreamWriter(ns); td = new Thread(new ThreadStart(Runs)); td.IsBackground = true; td.Start(); label1.Text = "已經連接"; btnZ.Text = "斷開連接"; btnX.Enabled = false; } catch (Exception ex) { label1.Text = "未找到網絡"; } } else { if (td != null) { try { sr.Close(); sw.Close(); ns.Close(); if (!b) { socket.Close(); } else { client.Close(); } sw = null; td.Abort(); } catch (Exception) { } } btnX.Enabled = true; btnZ.Text = "連 接"; label1.Text = ""; } } private void Runs() { ClosingFrom(); } private void button2_Click(object sender, EventArgs e) { if (btnX.Text == "監 聽") { if (string.IsNullOrEmpty(txtPort.Text)) { MessageBox.Show("請輸入端口號"); txtPort.Focus(); return; } //判斷端口號的方法 b = false; int j = int.Parse(txtPort.Text.Trim()); Control.CheckForIllegalCrossThreadCalls = false; listenter = new TcpListener(j); //監聽實例化 並寫入需要監聽的端口 listenter.Start(); //開始監聽 td = new Thread(new ThreadStart(Run)); //定義線程進行監聽 td.IsBackground = true; td.Start(); //線程啟動 btnX.Text = "取消監聽"; btnZ.Enabled = false; labJ.Text = "正在監聽..."; } else { try { listenter.Stop(); td.Abort(); } catch (Exception) { } btnZ.Enabled = true; btnX.Text = "監 聽"; labJ.Text = "";
} } } }
界面:
首先應在服務器上打開 輸入端口號並監聽
然後本地輸入服務Ip 和端口 點擊鏈接 就可以了
寫的比較急,就不多說了
c# TcpClient簡易聊天工具