110,534
社区成员
发帖
与我相关
我的任务
分享
public bool btnstatu = true; //开始停止服务按钮状态
public Thread myThread; //声明一个线程实例
//开始停止服务按钮
private void startService_Click(object sender, EventArgs e)
{
if (btnstatu)
{
// 实例化新线程
myThread = new Thread(new ThreadStart(Listen));
btnstatu = false;
}
else
{
try
{
newsock.Shutdown(SocketShutdown.Both);
}
catch { }
newsock.Close();
myThread.Abort();
btnstatu = true;
}
GC.Collect();//即使垃圾回收
GC.WaitForPendingFinalizers();
}
//监听函数
public void Listen()
{
//后台线程
Thread.CurrentThread.IsBackground = true;
//初始化SOCKET实例
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//初始化终结点实例
ipa = IPAddress.Any; //IPAddress.Parse(textBox1.Text.Trim());
setPort = int.Parse(serverport.Text.Trim());
localEP=new IPEndPoint(ipa,setPort);
try
{
newsock.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveSetting(1,1000,1000), null);
//绑定
newsock.Bind(localEP);
//监听
newsock.Listen(500);
//开始接受连接,异步。
while (true)
{
AcceptDone.Reset();
try
{
newsock.BeginAccept(new AsyncCallback(AcceptCallback), newsock);
}
catch
{
Thread.Sleep(1000);
newsock.BeginAccept(new AsyncCallback(AcceptCallback), newsock);
}
AcceptDone.WaitOne();
}
}
catch (Exception ex)
{
showClientMsg(ex.Message);
}
}
public void AcceptCallback(IAsyncResult ar) //accpet的回调处理函数
{
AcceptDone.Set();
server1 = (Socket)ar.AsyncState;
//将接收到的Socket传给Client,然后继续监听
Client = server1.EndAccept(ar);
Control.CheckForIllegalCrossThreadCalls = false;
DateTimeOffset now = DateTimeOffset.Now;
//将提示信息发送给客户端,并在服务端显示连接信息。
ClientIP = (Client.RemoteEndPoint as IPEndPoint).Address;//获取远程端口的ip地址
ClientPORT = (Client.RemoteEndPoint as IPEndPoint).Port;//获取远程的端口
ClientName = "IP:" + ClientIP + "Port:" + ClientPORT;
StateObject state = new StateObject();
state.workSocket = Client;
state.socketid = ClientName;
try
{
Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
catch (SocketException)
{
Client.Shutdown(SocketShutdown.Both);
Client.Close();
}
}
public void ReadCallback(IAsyncResult ar)
{
try
{
String content = String.Empty;
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(ar);
;
if (bytesRead == 0)
{
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
if (bytesRead > 0)
{
string stringdata = ""; //接收到的数据
stringdata += Encoding.GetEncoding("utf-8").GetString(state.buffer, 0, bytesRead);
//显示客户端发送过来的信息
showClientMsg("【" + state.socketid + "】发送消息" + stringdata + "时间:" + DateTime.Now.ToString() + "\r\n");
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch
{
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FlowLayoutPanel layoutPanel = new FlowLayoutPanel() { Dock = DockStyle.Fill };
layoutPanel.Controls.AddRange(new Control[] { textBoxPort, buttonStartStop, listBoxLog});
buttonStartStop.Click += button1_Click;
this.Controls.Add(layoutPanel);
}
void button1_Click(object sender, EventArgs e)
{
listening = !listening;
if (listening)
{
int port = int.Parse(textBoxPort.Text);
listenerThread = new Thread(ListenerThread) { IsBackground = true };
listenerThread.Start(port);
}
else
{
listenerThread.Join();
}
buttonStartStop.Text = listening ? "Stop" : "Start";
}
void ListenerThread(object state)
{
try
{
TcpListener listener = new TcpListener(IPAddress.Any, (int)state);
listener.Start();
Log("Listener started on " + state);
while (listening)
{
if (listener.Server.Poll(100, SelectMode.SelectRead)) // 一次最多阻塞100毫秒,以便检查listening标志。
{
Socket socket = listener.AcceptSocket();
Log("Client connected - " + socket.RemoteEndPoint);
StateObject so = new StateObject() {socket = socket };
socket.BeginReceive(so.buffer, 0, so.buffer.Length, SocketFlags.None, OnReceived, so);
}
}
listener.Stop();
Log("Listener stopped");
}
catch (Exception ex)
{
Log("Exception in ListenerThread: " + ex.Message);
}
}
void OnReceived(IAsyncResult iar)
{
try
{
StateObject so = (StateObject)iar.AsyncState;
int received = so.socket.EndReceive(iar);
if (received > 0)
{
string msg = Encoding.UTF8.GetString(so.buffer, 0, received);
so.socket.Send(Encoding.UTF8.GetBytes(DateTime.Now.ToString("s")));
Log(so.socket.RemoteEndPoint + " -> " + msg);
}
Log("Closing connection with " + so.socket.RemoteEndPoint);
so.socket.Shutdown(SocketShutdown.Both);
so.socket.Close();
}
catch (Exception ex)
{
Log("Exception in OnReceived: " + ex.Message);
}
}
void Log(string line)
{
if (this.InvokeRequired)
{
this.BeginInvoke((Action)delegate { Log(line); });
return;
}
listBoxLog.Items.Add(string.Format("[{0:s}] {1}", DateTime.Now, line));
}
TextBox textBoxPort = new TextBox() { Text = "5678" };
Button buttonStartStop = new Button() { Text = "Start" };
ListBox listBoxLog = new ListBox() { Size = new Size(400, 300) };
Thread listenerThread;
bool listening = false;
private class StateObject
{
public Socket socket;
public byte[] buffer = new byte[4 * 1024];
}
}
[/quote]
listenerThread.Join()这一句代码是什么意思啊,是关掉所有后台线程,还是只是关掉监听线程,刚接触socket,还请大神多多指教