c# socket通訊實現簡單的視窗資訊互相傳送 (聊天室的deom)
這次使用socket來實現簡單的視窗資訊互相傳送
首先我們建立一個伺服器端 services (winfrom檔案)
這邊注意。你的ip地址和埠號可以 命令建+r 開啟cmd 輸入ipconfig找到自己的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.Windows.Forms; using System.Net.Sockets; using System.Net;
namespace Service { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
IPAddress ip; TcpListener listener; TcpClient tcpClient = new TcpClient();
private void button2_Click(object sender, EventArgs e) { this.backgroundWorker1.RunWorkerAsync();//開始進行 但是是線上程裡run控制元件 會報錯 Control.CheckForIllegalCrossThreadCalls = false; //直接不檢查這個錯誤 前後臺同時執行 }
private void textBoxIp_TextChanged(object sender, EventArgs e) {
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { IPAddress ip = IPAddress.Parse(this.textBoxIp.Text);//建立ip TcpListener listener = new TcpListener(ip, Convert.ToInt32(this.textBoxPort.Text));//建立tcp監聽物件 listener.Start();
this.textBoxInfo.Text = "伺服器啟動-" + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text;
TcpClient tcpClient = listener.AcceptTcpClient();//中斷 等待 我們使用多執行緒 fuck媽的 this.textBoxInfo.Text = "連線成功-" + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text;
NetworkStream stream = tcpClient.GetStream();//建立管道 儲存資訊 byte[] byteArrary = new byte[1024];//因為不知道多大 所以大一點
try {
while (true)//建立死迴圈 不影響 { int length = stream.Read(byteArrary, 0, 1024);//會把這個流裡面的位元組陣列放到bytearray裡面 返回來一個長度 中斷了這個。中斷 等待 我們使用多執行緒 fuck媽的 //length其實就是客戶端傳送的位元組陣列長度 string receiveMessage = Encoding.Unicode.GetString(byteArrary, 0, length); this.textBoxInfo.Text = "接受到的:" + receiveMessage + "-" + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text; } } catch (Exception ex) {
MessageBox.Show("mistake"); } }
private void buttonSend_Click(object sender, EventArgs e) {
string message = this.textBoxInput.Text; this.textBoxInfo.Text = "傳送" + message + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text;
NetworkStream stream = tcpClient.GetStream(); byte[] byteArray = Encoding.Unicode.GetBytes(message); stream.Write(byteArray, 0, byteArray.Length);// 傳送位元組流 位元組串的位元組陣列 }
private void textBoxInfo_TextChanged(object sender, EventArgs e) {
}
} } 切記先建立winform視窗,控制元件拖完了再去看程式碼,不要直接複製黏貼,winfrom不會識別你的程式碼中的控制元件名字,這個要記住。
客戶端
這部分我還沒有寫具體的新增使用者function,所以你們在拖控制元件時候 不要把這個“新增使用者”拖進去 。
程式碼部分
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Net.Sockets; using System.ComponentModel; using System.Windows.Forms; using System.Net;
namespace Clientform { public partial class Form1 : Form {
TcpClient tcpClient = new TcpClient(); public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; }
private void buttonSend_Click(object sender, EventArgs e) {
string message = this.textBoxInput.Text; this.textBoxInfo.Text = "傳送" + message + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text;
NetworkStream stream = tcpClient.GetStream(); byte[] byteArray = Encoding.Unicode.GetBytes(message);//存在數組裡面 stream.Write(byteArray, 0, byteArray.Length);// 傳送位元組流 位元組串的位元組陣列 }
private void buttonStart_Click(object sender, EventArgs e) { tcpClient = new TcpClient();
try { tcpClient.Connect(this.textBoxIp.Text, Convert.ToInt32(this.textBoxPort.Text)); this.textBoxInfo.Text = "連線成功 " + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text; this.backgroundWorker1.RunWorkerAsync(); } catch (Exception ex) {
MessageBox.Show("連線失敗" + ex.Message); }
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)//寫接收的程式碼 { NetworkStream stream = tcpClient.GetStream(); byte[] byteArrary = new byte[1024];//因為不知道多大 所以大一點
try {
while (true)//建立死迴圈 不影響 { int length = stream.Read(byteArrary, 0, 1024);//會把這個流裡面的位元組陣列放到bytearray裡面 返回來一個長度 中斷了這個。中斷 等待 我們使用多執行緒 fuck媽的 但是多執行緒我還沒寫 只用了backgroundworker 這個你們最好自己改了 //length其實就是客戶端傳送的位元組陣列長度 string receiveMessage = Encoding.Unicode.GetString(byteArrary, 0, length);//這邊轉碼 utf8格式也可以 this.textBoxInfo.Text = "接受到的:" + receiveMessage + "-" + DateTime.Now.ToShortTimeString() + "\r\n" + this.textBoxInfo.Text; } } catch (Exception ex) {
MessageBox.Show("mistake"); }
}
private void button1_Click(object sender, EventArgs e) { Form1 addnew = new Form1();
addnew.Show(); }
private void textBoxInfo_TextChanged(object sender, EventArgs e) {
}
}
}
注意事項是整個程式我沒有寫多執行緒,用backgroundworker代替了多執行緒,自己修改。簡單的demo實現,但是隻能客戶端發伺服器,後續功能自己新增。
特別注意,不要直接黏貼複製! 看程式碼再拖控制元件,搞清楚控制元件的name再去黏貼複製程式碼!別貪方便。