我一直在使用C#开发聊天客户端。我试图找出一种在断开与服务器的连接时暂停/停止线程的方法。现在,当我单击断开连接时,它应该向服务器发送一条{quit}消息,但是此后,由于接收线程仍在运行,客户端仅被锁定,试图在断开连接时从服务器接收数据。有人知道解决这个问题的好方法吗?
代码如下:
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;
using System.Net.Sockets;
using System.Threading;
namespace sockjtest
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
// send from sendbox
private void button1_Click(object sender, EventArgs e)
{
byte[] outStream = System.Text.Encoding.UTF8.GetBytes(sendbox.Text);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
sendbox.Clear();
}
private void recievebox_TextChanged(object sender, EventArgs e)
{
}
// send username to server once connected
private void sendusr()
{
byte[] userStream = System.Text.Encoding.UTF8.GetBytes(usernamebox.Text);
serverStream.Write(userStream, 0, userStream.Length);
serverStream.Flush();
}
// connect button
private void button2_Click(object sender, EventArgs e)
{
Thread ctThread = new Thread(recievestream);
if (conntectbutton.Text.Contains("Connect") == true)
{
string srvip = serverip.Text;
int srvport = Convert.ToInt32(serverport.Text);
try
{
clientSocket.Connect(srvip, srvport);
if (clientSocket.Connected)
{
// change button state
conntectbutton.Text = "Disconnect";
recievebox.Clear();
recievebox.AppendText("*** Connected to " + srvip + ":" + srvport + " ***\r\n");
// start recieve thread
serverStream = clientSocket.GetStream();
ctThread.Start();
// call send user function
sendusr();
}
}
catch (System.Net.Sockets.SocketException)
{
recievebox.Clear();
recievebox.AppendText("*** Could not connected to " + srvip + ":" + srvport + " ***");
}
} else
{
string quitmsg = "{quit}";
byte[] quitstream = System.Text.Encoding.UTF8.GetBytes(quitmsg);
serverStream.Write(quitstream, 0, quitstream.Length);
serverStream.Flush();
Thread.Sleep(3000);
ctThread.Abort();
conntectbutton.Text ="Connect";
}
}
private void serverip_TextChanged(object sender, EventArgs e)
{
}
private void serverport_TextChanged(object sender, EventArgs e)
{
}
// recieve
private void recievestream()
{
while (true)
{
NetworkStream serverStream = clientSocket.GetStream();
int buffSize = 0;
byte[] inStream = new byte[1024];
int bytesRead = serverStream.Read(inStream, 0, inStream.Length);
buffSize = clientSocket.ReceiveBufferSize;
string returndata = System.Text.Encoding.UTF8.GetString(inStream, 0, bytesRead);
readData = "" + returndata;
msg();
}
}
// print message to recievebox
private void msg()
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(msg));
else
recievebox.Text = recievebox.Text + Environment.NewLine + readData;
}
private void usernamebox_TextChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
将while(true)
更改为while(clientSocket.Connected)
,然后,当远程服务器断开连接时,您的循环将自动中断;
答案 1 :(得分:0)
我设法通过使服务器将{quit}消息发送回客户端来对其进行修复。收到后,它将关闭套接字,然后重新初始化,从而可以重新连接回服务器。