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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
| // Connection.cpp: implementation of the CConnection class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Winsock2.h"
#include "Connection.h"
#include <fstream>
#include "conio.h"
#include "stdlib.h"
#include <iostream>
using namespace std;
HANDLE hShared = CreateMutex(NULL, TRUE, "WriteData");
ofstream ofs("write.txt");
CConnection::CConnection(SOCKET s)
{
sock=s;
}
CConnection::~CConnection()
{
closesocket(sock);
}
void CConnection::Process()
{
HANDLE hShared = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "WriteData");
ofs << "TestDataWrite" << endl;
cout << "Wait !!!! Write File Data Proccess " << endl;
WaitForSingleObject(hShared, INFINITE);
int x[4][4];
recv(sock,(char*)x,sizeof(int)*16,0);
ofs << '\n';
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
ofs << x[i][j] << ' ';
}
ofs << '\n';
}
int b,c;
recv(sock, (char*)&b, sizeof(int), 0);
recv(sock, (char*)&c, sizeof(int), 0);
for (int i = 0; i < 4; i++)
{
int temp = x[i][b];
x[i][b] = x[i][c];
x[i][c] = temp;
}
send(sock,(char*)x,sizeof(int)*16,0);
ReleaseMutex(hShared);
CloseHandle(hShared);
ofs.close();
}
Listen
// Listen.cpp: implementation of the CListen class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "stdio.h"
#include "Winsock2.h"
#include "Listen.h"
#include "Connection.h"
#include "windows.h"
#include <iostream>
using namespace std;
CListen::CListen()
{
}
CListen::~CListen()
{
}
int CListen::ListenPort(int nPort)
{
SOCKET s,s1;
WSADATA wsaData;
sockaddr_in sa_local;
sockaddr_in sa_remoute;
int i;
if(WSAStartup(MAKEWORD(2,2), &wsaData)!=0)
return 1;
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sa_local.sin_family=AF_INET;
sa_local.sin_port=htons(nPort);
sa_local.sin_addr.S_un.S_addr=INADDR_ANY;
memset(sa_local.sin_zero,0,8);
if(bind(s,(sockaddr*)&sa_local,sizeof(sa_local))!=0)
return 2;
if(listen(s, SOMAXCONN)!=0)
return 3;
while(1)
{
i=sizeof(sa_remoute);
s1=accept(s, (sockaddr*)&sa_remoute,&i);
HANDLE hShared = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "WriteData");
cout << "Wait !!!! Write File Data Proccess " << endl;
WaitForSingleObject(hShared, INFINITE);
CreateThread (NULL,NULL, ConnectionServiceRoutine, &s1, NULL,NULL);
printf("Connection accepted from %i.%i.%i.%i\n",
(int)sa_remoute.sin_addr.S_un.S_un_b.s_b1,
(int)sa_remoute.sin_addr.S_un.S_un_b.s_b2,
(int)sa_remoute.sin_addr.S_un.S_un_b.s_b3,
(int)sa_remoute.sin_addr.S_un.S_un_b.s_b4);
if(s1==NULL)
return 4;
}
return 0;
}
DWORD WINAPI ConnectionServiceRoutine(LPVOID lpParameter)
{
CConnection con(*(SOCKET*)lpParameter);
con.Process();
return 0;
}
Chatserver
// chatserver.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Listen.h"
int main(int argc, char* argv[])
{
CListen server;
server.ListenPort(2810);
return 0;
}
Это сервер,и вот клиент
// client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "conio.h"
using namespace std;
int main(int argc, char* argv[])
{
SOCKET s,s1;
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2), &wsaData)!=0)
return 1;
sockaddr_in sa_local;
sockaddr_in sa_remoute;
int nPort=2810;
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sa_remoute.sin_family=AF_INET;
sa_remoute.sin_port=htons(nPort);
sa_remoute.sin_addr.S_un.S_un_b.s_b1=127;
sa_remoute.sin_addr.S_un.S_un_b.s_b2=0;
sa_remoute.sin_addr.S_un.S_un_b.s_b3=0;
sa_remoute.sin_addr.S_un.S_un_b.s_b4=1;
sa_local.sin_family=AF_INET;
sa_local.sin_port=0;
sa_local.sin_addr.S_un.S_addr=INADDR_ANY;
memset(sa_local.sin_zero,0,8);
if(bind(s,(sockaddr*)&sa_local,sizeof(sa_local))!=0)
return 2;
if(connect(s,(sockaddr*)&sa_remoute,sizeof(sa_remoute))!=0)
return 3;
int m[4][4];
int b,c;
cout << "Vvodite matricy po strokam:\n";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cin >> m[i][j];
}
}
cout << "Vvedite nomera stolbcov kotorie nuzhno pomenyat':";
cin >> b;
cin >> c;
send(s,(char*)m,sizeof(int)*16,0);
send(s, (char*)&b,sizeof(int), 0);
send(s, (char*)&c,sizeof(int), 0);
recv(s,(char*)m,sizeof(int)*16,0);
cout << '\n';
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout << m[i][j] << ' ';
}
cout << '\n';
}
closesocket(s);
getch();
} |