Код: Выделить всё
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Random rnd = new Random();
public Form1() { Server();
InitializeComponent();
}
public void Server()
{
IPHostEntry ipHost = Dns.GetHostEntry("localhost");
IPAddress ipAddr = ipHost.AddressList[1];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
Socket sListener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
sListener.Bind(ipEndPoint);
sListener.Listen(10);
while (true)
{
//label4.Text = "Ожидаем соединение через порт: " + ipEndPoint;
Socket handler = sListener.Accept();
string data = null;
string reply = label3.Text;
byte[] msg = Encoding.UTF8.GetBytes(reply);
handler.Send(msg);
if (data.IndexOf("<TheEnd>") > -1)
{
label4.Text = "Сервер завершил соединение с клиентом.";
break;
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.ReadLine();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int temp = rnd.Next(7000, 12000);
label3.Text = Convert.ToString(temp);
}
}
}