我没有很好地教网络。我知道TCP / IP的基础知识,但我面临着一项任务,我必须为多个项目管理多个线程,这些项目在同一个解决方案中相互通信。我正在使用Window的Forms,C#,为了简单起见,我只会写下需要编写的内容。
这是我的Client类,它连接到服务器,处理来往服务器的消息,并从服务器注销。
private TcpClient ClientConnection = null;
private NetworkStream CommandStream = null;
private Thread ClientThread = null;
bool isRunning = false;
public Client()
{
InitializeComponent();
try
{
//open the connection to the command port
ClientConnection = new TcpClient(address, Convert.ToInt32(port));
//Get the command stream
CommandStream = ClientConnection.GetStream();
if (CommandStream != null)
{
isConnected = true;
ClientThread = new Thread(Receive);
isRunning = true;
ClientThread.Start();
}
errorLabel.Visible = false;
}
catch (Exception ex)
{
errorLabel.Visible = true;
errorLabel.Text = "Failed to Connect";
}
}
private void Receive()
{
Byte[] data = new Byte[1024];
string message = string.Empty;
int BytesReceived = 0;
while (isRunning == true)
{
BytesReceived = CommandStream.Read(data, 0, 1024);
message = Encoding.ASCII.GetString(data, 0, BytesReceived);
//Do something with the message
}
}
private void logoutButton_Click(object sender, EventArgs e)
{
//Do logout logic here
errorLabel.Visible = false;
try
{
if (ClientConnection != null)
{
Byte[] command = new Byte[1024];
string commandStr = "SHUTDOWN";
command = Encoding.ASCII.GetBytes(commandStr);
CommandStream.Write(command, 0, command.GetLength(0));
ClientConnection.Close();
CommandStream.Close();
isRunning = false;
if (ClientThread.IsAlive == true)
{
errorLabel.Visible = true;
errorLabel.Text = "Thread still alive. Failed to Disconnect";
}
}
}
catch(Exception ex)
{
errorLabel.Visible = true;
errorLabel.Text = "Failed to Disconnect";
}
}
这是我的服务器类'Process Handler Function:
private void CommandProcessHandler(Socket client)
{
Byte[] data = new Byte[1024];
NetworkStream NetStream = null;
//Exception check
if(client.Connected == true)
NetStream = new NetworkStream(client);
while(bCommandListener == true)
{
//Read the command from the client
int bytes = NetStream.Read(data, 0, 1024);
string Command = Encoding.ASCII.GetString(data, 0, bytes);
//Do something with the command
if (Command == "SHUTDOWN")
{
NetStream.Close();
client.Close();
bCommandListener = false;
continue;
}
//Display the command in the command list box
string buttonPressed;
string buttonLetter;
buttonPressed = CommandDataObject.Instance.DecodeUIDFromMessage(Command);
buttonLetter = CommandDataObject.Instance.DecodeMessageFromUID(Command);
Command = ((client.RemoteEndPoint) as IPEndPoint).Address.ToString() + ">>>" + Command;
UpdateCommandsListBox(Command);
}
}
我确信我的线程完全正常,但问题在于关闭它们。这是我从未正确教过的一个方面,我在学习上遇到了麻烦。按照目前的情况,我按下“注销”按钮,该按钮应该向服务器发送一条消息以关闭,以及关闭TcpClient和NetworkStream。它还设置isRunning bool,它使客户端的Receive线程保持活动状态为false。
然而,就目前而言,我在第98行(在客户端的Receive函数期间,当BytesReceived = CommandStream.Read期间)执行此操作时出现错误,如下所示:
An unhandled exception of type 'System.IO.IOException' occurred in System.dll
Additional information: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.
我不确定错误是指什么,但我已经坚持了一段时间,并想要修复它。有解决方案吗
答案 0 :(得分:0)
当您使用NetworkStream时,如果在尝试阅读see MSDN documentation时底层套接字已关闭,它将抛出IOException。所以你的代码非常好。你只需要处理异常。
如果您直接读取套接字,则在套接字关闭时您将收到0。