Вывести список подключенных клиентов
30.03.2016, 11:34. Показов 1461. Ответов 1
Здравствуйте дорогие форумчане. Подскажите пожалуйста:
1. Как вывести список подключенных клиентов (для формы Клиенты)
2. И как клиенту выбрать из списка любого, и отправить ему сообщение?
Я знаю только как клиент-сервер.
Сервер:
| C++ | 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| private: System::Void Form1_Activated(System::Object^ sender, System::EventArgs^ e) {
WSADATA WSAData;
int rc;
char Name[101], *IpAddr, Buf[1000];
PHOSTENT phe;
//listBox1->Items->Add("111");
if (f==1) return;
f = 1;
rc = WSAStartup(MAKEWORD(2,0), &WSAData);
if (rc != 0) {
listBox1->Items->Add("Ошибка инициализации WSAStartup");
return;
} // if
TCPSocket = socket(AF_INET, SOCK_STREAM, 0);
if (TCPSocket == INVALID_SOCKET) {
listBox1->Items->Add("Протокол TCP установлен.");
} // if
memset(&OurAddress, 0, sizeof(OurAddress));
OurAddress.sin_family = AF_INET;
OurAddress.sin_port = SERVER_PORT;
rc =bind(TCPSocket, (LPSOCKADDR)&OurAddress, sizeof(sockaddr_in));
if (rc == SOCKET_ERROR) {
listBox1->Items->Add("Адресная ошибка");
return;
} // if
rc = WSAAsyncSelect(TCPSocket, (HWND)(this->Handle.ToInt32()), WSA_NETACCEPT, FD_ACCEPT);
if (rc != 0) {
listBox1->Items->Add("Ошибка WSAAsyncSelect");
return;
} // if
rc = listen(TCPSocket, 1);
if (rc == SOCKET_ERROR) {
listBox1->Items->Add("Ошибка listen");
return;
} // if
gethostname(Name, 101);
strcpy(Buf, "Имя компьютера ");
strcat(Buf, Name);
String ^ s= gcnew String(Buf);
listBox1->Items->Add(s);
phe = gethostbyname(Name);
if (phe != NULL) {
memcpy((void *)&(OurAddress.sin_addr), phe->h_addr, phe->h_length);
IpAddr = inet_ntoa(OurAddress.sin_addr);
strcpy(Buf, "IP-Адрес ");
strcat(Buf, IpAddr);
String ^ s2= gcnew String(Buf);
listBox1->Items->Add(s2);
} // if
listBox1->Items->Add(L"Сервер запущен");
} // Form1_Activated
// -----------------------------------------------------------------------------------------------------------------
protected:
virtual void WndProc (Message% m) override
{
int rc, l=sizeof(CallAddress);
wchar_t Buf[500];
if (m.Msg == WSA_NETACCEPT) {
if (m.LParam.ToInt32() == FD_ACCEPT) {
TmpSocket = accept((SOCKET)m.WParam.ToInt32(), (PSOCKADDR)&CallAddress, (int *)&l);
if (TmpSocket == INVALID_SOCKET) {
rc = WSAGetLastError();
listBox1->Items->Add(String::Format( "Ошибка accept {0}", rc ));
return;
} // if
rc = WSAAsyncSelect(TmpSocket, (HWND)(this->Handle.ToInt32()), WSA_NETEVENT, FD_READ|FD_CLOSE);
if (rc != 0) {
listBox1->Items->Add("Ошибка WSAAsyncSelect");
return;
} // if
comboBox1->Items->Add(gcnew String(inet_ntoa(CallAddress.sin_addr)));
comboBox3->Items->Add(TmpSocket);
comboBox1->Text = gcnew String(inet_ntoa(CallAddress.sin_addr));
comboBox3->Text = Convert::ToString((int)TmpSocket);
listBox1->Items->Add("Канал создан");
} // if
} // if
if (m.Msg == WSA_NETEVENT) {
int Sock = m.WParam.ToInt32();
if (m.LParam.ToInt32() == FD_READ) {
rc = recv((SOCKET)m.WParam.ToInt32(), (char *)Buf, sizeof(Buf)-1, 0);
if (rc == SOCKET_ERROR) {
rc = WSAGetLastError();
listBox1->Items->Add(String::Format( "Ошибка recv {0}", rc ));
return;
} // if
if (rc >= 1) {
String ^ s= gcnew String(Buf);
int nPos = s->IndexOf("|");
String ^SNik = s->Substring(0, nPos);
String ^SMsg = s->Substring(nPos + 1);
listBox2->Items->Add(SMsg);
for (int i = 0; i < comboBox3->Items->Count; i++) {
int iSock = Convert::ToInt32(comboBox3->Items[i]->ToString());
if (iSock == Sock) {
if (i < comboBox2->Items->Count) {
comboBox2->Items[i] = SNik;
} else {
for (int j = comboBox2->Items->Count; j < i; j++) {
comboBox2->Items->Add(" ");
}
comboBox2->Items->Add(SNik);
comboBox2->Text = SNik;
}
} // if
} // for
} // if
} else {
for (int i = 0; i < comboBox3->Items->Count; i++) {
int iSock = Convert::ToInt32(comboBox3->Items[i]->ToString());
if (iSock == Sock) {
comboBox1->Items->RemoveAt(i);
comboBox2->Items->RemoveAt(i);
comboBox3->Items->RemoveAt(i);
if (comboBox1->Items->Count > 0) {
comboBox1->Text = comboBox1->Items[0]->ToString();
comboBox2->Text = comboBox2->Items[0]->ToString();
comboBox3->Text = comboBox3->Items[0]->ToString();
} else {
comboBox1->Text = "";
comboBox2->Text = "";
comboBox3->Text = "";
} // if
} // if
} // for
listBox1->Items->Add("Канал разорван");
} // else
} // if
Form::WndProc( m );
} // WndProc
// -----------------------------------------------------------------------------------------------------------------
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int rc, i, l;
wchar_t Buf[501];
l = textBox1->Text->Length;
if (l == 0) return;
for (i=0; i < l; i++) {
Buf[i] = textBox1->Text->default[i];
Buf[i+1] = 0;
} // for
SOCKET Sock = (SOCKET)(Convert::ToInt32(comboBox3->Text));
rc = send(Sock, (char *)Buf, 2*l+2, 0);
if (rc == SOCKET_ERROR) {
rc = WSAGetLastError();
listBox1->Items->Add(String::Format( "Ошибка send {0}", rc ));
return;
} // if
listBox1->Items->Add(textBox1->Text);
} // button1_Click
// -----------------------------------------------------------------------------------------------------------------
private: System::Void Form1_FormClosed(System::Object^ sender, System::Windows::Forms::FormClosedEventArgs^ e) {
closesocket(TCPSocket);
WSACleanup();
} //Form1_FormClosed
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
int si = comboBox1->SelectedIndex;
comboBox3->SelectedIndex = si;
if (comboBox2->Items->Count > si) comboBox2->SelectedIndex = si;
else comboBox2->Text = " ";
}
private: System::Void comboBox3_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
int si = comboBox3->SelectedIndex;
comboBox1->SelectedIndex = si;
if (comboBox2->Items->Count > si) comboBox2->SelectedIndex = si;
else comboBox2->Text = " ";
}
private: System::Void comboBox2_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
int si = comboBox2->SelectedIndex;
comboBox1->SelectedIndex = si;
comboBox3->SelectedIndex = si;
}
};
} |
|
Клиент:
| C++ | 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| private: System::Void Form1_Activated(System::Object^ sender, System::EventArgs^ e) {
WSADATA WSAData;
int rc;
char Name[101], *IpAddr, Buf[1000];
PHOSTENT phe;
if (f==1) return;
f = 1;
rc = WSAStartup(MAKEWORD(2,0), &WSAData);
if (rc != 0) {
listBox1->Items->Add("Ошибка инициализации WSAStartup");
return;
} // if
TCPSocket = socket(AF_INET, SOCK_STREAM, 0);
if (TCPSocket == INVALID_SOCKET) {
listBox1->Items->Add("Протокол TCP установлен.");
} // if
int i;
for (i = 0; i<64*1024; i++) {
memset(&OurAddress, 0, sizeof(OurAddress));
OurAddress.sin_family = AF_INET;
OurAddress.sin_port = CLIENT_PORT+i;
rc = bind(TCPSocket, (LPSOCKADDR)&OurAddress, sizeof(sockaddr_in));
if (rc != SOCKET_ERROR) {
break;
} // if
} // while
if (i == 64 * 1024) {
listBox1->Items->Add("Адресная ошибка");
return;
}
/*
rc = WSAAsyncSelect(UDPSocket, (HWND)(this->Handle.ToInt32()), WSA_NETEVENT, FD_READ);
if (rc != 0) {
listBox1->Items->Add("Ошибка WSAAsyncSelect");
return;
} // if
*/
gethostname(Name, 101);
strcpy(Buf, "Имя компьютера ");
strcat(Buf, Name);
String ^ s= gcnew String(Buf);
listBox1->Items->Add(s);
phe = gethostbyname(Name);
if (phe != NULL) {
memcpy((void *)&(OurAddress.sin_addr), phe->h_addr, phe->h_length);
IpAddr = inet_ntoa(OurAddress.sin_addr);
strcpy(Buf, "IP-Адрес ");
strcat(Buf, IpAddr);
String ^ s2= gcnew String(Buf);
listBox1->Items->Add(s2);
} // if
listBox1->Items->Add(L"Клиент запущен");
}
// -----------------------------------------------------------------------------------------------------------------
protected: virtual void WndProc (Message% m) override
{
int rc, l=sizeof(CallAddress);
wchar_t Buf[500];
if (m.Msg == WSA_NETEVENT) {
if (m.LParam.ToInt32() == FD_READ) {
rc = recv((SOCKET)m.WParam.ToInt32(), (char *)Buf, sizeof(Buf)-1, 0);
if (rc == SOCKET_ERROR) {
rc = WSAGetLastError();
listBox1->Items->Add(String::Format( "Ошибка recv {0}", rc ));
return;
} // if
if (rc >= 1) {
String ^ s= gcnew String(Buf);
listBox2->Items->Add(s);
} // if
} // if
} // if
Form::WndProc( m );
} // WndProc
// -----------------------------------------------------------------------------------------------------------------
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
int rc, l, i;
char Buf[500];
wchar_t Buf2[1000];
PHOSTENT phe;
l = textBox1->Text->Length;
if (l > 0 && textBox4->Text->Length > 0) {
String ^s = textBox4->Text + "|" + textBox1->Text;
int l2 = s->Length;
for (i=0; i < l2; i++) {
Buf2[i] = s->default[i];
Buf2[i+1] = 0;
} // for
rc = send(TCPSocket, (char *)Buf2, 2*l2+2, 0);
if (rc == SOCKET_ERROR) {
rc = WSAGetLastError();
listBox1->Items->Add(String::Format( "Ошибка recv {0}", rc ));
return;
} // if
String ^ s2= gcnew String(Buf2);
listBox2->Items->Add(s2);
} // if
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
shutdown(TCPSocket,1);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int rc, l, i;
char Buf[500];
PHOSTENT phe;
memset(&CallAddress, 0, sizeof(CallAddress));
CallAddress.sin_family = AF_INET;
CallAddress.sin_port = SERVER_PORT;
l = textBox2->Text->Length;
if (l > 0) {
for (i=0; i < l; i++) {
Buf[i] = textBox2->Text->default[i];
Buf[i+1] = 0;
} // for
CallAddress.sin_addr.s_addr = inet_addr(Buf);
} else {
l = textBox3->Text->Length;
if (l > 0) {
for (i=0; i < l; i++) {
Buf[i] = textBox3->Text->default[i];
Buf[i+1] = 0;
} // for
phe = gethostbyname(Buf);
if (phe != NULL) {
memcpy((void *)&(CallAddress.sin_addr), phe->h_addr, phe->h_length);
} // if
} else {
return;
} // else
} // else
rc = connect(TCPSocket, (PSOCKADDR)&CallAddress, sizeof(CallAddress));
if (rc == SOCKET_ERROR) {
rc = WSAGetLastError();
listBox1->Items->Add(String::Format( "Ошибка connect {0}", rc ));
return;
} // if
listBox1->Items->Add("Канал создан");
rc = WSAAsyncSelect(TCPSocket, (HWND)(this->Handle.ToInt32()), WSA_NETEVENT, FD_READ|FD_CLOSE);
if (rc != 0) {
listBox1->Items->Add("Ошибка WSAAsyncSelect");
return;
} // if
}
};
} |
|
0
|