====TClientSocket.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;//for Thread
using System.Threading.Tasks;
using System.Net;//for IPEndPoint
using System.Net.Sockets;//for Socket
using System.Windows.Forms;//for MessageBox
namespace HelloSocket
{
public class TClientSocket
{
private Socket FSocket = null;
public String Address;
public int Port;
private Form Owner;
//----宣告委派----
public delegate void SynchronizeMethod();//萬用委派函式
public delegate void _OnRead(object sender, Socket socket);//收到訊息的委派函式
//我的收到訊息的事件
/// <summary>
/// 實做public void OnRead(object sender, Socket socket)函式,並帶入TClientSocket.OnRead。
/// </summary>
public _OnRead OnRead=null;
//執行序,收訊息用
private Thread thread = null;
//離開執行序用
private bool bStop = false;
//建構子
public TClientSocket(Form Owner)
{
//會跟著釋放
this.Owner = Owner;
if (Owner == null)
{
Owner = new Form();
}
}
//聽訊息(執行序內的程式)
private void Listent()
{
while(true)
{
//降CPU Load用
Thread.Sleep(1);
//如果有代入事件,就觸發
if (OnRead != null)
{
if (FSocket.Available != 0)
{
//使用Invoke 呼叫萬用委派函式
Synchronize(Sync_OnRead);
}
}
if (bStop) break;
if (Owner.Disposing) break;
}
//不斷線就關閉程式,伺服端會出現ErrorCode:10053
FSocket.Disconnect(true);
FSocket = null;
}
//避免代入程式有使用到可視元件,使用Invoke呼叫委派函式
void Sync_OnRead()
{
OnRead(this, FSocket);
}
//使用Invoke 呼叫萬用委派函式
public void Synchronize(SynchronizeMethod Method)
{
while (!Owner.InvokeRequired) ;
Owner.Invoke(Method);
}
//連線
public void Connect()
{
bStop = false;
try
{
FSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
FSocket.Connect(new IPEndPoint(IPAddress.Parse(Address), Port));
}
catch (SocketException e)
{
if (e.ErrorCode == 10061)
{
MessageBox.Show("伺服器未開啟");
}
else
{
MessageBox.Show("ErrorCode:" + e.ErrorCode.ToString());
}
return;
}
//連線失敗
if (!FSocket.Connected) return;
//建立執行序
thread = new Thread(Listent);//帶入聽訊息函式
thread.Start();//開始執行序
}
//連線(多載)
public void Connect(string address, int port)
{
Address = address;
Port = port;
Connect();
}
//離線
public void Disconnect()
{
bStop = true;
}
//傳送訊息
public void SendText(String Text)
{
if (FSocket == null) return;
Byte[] bytesSend = Encoding.ASCII.GetBytes(Text);
FSocket.Send(bytesSend, bytesSend.Length, 0);
}
}
}
====Form1.cs====
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.Sockets;
namespace HelloSocket
{
public partial class Form1 : Form
{
//我的Socket元件
public TClientSocket ClientSocket;
public Form1()
{
InitializeComponent();
//建立Socket元件
ClientSocket = new TClientSocket(this);//會跟著主程式釋放
ClientSocket.OnRead = ClientSocket_OnReceive;//代入我的event
}
//我的event,有收到訊息會觸發
public void ClientSocket_OnReceive(object sender, Socket socket)
{
//取得Buf Size
long Size = socket.Available;
//配置記憶體
byte[] Buf = new byte[Size];
//取得內容
socket.Receive(Buf);
//轉字串(ASCII Code 轉 UniCode)
string S = Encoding.Default.GetString(Buf);
//顯示
textBox4.AppendText(S + "\r\n");
}
private void button1_Click(object sender, EventArgs e)
{
//連線
int port = 0;
Int32.TryParse(textBox2.Text, out port);
ClientSocket.Connect(textBox1.Text, port);
}
private void button2_Click(object sender, EventArgs e)
{
//離線
ClientSocket.Disconnect();
}
private void button3_Click(object sender, EventArgs e)
{
//傳送
ClientSocket.SendText(textBox3.Text);
}
}
}
我的問卷調查
留言列表