1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| Unit Unit1;
interface
uses System, System.Drawing, System.Windows.Forms, System.Net, System.Net.Sockets, System.IO, System.Threading;
type
Form1 = class(Form)
{$region FormDesigner}
private
{$resource Unit1.Form1.resources}
textBox1: TextBox;
{$include Unit1.Form1.inc}
{$endregion FormDesigner}
public
constructor;
begin
InitializeComponent;
end;
end;
type
TByteArray = array of byte;
var
client: array [1..64] of TCPClient;
s_str: array [1..64] of NetworkStream;
s_raw: TByteArray;
listener: system.Net.Sockets.TcpListener;
s_ip, msg, nowdate, cl_str: string;
s_port, length, i, n: integer;
thr: array [1..64] of thread;
thr_s: thread;
implementation
procedure chat(data: object);
var
i, err:integer;
name: string;
begin
Val(data.ToString, i, err);
s_str[i] := client[i].GetStream;
length := s_str[i].Read(s_raw, 0, s_raw.Length);
name := System.Text.Encoding.Default.GetString(s_raw, 0, length);
//сообщение('Есть клиент ', name, '.');
//сообщение msg := Concat('Добро пожаловать на сервер ', s_ip, ', ', name, '!');
s_str[i].Write(System.Text.Encoding.Default.GetBytes(msg), 0, msg.Length);
try
while true do
begin
length := s_str[i].Read(s_raw, 0, s_raw.Length);
cl_str := System.Text.Encoding.Default.GetString(s_raw, 0, length);
writeln(name, ': ', cl_str);
for j:integer:=1 to n do begin
s_str[j] := client[j].GetStream;
msg := name + ': ' + cl_str{+#13#10};
if s_str[j].CanWrite then
s_str[j].Write(System.Text.Encoding.Default.GetBytes(msg), 0, msg.Length)
else
//сообщение'Ошибка. Невозможно отправить сообщение клиенту.';
end;
end;
except
//сообщение'Клиент отключился.';
end;
end;
begin
System.Windows.Forms.Application.Run(new Form1);
s_raw := TByteArray(System.Array.CreateInstance(typeof(byte), 1024));
try
s_ip:='127.0.0.1';
s_port:=9090;
listener := TCPListener.Create(IPAddress.Parse(s_ip), s_port);
listener.Start(64);
//собщение'Готово. Ожидаем подключения...';
except
//сообщение'Ошибка. Приложение будет закрыто.';
sleep(2500);
exit;
end;
i := 0;
while true do
if listener.Pending then
begin
i += 1;
n:=i;
client[i] := listener.AcceptTcpClient();
thr[i] := new Thread(chat);
thr[i].Start(i);
end;
end. |