using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace JohnTcpServer
{
public partial class JohnTcpServer : Component
{
//变量申明
private TcpListener John_Tcp_Server = null;
private IPEndPoint ServerEndPoint = null;
private Thread th_acceptSocket=null;
//事件申明
public delegate void ReceiveEventHandler(TcpClient reTcpClient);//委托
public event ReceiveEventHandler Receive;
//构造函数
public JohnTcpServer()
{
InitializeComponent();
} public JohnTcpServer(IContainer container)
{
container.Add(this); InitializeComponent();
}
public JohnTcpServer(string ServerIP, int ServerPort)
{
InitializeComponent();
LocalHost = ServerIP;
localPort = ServerPort;
//ServerEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort);
}
//定义LocalHost属性
private string localHost = "127.0.0.1";
[Browsable(true),Category("Local"),Description("本地IP地址")]
public string LocalHost {
get { return localHost; }
set { localHost = value; }
}
//定义localport属性
private int localPort = 7956;
[Browsable(true), Category("Local"), Description("本地端口号")]
public int LocalPort {
get { return localPort; }
set { localPort = value; }
}
//定义ACTIVE属性
private bool active = false;
[Browsable(true), Category("Local"), Description("激活服务端")]
public bool Active {
get { return active; }
set {
active = value;
if (active)
{
//打开服务端
this.Open();
}
else {
//关闭服务端
this.Close();
}
}
}
//打开服务端
public void Open() {
ServerEndPoint = new IPEndPoint(IPAddress.Parse(localHost), localPort);
//判断服务端是否启动
if (John_Tcp_Server != null)
{
John_Tcp_Server.Stop();
John_Tcp_Server = null;
}
John_Tcp_Server = new TcpListener(ServerEndPoint);
John_Tcp_Server.Start();//启动监听
try
{
th_acceptSocket = new Thread(new ThreadStart(acceptTcpClient));
th_acceptSocket.Start();
}catch(Exception ex )
{
MessageBox.Show(ex.ToString());
}
} private void acceptTcpClient()
{
while (true)
{
TcpClient tempClient = John_Tcp_Server.AcceptTcpClient();
Thread th_Client = new Thread(new ParameterizedThreadStart(receive));
th_Client.Start(tempClient);
}
} private void receive(object tempClient)
{
if(Receive!=null){
Receive((TcpClient)tempClient);
}
Thread.Sleep(0);
} public void Close() {
if (John_Tcp_Server != null) {
John_Tcp_Server.Stop();
John_Tcp_Server = null;
}
if (th_acceptSocket != null) //结束线程
{
Thread.Sleep(30);
th_acceptSocket.Abort();
th_acceptSocket = null;
}
} }
}如果在 Receive 调用窗体的控件 就会报线程错误 那位大侠能指点一下 如何同步线程到UI线程?
// the text property on a TextBox control.
delegate void SetTextCallback(TextBox cTextBox, string text); /// <summary>
/// 线程安全调用修改窗体显示文本
/// </summary>
/// <param name="cTextBox"></param>
/// <param name="text"></param>
/// <param name="nState"></param>
private void SetText(TextBox cTextBox,string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (cTextBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] {cTextBox,text});
}
else
{
cTextBox.Text = text;
}
}