Как отключить клиент от сервера, не закрывая при этом приложения - C#
Формулировка задачи:
Приветствую. Имеется 2 приложения сервер-клиент.
При загрузке форму сервер запускается на прослушку из класса, отправка так-же из класса.
Собственно вопросы:
1)Как правильно завершить поток? Как отключить клиент от сервера, не закрывая при этом приложения.
2)Как научить сервер слушать сразу несколько клиентов? Или хотя бы переходить в режим прослушки после отсоединения первого клиента.
Класс сервера:
Класс клиента:
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 Server
{
class Class1
{
public static int i;
public static TcpListener server = new TcpListener(IPAddress.Any, 11000);
public static NetworkStream stream;
public static TcpClient client;
public static byte[] datalength = new byte[4];
public static string message;
public static void ServerReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
stream.Read(data, 0, data.Length);
message = System.Environment.NewLine + "Client : " + Encoding.Default.GetString(data);
MessageBox.Show(message);
}
}).Start();
}
public static void ServerSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
}
}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 Client
{
class Class1
{
public static string userid;
public static string userpass;
public static int i;
public static TcpClient client;
public static NetworkStream stream;
public static byte[] datalength = new byte[4];
public static string message;
public static string messagei;
public static void ClientSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
public static void ClientReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while (((i = stream.Read(datalength, 0, 4)) != 0))
{
byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
stream.Read(data, 0, data.Length);
{
message= System.Environment.NewLine + "Server : " + Encoding.Default.GetString(data); // Encoding.Default.GetString(data); Converts Bytes Received to String
MessageBox.Show(message);
};
}
}).Start();
}
}
}Решение задачи: «Как отключить клиент от сервера, не закрывая при этом приложения»
textual
Листинг программы
public static void ServerReceive()
{
stream = client.GetStream();
bool stop = false;
new Thread(() =>
{
while (!stop)
{
try
{
if ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
stream.Read(data, 0, data.Length);
message = System.Environment.NewLine + "Client : " + Encoding.Default.GetString(data);
MessageBox.Show(message);
}
}
catch (Exception)
{
MessageBox.Show("Клиент разорвал соединение");
Class1.server.Start();
MessageBox.Show("Waiting For Connection");
new Thread(() =>
{
Class1.client = Class1.server.AcceptTcpClient();
MessageBox.Show("Connected To Client");
if (Class1.client.Connected)
{
Class1.ServerReceive();
}
}).Start();
stop = true;
break;
}
}