我正在创建一个由套接字和线程管理的客户端/服务器应用程序。我想知道如何正确关闭套接字和相关线程。这是我的代码:
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
try
{
if (UserList.Count > 0)
{
foreach (User user in UserList)
{
Socket socketUser = user.getSocket();
socketUser.Send(Encoding.ASCII.GetBytes("!close"));
socketUser.Close();
}
}
thrAccept.Abort();
thrReceive.Abort();
socket.Close();
incoming.Close();
Application.Exit();
}
catch
{
MessageBox.Show("aasa");
}
}
private void connectBtn_Click(object sender, EventArgs e)
{
string port=portNum.Text;
string welcome = "Server up and running - " + System.DateTime.Now.GetDateTimeFormats()[0] + " " + DateTime.Now.ToString("HH:mm") + "\n" + "Listening Port: " + port + "\n";
socket.Bind(new IPEndPoint(IPAddress.Any, Convert.ToInt32(portNum.Text)));
socket.Listen(3);
thrAccept = new Thread(new ThreadStart(Accept));
serverLog.AppendText(welcome + "\n");
thrAccept.Start();
connectBtn.Enabled = false;
}
private void Accept()
{
while (true)
{
incoming = socket.Accept();
thrReceive = new Thread(new ThreadStart(Receive));
thrReceive.Start();
}
}
private void Receive()
{
User user = new User();//create an istance for the class User
while (true)
{
byte[] buffer = new byte[64];
incoming.Receive(buffer);
// the program continue....
如何正确关闭套接字?
类用户
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace HW2_SERVER
{
class User
{
public string username;
public Socket S;
public string role;
public bool managed;
public User()
{
username = "";
S = null;
role = "";
managed = false;
}
public void setManage(bool i)
{
managed = i;
}
public void setUsername(string u)
{
username = u;
}
public void setSocket(Socket sock)
{
S = sock;
}
public void setRole(string r)
{
role = r;
}
public bool getManaged()
{
return managed;
}
public string getUsername()
{
return username;
}
public Socket getSocket()
{
return S;
}
public string getRole()
{
return role;
}
}
}