Sunteți pe pagina 1din 26

1 // HelloWorld.cpp : Defines the entry point for the console application.

2 //
3
4 #include "stdafx.h"
5 #include <stdio.h>
6 #include <conio.h>
7 #include <winsock2.h>
8 #include <ws2tcpip.h>
9 int _tmain(int argc, _TCHAR* argv[])
10 {
11 WSADATA wsaData;
12 WORD wVersion = MAKEWORD(2,2);
13 int ret;
14 SOCKET s;
15 SOCKADDR_IN address;
16
17 ret = WSAStartup(wVersion,&wsaData);
18 if (ret==SOCKET_ERROR)
19 {
20 printf("Khong khoi tao dc WinSock\n");
21 }
22 else
23 printf("Khoi tao thanh cong!\n");
24
25 addrinfo *res,*pHead;
26
27
28 char str[1024];
29 while (1)
30 {
31 printf("Nhap vao ten mien:");
32 gets(str);
33 ret = getaddrinfo(str,"http",0,&res);
34 if (ret == 0) // Thanh cong
35 {
36 pHead = res;
37 while (res!=0)
38 {
39 memcpy(&address,res->ai_addr,res->ai_addrlen);
40 printf("Dia chi IP:%s\n",inet_ntoa(address.sin_addr));
41 res = res->ai_next;
42 }
43 freeaddrinfo(pHead);
44 }
45 else
46 printf("Khong ton tai ten mien nay!!!\n");
47 }
48
49 ret = WSACleanup();
50 if (ret==SOCKET_ERROR)
51 {
52 ret = WSAStartup(wVersion,&wsaData);
53 printf("Loi:%d",WSAGetLastError());
54 }
55 else
56 printf("Giai phong thanh cong!\n");
57
58 getch();
59 return 0;
60 }
61
62
63 // TCPClient.cpp : Defines the entry point for the console application.
64 //
65
66 #include "stdafx.h"
67 #include <winsock2.h>
68 #include <stdio.h>
lap trinh manf 1 / 26
69 #include <conio.h>
70
71 int _tmain(int argc, _TCHAR* argv[])
72 {
73
74 WSADATA wsaData;
75 WORD wVersion = MAKEWORD(2,2);
76 int ret;
77 ret = WSAStartup(wVersion,&wsaData);
78
79 SOCKET s;
80 SOCKADDR_IN serverAddr;
81 char diachi[128];
82 while(1)
83 {
84 printf("Nhap dia chi ip cua server:");
85 gets(diachi);
86 if(inet_addr(diachi)!=INADDR_NONE)
87 break;
88 else
89 printf("Dia chi khong hop le!\n");
90 };
91 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
92 serverAddr.sin_family = AF_INET;
93 serverAddr.sin_port = htons(8888);
94 serverAddr.sin_addr.s_addr = inet_addr(
95 );
96
97 ret = connect(s,(sockaddr*)&serverAddr,
98 sizeof(serverAddr));
99 if (ret==SOCKET_ERROR)
100 {
101 printf("Loi %d",WSAGetLastError());
102 getch();
103 closesocket(s);
104 WSACleanup();
105 return 0;
106 };
107 printf("Da ket noi den server!\n");
108 char str[1024];
109 while(1)
110 {
111 ret = recv(s,str,1024,0);
112 if (ret>0)
113 {
114 str[ret]=0;
115 printf("Server:%s",str);
116 }
117 else
118 {
119 printf("Ket noi da bi ngat!");
120 break;
121 };
122 gets(str);
123 send(s,str,strlen(str),0);
124 }
125
126 getch();
127 closesocket(s);
128 WSACleanup();
129 return 0;
130 }
131
132 // TCPServer.cpp : Defines the entry point for the console application.
133 //
134
135 #include "stdafx.h"
136 #include <winsock2.h>
lap trinh manf 2 / 26
137 #include <stdio.h>
138 #include <conio.h>
139
140 int _tmain(int argc, _TCHAR* argv[])
141 {
142 WSADATA wsaData;
143 WORD wVersion = MAKEWORD(2,2);
144 int ret ;
145 ret = WSAStartup(wVersion,&wsaData);
146 SOCKET server,client;
147 SOCKADDR_IN serverAddr,clientAddr;
148 int clientAddrLen = sizeof(clientAddr);
149 server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
150
151 serverAddr.sin_family = AF_INET;
152 serverAddr.sin_port = htons(8888);
153 serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
154
155 ret = bind(server,(sockaddr*)&serverAddr,
156 sizeof(serverAddr));
157 if (ret == SOCKET_ERROR)
158 {
159 printf("Khong mo duoc cong 8888!");
160 closesocket(server);
161 WSACleanup();
162 getch();
163 return 0;
164 };
165 ret =listen(server,10);
166
167
168 client = accept(server,(sockaddr*)&clientAddr,
169 &clientAddrLen);
170 printf("Co ket noi moi tu %s:%d\n",
171 inet_ntoa(clientAddr.sin_addr),
172 ntohs(clientAddr.sin_port));
173
174 char buff[1024];
175 while(1)
176 {
177 gets(buff);
178 strcat(buff,"\r\n");
179 ret = send(client,buff,strlen(buff),0);
180
181 ret = recv(client,buff,1024,0);
182 buff[ret]=0;
183 printf("Client:%s\n",buff);
184
185 }
186
187
188 getch();
189 closesocket(server);
190 WSACleanup();
191 return 0;
192 }
193
194 // UDPSender.cpp : Defines the entry point for the console application.
195 //
196
197 #include "stdafx.h"
198 #include <winsock2.h>
199 #include <stdio.h>
200 #include <conio.h>
201
202 int _tmain(int argc, _TCHAR* argv[])
203 {
204 WSADATA wsaData;
lap trinh manf 3 / 26
205 WORD wVersion = MAKEWORD(2,2);
206 int ret;
207 WSAStartup(wVersion,&wsaData);
208 SOCKET sender;
209 SOCKADDR_IN receiverAddr;
210 sender = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
211 receiverAddr.sin_family = AF_INET;
212 receiverAddr.sin_port = htons(8888);
213 receiverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
214
215 char str[1024];
216 while (1)
217 {
218 gets(str);
219 strcat(str,"\r\n");
220 sendto(sender,str,strlen(str),0,
221 (sockaddr*)&receiverAddr,
222 sizeof(receiverAddr));
223 }
224 getch();
225 closesocket(sender);
226 WSACleanup();
227 return 0;
228 }
229
230 // UDPReceiver.cpp : Defines the entry point for the console application.
231 //
232
233 #include "stdafx.h"
234 #include <winsock2.h>
235 #include <stdio.h>
236 #include <conio.h>
237
238 int _tmain(int argc, _TCHAR* argv[])
239 {
240 WSADATA wsaData;
241 WORD wVersion = MAKEWORD(2,2);
242 int ret;
243 WSAStartup(wVersion,&wsaData);
244
245 SOCKET receiver;
246 receiver = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
247 SOCKADDR_IN receiverAddr,senderAddr;
248 int senderAddrLen = sizeof(senderAddr);
249 receiverAddr.sin_family = AF_INET;
250 receiverAddr.sin_port = htons(8888);
251 receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
252 ret = bind(receiver,(sockaddr*)&receiverAddr,
253 sizeof(receiverAddr));
254 if (ret==SOCKET_ERROR)
255 {
256 printf("Loi %d",WSAGetLastError());
257 closesocket(receiver);
258 WSACleanup();
259 getch();
260 return 0;
261 };
262 char str[1024];
263 while (1)
264 {
265 ret = recvfrom(receiver,str,1024,0,
266 (sockaddr*)&senderAddr,
267 &senderAddrLen);
268 if (ret<=0)
269 break;
270 str[ret]=0;
271 printf("[%s:%d]%s\n",
272 inet_ntoa(senderAddr.sin_addr),
lap trinh manf 4 / 26
273 ntohs(senderAddr.sin_port),str);
274
275 };
276
277 getch();
278 closesocket(receiver);
279 WSACleanup();
280 return 0;
281 }
282
283 // UDPFileTransfer.cpp : Defines the entry point for the console application.
284 //
285
286 #include "stdafx.h"
287 #include <windows.h>
288 struct Packet
289 {
290 BYTE type;
291 char sender[32];
292 char receiver[32];
293 int length;
294 WORD checksum;
295 int offset;
296 BYTE data[65536];
297 }
298 int _tmain(int argc, _TCHAR* argv[])
299 {
300 char buff[1024];
301 Packet pk;
302 pk.type = 0x01;
303 strcpy(pk.sender,"abc");
304 strcpy(pk.receiver,"def");
305 pk.length = 1024;
306 pk.checksum = 0x1234;
307 pk.offset = 0;
308 memcpy(pk.data,buff,pk.length);
309 sendto(s,&pk,75+pk.length,0,&cAddr,sizeof(cAddr));
310
311
312 recvfrom(s,&pk,75,....);
313 ..
314 recvfrom(s,pk.data,pk.length,0,...);
315
316
317 return 0;
318 }
319
320 // Server.cpp : Defines the entry point for the console application.
321 //
322
323 #include "stdafx.h"
324 #include <winsock2.h>
325 #include <stdio.h>
326 #include <conio.h>
327 SOCKET s,c;
328 SOCKADDR_IN sAddr,cAddr;
329 unsigned char x;
330 DWORD WINAPI ReceiverThread(LPVOID lpParam)
331 {
332 char str[1024];
333 int len;
334 int tg;
335 while (1)
336 {
337 len = recv(c,str,1024,0);
338 if (len>=1)
339 {
340 str[len]=0;
lap trinh manf 5 / 26
341 for (int i=0;i<len;i++)
342 if (str[i]>=x)
343 str[i] = str[i]-x;
344 else
345 {
346 tg = str[i];
347 tg=tg+256;
348 tg=tg-x;
349 str[i]=tg;
350 };
351 printf("Client:%s",str);
352
353 }
354
355 }
356 }
357
358 int _tmain(int argc, _TCHAR* argv[])
359 {
360 WSADATA wsaData;
361 int ret;
362
363 ret = WSAStartup(MAKEWORD(2,2),&wsaData);
364
365 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
366 sAddr.sin_family = AF_INET;
367 sAddr.sin_port = htons(8888);
368 sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
369 printf("Nhap vao mot so x:");
370 scanf("%d",&x);
371
372 ret = bind(s,(sockaddr*)&sAddr,sizeof(sAddr));
373 ret = listen(s,10);
374
375 int cAddrLen = sizeof(cAddr);
376 c = accept(s,(sockaddr*)&cAddr,&cAddrLen);
377
378 send(c,(const char*)&x,1,0);
379 CreateThread(0,0,ReceiverThread,0,0,0);
380 char str[1024];
381 int len;
382 int tg;
383 while (1)
384 {
385 scanf("%s",str);
386 len = strlen(str);
387 if (len>1)
388 {
389 for (int i=0;i<len;i++)
390 {
391 tg = str[i];
392 tg=tg+x;
393 str[i] = tg%256;
394 };
395 send(c,str,len,0);
396 }
397 else
398 break;
399 };
400 closesocket(c);
401 closesocket(s);
402 WSACleanup();
403
404 return 0;
405 }
406 // SelectServer.cpp : Defines the entry point for the console application.
407 //
408
lap trinh manf 6 / 26
409 #include "stdafx.h"
410 #include <winsock2.h>
411 #include <stdio.h>
412 #include <conio.h>
413
414 int _tmain(int argc, _TCHAR* argv[])
415 {
416 WSADATA wsaData;
417 WORD wVersion = MAKEWORD(2,2);
418 int ret;
419 WSAStartup(wVersion,&wsaData);
420
421
422 SOCKET s,c = SOCKET_ERROR;
423 SOCKADDR_IN sAddr,cAddr;
424 int cLen;
425
426
427 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
428 sAddr.sin_family = AF_INET;
429 sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
430 sAddr.sin_port = htons(8888);
431
432 ret = bind(s,(sockaddr*)&sAddr,sizeof(sAddr));
433 ret = listen(s,10);
434
435 fd_set readfds;
436 char str[1024];
437 while (1)
438 {
439 FD_ZERO(&readfds);
440
441 if (c==SOCKET_ERROR)
442 FD_SET(s,&readfds);
443
444 if (c!=SOCKET_ERROR)
445 FD_SET(c,&readfds);
446 ret = select(0,&readfds,0,0,0);
447 if (ret<=0)
448 {
449 printf("Loi %d",WSAGetLastError());
450 break;
451 };
452 if (FD_ISSET(s,&readfds))
453 {
454 if (c==SOCKET_ERROR) // Chua dng socket c
455 {
456 cLen = sizeof(cAddr);
457 c = accept(s,(sockaddr*)&cAddr,&cLen);
458 printf("Co khach den choi\n");
459 }
460 else
461 {
462 printf("Khong the tiep them khach nua,sorry!");
463
464 }
465
466 };
467 if (c!=SOCKET_ERROR)
468 if (FD_ISSET(c,&readfds))
469 {
470 ret = recv(c,str,1024,0);
471 if (ret<=0)
472 {
473 printf("Loi %d",WSAGetLastError());
474 continue;
475 };
476 str[ret] = 0;
lap trinh manf 7 / 26
477 printf("Client:%s\n",str);
478 }
479
480 }
481
482
483
484 return 0;
485 }
486
487 // Overlapped-Event.cpp : Defines the entry point for the console application.
488 //
489
490 #include "stdafx.h"
491 #include <winsock2.h>
492 #include <stdio.h>
493 #include <conio.h>
494 int _tmain(int argc, _TCHAR* argv[])
495 {
496 WSADATA wsaData;
497 int ret;
498 WSAStartup(MAKEWORD(2,2),&wsaData);
499 SOCKET c;
500 SOCKADDR_IN sAddr;
501 int len;
502 c = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
503
504 sAddr.sin_family = AF_INET;
505 sAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
506 sAddr.sin_port = htons(8888);
507
508 ret = connect(c,(sockaddr*)&sAddr,sizeof(sAddr));
509
510 if (ret==SOCKET_ERROR)
511 {
512 //
513 };
514 OVERLAPPED overlapped;
515 memset(&overlapped,0,sizeof(overlapped));
516 overlapped.hEvent = WSACreateEvent();
517 char buff[1024];
518 WSABUF databuf;
519 databuf.buf = buff;
520 databuf.len = 1024;
521 DWORD bytesReceived = 0;
522 DWORD flags = 0;
523 while (1)
524 {
525 ret = WSARecv(c,&databuf,1,&bytesReceived,&flags,
526 &overlapped,0);
527 if (ret == SOCKET_ERROR)
528 {
529 ret = WSAGetLastError();
530 if (ret != WSA_IO_PENDING)
531 {
532 printf("Loi %d !\n",ret);
533 continue;
534 }
535 };
536 ret = WSAWaitForMultipleEvents(1,&overlapped.hEvent,
537 TRUE,WSA_INFINITE,FALSE);
538 if ((ret == WSA_WAIT_FAILED)||
539 (ret==WSA_WAIT_TIMEOUT)) continue;
540 WSAResetEvent(overlapped.hEvent);
541 ret = WSAGetOverlappedResult(c,&overlapped,
542 &bytesReceived,FALSE,&flags);
543 // Kim tra li
544
lap trinh manf 8 / 26
545 // Hin thi
546 buff[bytesReceived] = 0;
547 printf(buff);
548 }
549
550
551
552 return 0;
553 }
554
555 // Overlapped-Completion.cpp : Defines the entry point for the console application.
556 //
557
558 #include "stdafx.h"
559
560 #include <winsock2.h>
561 #include <stdio.h>
562 #include <conio.h>
563 SOCKET s;
564 OVERLAPPED overlapped;
565 char buff[1024];
566 WSABUF databuff;
567 DWORD flags;
568 DWORD bytesReceived = 0;
569 int ret = 0;
570
571 void CALLBACK CompletionRoutine( IN DWORD dwError,
572 IN DWORD cbTransferred,
573 IN LPWSAOVERLAPPED lpOverlapped,
574 IN DWORD dwFlags)
575 {
576 if (dwError != 0||cbTransferred==0) // Xu l li
577 {
578 closesocket(s);
579 return;
580 };
581 // Hin thi xu ra mn hnh
582 buff[cbTransferred]=0;
583 printf(buff);
584
585 // Khoi tao lai cu trc overlapped v lai gui tip yu cu nhn d liu
586 memset(&overlapped,0,sizeof(overlapped));
587 flags = 0;
588 ret = WSARecv(s, &databuff, 1, &bytesReceived, &flags, &overlapped, CompletionRoutine);
589 if (ret == SOCKET_ERROR)
590 {
591 ret = WSAGetLastError();
592 if (ret != WSA_IO_PENDING)
593 printf("Loi %d !\n",ret);
594 };
595
596 return;
597 }
598
599 int _tmain(int argc, _TCHAR* argv[])
600 {
601 // Khoi tao v kt ni dn 127.0.0.1:8888
602 WSADATA wsaData;
603 WSAStartup(MAKEWORD(2,2),&wsaData);
604 SOCKADDR_IN sAddr;
605 sAddr.sin_family = AF_INET;
606 sAddr.sin_port = htons(8888);
607 sAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
608 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
609 ret = connect(s,(sockaddr*)&sAddr,sizeof(sAddr));
610 // Khoi tao cu trc overlapped
611 memset(&overlapped,0,sizeof(overlapped));
612 // Khoi tao b dm d liu
lap trinh manf 9 / 26
613 databuff.buf = buff;
614 databuff.len = 1024;
615 // Gui yu cu vo ra
616 ret = WSARecv(s, &databuff,1,&bytesReceived,&flags,&overlapped, CompletionRoutine);
617 // Xu l li
618 // Chuyn lung sang trang thi alertable
619 while (1) SleepEx(1000,TRUE);
620 getch();
621 closesocket(s);
622 WSACleanup();
623 return 0;
624
625 return 0;
626 }
627 // MFCSocket.cpp : Defines the entry point for the console application.
628 //
629
630 #include "stdafx.h"
631 #include "MFCSocket.h"
632 #include <afxsock.h>
633
634 #ifdef _DEBUG
635 #define new DEBUG_NEW
636 #endif
637
638
639 // The one and only application object
640
641 CWinApp theApp;
642
643 using namespace std;
644
645 int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
646 {
647 int nRetCode = 0;
648
649 // initialize MFC and print and error on failure
650 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
651 {
652 // TODO: change error code to suit your needs
653 _tprintf(_T("Fatal Error: MFC initialization failed\n"));
654 nRetCode = 1;
655 }
656 else
657 {
658 client.Create();
659 client.Connect(L"127.0.0.1",8888);
660 char str[1024];
661 strcpy(str,"Hello CSocket");
662 client.Send(str,strlen(str));
663 client.Close();
664
665
666 }
667
668 return nRetCode;
669 }
670 // FileServer.cpp : Defines the entry point for the console application.
671 //
672
673 #include "stdafx.h"
674 #include <winsock2.h>
675 #include <conio.h>
676 #include <stdio.h>
677 SOCKET s;
678 SOCKET c[64];
679 SOCKADDR_IN sAddr;
680 SOCKADDR_IN cAddrs[64];
lap trinh manf 10 / 26
681 int nClients;
682 int cAddrLen = sizeof(SOCKADDR_IN);
683 DWORD WINAPI HandleClient(LPVOID lpParam)
684 {
685 int index = (int)lpParam;
686 char str[1024];
687 char command[1024];
688 char reply[1024];
689 char filename[1024];
690 int len = 0;
691 command[0] = 0;
692 while (1)
693 {
694 len = recv(c[index],str,1024,0);
695 if (len>0)
696 {
697 strcat(command,str);
698 if (strchr(command,'\n')!=0)
699 {
700 if(strstr(command,"GET ")==command)
701 {
702 strncpy(filename,command+4,
703 strchr(command,'\n')-command-4);
704 filename[strchr(command,'\n')-command-4]=0;
705 break;
706 }
707 else
708 {
709 strcpy(reply,"FAILED\nInvalid command\n\n");
710 send(c[index],reply,strlen(reply),0);
711 closesocket(c[index]);
712 c[index] = SOCKET_ERROR;
713 return 0;
714 }
715 }
716
717 }
718
719 };
720 printf("Client yeu cau file:%s\n",filename);
721 FILE * fp;
722 int filelen = 0;
723 fp = fopen(filename,"rb");
724 if (!fp)
725 {
726 strcpy(reply,"FAILED\nFile not found\n\n");
727 send(c[index],reply,strlen(reply),0);
728 closesocket(c[index]);
729 c[index] = SOCKET_ERROR;
730 return 0;
731 };
732 fseek(fp,0,SEEK_END);
733 filelen = ftell(fp);
734 fseek(fp,0,SEEK_SET);
735 sprintf(reply,"OK\n%d\n\n",filelen);
736 send(c[index],reply,strlen(reply),0);
737 char buff[1024];
738 while (!feof(fp))
739 {
740 len = fread(buff,1,1024,fp);
741 send(c[index],buff,len,0);
742 };
743 fclose(fp);
744 closesocket(c[index]);
745 c[index]=SOCKET_ERROR;
746 printf("Gui xong !");
747 return 0;
748
lap trinh manf 11 / 26
749 }
750
751 int _tmain(int argc, _TCHAR* argv[])
752 {
753 WSADATA wsaData;
754 WSAStartup(MAKEWORD(2,2),&wsaData);
755 int ret = 0;
756 int i;
757 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
758 sAddr.sin_family = AF_INET;
759 sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
760 sAddr.sin_port = htons(8888);
761
762 ret = bind(s,(sockaddr*)&sAddr,sizeof(sAddr));
763 if (ret==SOCKET_ERROR)
764 {
765 printf("Loi %d",WSAGetLastError());
766 // Xu ly loi...
767 return 0;
768 };
769 listen(s,10);
770 nClients = 0;
771 for (i=0;i<64;i++) c[i] = SOCKET_ERROR;
772 while (1)
773 {
774 for (i=0;i<nClients;i++)
775 if (c[i] == SOCKET_ERROR) break;
776 c[i]= accept(s,(sockaddr*)&cAddrs[i],
777 &cAddrLen);
778 CreateThread(0,0,HandleClient,(LPVOID)i,0,0);
779 }
780 return 0;
781 }
782
783 // BlockingServer.cpp : Defines the entry point for the console application.
784 //
785
786 #include "stdafx.h"
787 #include <winsock2.h>
788 #include <stdio.h>
789 #include <conio.h>
790 SOCKET server,client;
791 DWORD WINAPI ReceiverThread(LPVOID lpParam)
792 {
793 char str[1024];
794 int ret;
795 while (1)
796 {
797 ret = recv(client,str,1024,0);
798 if (ret>0)
799 {
800 str[ret] = 0;
801 printf("Client:%s\n",str);
802 }
803 else
804 {
805 printf("Ket noi da bi dong!");
806 break;
807 }
808 }
809 return 0;
810 }
811
812
813 int _tmain(int argc, _TCHAR* argv[])
814 {
815 WSADATA wsaData;
816 WORD wVersion = MAKEWORD(2,2);
lap trinh manf 12 / 26
817 int ret;
818 WSAStartup(wVersion,&wsaData);
819
820 SOCKADDR_IN serverAddr,clientAddr;
821 int clientAddrLen;
822
823 server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
824 serverAddr.sin_family = AF_INET;
825 serverAddr.sin_port = htons(8888);
826 serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
827 ret = bind(server,(sockaddr*)&serverAddr,sizeof(serverAddr));
828 if (ret==SOCKET_ERROR)
829 {
830 printf("Loi %d",WSAGetLastError());
831 closesocket(server);
832 WSACleanup();
833 getch();
834 return 0;
835 };
836 ret = listen(server,10);
837 clientAddrLen = sizeof(clientAddr);
838 client = accept(server,(sockaddr*)&clientAddr,
839 &clientAddrLen);
840 printf("Co ket noi tu %s:%d\n",
841 inet_ntoa(clientAddr.sin_addr),
842 htons(clientAddr.sin_port));
843 CreateThread(0,0,ReceiverThread,0,0,0);
844 char str[1024];
845 while (1)
846 {
847 gets(str);
848 strcat(str,"\r\n");
849 ret = send(client,str,strlen(str),0);
850 }
851
852 closesocket(server);
853 closesocket(client);
854 WSACleanup();
855 return 0;
856 }
857
858 // BlockingClient.cpp : Defines the entry point for the console application.
859 //
860
861 #include "stdafx.h"
862
863
864 #include <winsock2.h>
865 #include <stdio.h>
866 #include <conio.h>
867 SOCKET s;
868 DWORD WINAPI ReceiverThread(LPVOID lpParam)
869 {
870 char str[1024];
871 int ret;
872 while (1)
873 {
874 ret = recv(s,str,1024,0);
875 if (ret>0)
876 {
877 str[ret] = 0;
878 printf("Server:%s\n",str);
879 }
880 else
881 {
882 printf("Ket noi da bi dong!");
883 break;
884 }
lap trinh manf 13 / 26
885 }
886 return 0;
887 }
888
889 int _tmain(int argc, _TCHAR* argv[])
890 {
891
892 WSADATA wsaData;
893 WORD wVersion = MAKEWORD(2,2);
894 int ret;
895 ret = WSAStartup(wVersion,&wsaData);
896
897 SOCKADDR_IN serverAddr;
898 char diachi[128];
899 while(1)
900 {
901 printf("Nhap dia chi ip cua server:");
902 gets(diachi);
903 if(inet_addr(diachi)!=INADDR_NONE)
904 break;
905 else
906 printf("Dia chi khong hop le!\n");
907 };
908 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
909 serverAddr.sin_family = AF_INET;
910 serverAddr.sin_port = htons(8888);
911 serverAddr.sin_addr.s_addr = inet_addr(diachi);
912
913 ret = connect(s,(sockaddr*)&serverAddr,
914 sizeof(serverAddr));
915 if (ret==SOCKET_ERROR)
916 {
917 printf("Loi %d",WSAGetLastError());
918 getch();
919 closesocket(s);
920 WSACleanup();
921 return 0;
922 };
923 printf("Da ket noi den server!\n");
924 CreateThread(0,0,ReceiverThread,0,0,0);
925 char str[1024];
926 while(1)
927 {
928
929 gets(str);
930 send(s,str,strlen(str),0);
931 }
932
933 getch();
934 closesocket(s);
935 WSACleanup();
936 return 0;
937 }
938
939
940 // BetterSelectServer.cpp : Defines the entry point for the console application.
941 //
942
943 #include "stdafx.h"
944 #include <winsock2.h>
945
946 int _tmain(int argc, _TCHAR* argv[])
947 {
948 WSADATA wsaData;
949 WORD wVersion = MAKEWORD(2,2);
950 // Khoi tao WinSock
951 WSAStartup(wVersion,&wsaData);
952 // Khai bo socket cho server
lap trinh manf 14 / 26
953 SOCKET s;
954 // Khai bo mang socket cho client
955 SOCKET c[64];
956 // Bin dm s luong socket ti da
957 // dsu dng
958 int nClient = 0;
959 SOCKADDR_IN sAddr;// ia chi server
960 SOCKADDR_IN cAddrs[64];// ia chi client
961
962 int i,ret;
963 // Khoi tao mang cc socket client
964 for (i=0;i<64;i++) c[i] = SOCKET_ERROR;
965
966 // Tao socket cho server
967 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
968 sAddr.sin_family = AF_INET;
969 sAddr.sin_port = htons(8888);
970 sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
971 // Gn dia chi cho socket server
972 ret = bind(s,(sockaddr*)&sAddr,
973 sizeof(sAddr));
974 // Chuyn server sang trang thi doi
975 // kt ni
976 ret = listen(s,10);
977
978 fd_set readfds;
979 char str[1024];
980 while (1)
981 {
982 // Xa tp readfds
983 FD_ZERO(&readfds);
984 // Cho s vo tp tham d
985 FD_SET(s,&readfds);
986 // Cho cc client hop l vo tp
987 // d tham d
988 for (i=0;i<nClient;i++)
989 if (c[i]!=SOCKET_ERROR)
990 FD_SET(c[i],&readfds);
991 // Thc hin tham d s kin
992 ret = select(0,&readfds,0,0,0);
993 if (ret<0)
994 break;
995 if (FD_ISSET(s,&readfds)) // Nu c kt ni mi
996 {
997 // Tm mt ch trng trong mang
998 // c d cho socket vo
999 for (i=0;i<nClient;i++)
1000 if (c[i]==SOCKET_ERROR) break;
1001 if (i<64) // Nu chua du 64 kt ni
1002 // => thm duoc
1003 {
1004 int cLen = sizeof(SOCKADDR_IN);
1005 // Chp nhn kt ni mi
1006 c[i] = accept(s,(sockaddr*)&cAddrs[i],
1007 &cLen);
1008 // Hin thng tin v kt ni mi
1009 printf("Co ket noi moi tu %s:%d\n",
1010 inet_ntoa(cAddrs[i].sin_addr),
1011 htons(cAddrs[i].sin_port));
1012 nClient++;
1013 }
1014 else
1015 {
1016 SOCKET tmp;
1017 SOCKADDR_IN tmpAddr;
1018 int tmpLen = sizeof(tmpAddr);
1019 tmp = accept(s,(sockaddr*)&tmpAddr,
1020 &tmpLen);
lap trinh manf 15 / 26
1021 closesocket(tmp);
1022 printf("Khong tiep khach nua :-w");
1023 }
1024 }
1025 int len = 0;
1026 for (i=0;i<nClient;i++)
1027 if (c[i]!=SOCKET_ERROR)
1028 if (FD_ISSET(c[i],&readfds))
1029 {
1030 len = recv(c[i],str,1024,0);
1031 if (len>0)
1032 {
1033 str[len] = 0;
1034 printf("Client %d:%s\n",i,str);
1035 }
1036 else
1037 {
1038 closesocket(c[i]);
1039 c[i] = SOCKET_ERROR;
1040 }
1041 }
1042
1043
1044 }
1045
1046 return 0;
1047 }
1048
1049 ,0
1050 FILE * fp;
1051 char buff[1024];
1052 int len;
1053 fp = fopen("name.txt","rb");
1054 while (!feof(fp))
1055 {
1056 len = fread(buff,1,1024,fp);
1057 if (len
1058 }
1059
1060 // BetterBlockingServer.cpp : Defines the entry point for the console application.
1061 //
1062
1063 #include "stdafx.h"
1064 #include <winsock2.h>
1065 #include <stdio.h>
1066 #include <conio.h>
1067 SOCKET server;
1068 SOCKET clients[64];
1069 SOCKADDR_IN serverAddr;
1070 SOCKADDR_IN clientAddrs[64];
1071 int nClients,clientAddrLen;
1072
1073 DWORD WINAPI DocBanPhim(LPVOID lpParam)
1074 {
1075 char str[1024];
1076 int i;
1077 while (1)
1078 {
1079 gets(str);
1080 strcat(str,"\r\n");
1081 for (i=0;i<nClients;i++)
1082 if(clients[i]!=SOCKET_ERROR)
1083 {
1084 send(clients[i],str,strlen(str),0);
1085 }
1086 }
1087 }
1088 DWORD WINAPI ReceiverThread(LPVOID lpParam)
lap trinh manf 16 / 26
1089 {
1090 int index = (int)lpParam;
1091 char str[1024];
1092 int ret;
1093 while (1)
1094 {
1095 ret = recv(clients[index],str,1024,0);
1096 if (ret<=0)
1097 {
1098 closesocket(clients[index]);
1099 clients[index] = SOCKET_ERROR;
1100 break;
1101 };
1102 str[ret] = 0;
1103 printf("[%s:%d]: %s\n",
1104 inet_ntoa(clientAddrs[index].sin_addr),
1105 htons(clientAddrs[index].sin_port),
1106 str);
1107 for (int i = 0;i<nClients;i++)
1108 if ((clients[i]!=SOCKET_ERROR)&&
1109 (i!=index))
1110 send(clients[i],str,strlen(str),0);
1111
1112
1113
1114 };
1115 return 0;
1116 }
1117 int _tmain(int argc, _TCHAR* argv[])
1118 {
1119 WSADATA wsaData;
1120 WORD wVersion = MAKEWORD(2,2);
1121 int ret = 0;
1122 bool bFirstClient = true;
1123 WSAStartup(wVersion,&wsaData);
1124 server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
1125 serverAddr.sin_family = AF_INET;
1126 serverAddr.sin_port = htons(8888);
1127 serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
1128
1129 ret = bind(server,(sockaddr*)&serverAddr,
1130 sizeof(serverAddr));
1131
1132 if (ret==SOCKET_ERROR)
1133 {
1134 printf("Loi %d",WSAGetLastError());
1135 closesocket(server);
1136 WSACleanup();
1137 getch();
1138 return 0;
1139 };
1140 ret = listen(server,10);
1141 int i;
1142 for (i = 0;i<64;i++)
1143 clients[i] = SOCKET_ERROR;
1144 nClients = 0;
1145
1146 while (1)
1147 {
1148 for (i=0;i<nClients;i++)
1149 if (clients[i]==SOCKET_ERROR) break;
1150 clientAddrLen = sizeof(SOCKADDR_IN);
1151 clients[i] = accept(server,
1152 (sockaddr*)&clientAddrs[i],
1153 &clientAddrLen);
1154 if (bFirstClient)
1155 {
1156 CreateThread(0,0,DocBanPhim,0,0,0);
lap trinh manf 17 / 26
1157 bFirstClient = false;
1158 };
1159 CreateThread(0,0,ReceiverThread,(LPVOID)i,0,0);
1160 nClients++;
1161 }
1162 closesocket(server);
1163 for (i = 0;i<nClients;i++)
1164 if (clients[i]!=SOCKET_ERROR)
1165 closesocket(clients[i]);
1166 WSACleanup();
1167 getch();
1168 return 0;
1169 }
1170
1171 // AsyncSocket.cpp : Defines the class behaviors for the application.
1172 //
1173
1174 #include "stdafx.h"
1175 #include "MySocket.h"
1176 #include "AsyncSocket.h"
1177 #include "AsyncSocketDlg.h"
1178
1179 #ifdef _DEBUG
1180 #define new DEBUG_NEW
1181 #endif
1182
1183
1184 // CAsyncSocketApp
1185
1186 BEGIN_MESSAGE_MAP(CAsyncSocketApp, CWinApp)
1187 ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
1188 END_MESSAGE_MAP()
1189
1190
1191 // CAsyncSocketApp construction
1192
1193 CAsyncSocketApp::CAsyncSocketApp()
1194 {
1195 // TODO: add construction code here,
1196 // Place all significant initialization in InitInstance
1197 }
1198
1199
1200 // The one and only CAsyncSocketApp object
1201
1202 CAsyncSocketApp theApp;
1203
1204
1205 // CAsyncSocketApp initialization
1206
1207 BOOL CAsyncSocketApp::InitInstance()
1208 {
1209 // InitCommonControlsEx() is required on Windows XP if an application
1210 // manifest specifies use of ComCtl32.dll version 6 or later to enable
1211 // visual styles. Otherwise, any window creation will fail.
1212 INITCOMMONCONTROLSEX InitCtrls;
1213 InitCtrls.dwSize = sizeof(InitCtrls);
1214 // Set this to include all the common control classes you want to use
1215 // in your application.
1216 InitCtrls.dwICC = ICC_WIN95_CLASSES;
1217 InitCommonControlsEx(&InitCtrls);
1218
1219 CWinApp::InitInstance();
1220
1221 if (!AfxSocketInit())
1222 {
1223 AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
1224 return FALSE;
lap trinh manf 18 / 26
1225 }
1226
1227 AfxEnableControlContainer();
1228
1229 // Standard initialization
1230 // If you are not using these features and wish to reduce the size
1231 // of your final executable, you should remove from the following
1232 // the specific initialization routines you do not need
1233 // Change the registry key under which our settings are stored
1234 // TODO: You should modify this string to be something appropriate
1235 // such as the name of your company or organization
1236 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
1237
1238 CAsyncSocketDlg dlg;
1239 m_pMainWnd = &dlg;
1240 INT_PTR nResponse = dlg.DoModal();
1241 if (nResponse == IDOK)
1242 {
1243 // TODO: Place code here to handle when the dialog is
1244 // dismissed with OK
1245 }
1246 else if (nResponse == IDCANCEL)
1247 {
1248 // TODO: Place code here to handle when the dialog is
1249 // dismissed with Cancel
1250 }
1251
1252 // Since the dialog has been closed, return FALSE so that we exit the
1253 // application, rather than start the application's message pump.
1254 return FALSE;
1255 }
1256 // AsyncSelectServer.cpp : Defines the class behaviors for the application.
1257 //
1258
1259 #include "stdafx.h"
1260 #include "AsyncSelectServer.h"
1261 #include "AsyncSelectServerDlg.h"
1262
1263 #ifdef _DEBUG
1264 #define new DEBUG_NEW
1265 #endif
1266
1267
1268 // CAsyncSelectServerApp
1269
1270 BEGIN_MESSAGE_MAP(CAsyncSelectServerApp, CWinApp)
1271 ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
1272 END_MESSAGE_MAP()
1273
1274
1275 // CAsyncSelectServerApp construction
1276
1277 CAsyncSelectServerApp::CAsyncSelectServerApp()
1278 {
1279 // TODO: add construction code here,
1280 // Place all significant initialization in InitInstance
1281 }
1282
1283
1284 // The one and only CAsyncSelectServerApp object
1285
1286 CAsyncSelectServerApp theApp;
1287
1288
1289 // CAsyncSelectServerApp initialization
1290
1291 BOOL CAsyncSelectServerApp::InitInstance()
1292 {
lap trinh manf 19 / 26
1293 // InitCommonControlsEx() is required on Windows XP if an application
1294 // manifest specifies use of ComCtl32.dll version 6 or later to enable
1295 // visual styles. Otherwise, any window creation will fail.
1296 INITCOMMONCONTROLSEX InitCtrls;
1297 InitCtrls.dwSize = sizeof(InitCtrls);
1298 // Set this to include all the common control classes you want to use
1299 // in your application.
1300 InitCtrls.dwICC = ICC_WIN95_CLASSES;
1301 InitCommonControlsEx(&InitCtrls);
1302
1303 CWinApp::InitInstance();
1304
1305 AfxEnableControlContainer();
1306
1307 // Standard initialization
1308 // If you are not using these features and wish to reduce the size
1309 // of your final executable, you should remove from the following
1310 // the specific initialization routines you do not need
1311 // Change the registry key under which our settings are stored
1312 // TODO: You should modify this string to be something appropriate
1313 // such as the name of your company or organization
1314 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
1315
1316 CAsyncSelectServerDlg dlg;
1317 m_pMainWnd = &dlg;
1318 INT_PTR nResponse = dlg.DoModal();
1319 if (nResponse == IDOK)
1320 {
1321 // TODO: Place code here to handle when the dialog is
1322 // dismissed with OK
1323 }
1324 else if (nResponse == IDCANCEL)
1325 {
1326 // TODO: Place code here to handle when the dialog is
1327 // dismissed with Cancel
1328 }
1329
1330 // Since the dialog has been closed, return FALSE so that we exit the
1331 // application, rather than start the application's message pump.
1332 return FALSE;
1333 }
1334 // AsyncSelectClient.cpp : Defines the class behaviors for the application.
1335 //
1336
1337 #include "stdafx.h"
1338 #include "AsyncSelectClient.h"
1339 #include "AsyncSelectClientDlg.h"
1340
1341 #ifdef _DEBUG
1342 #define new DEBUG_NEW
1343 #endif
1344
1345
1346 // CAsyncSelectClientApp
1347
1348 BEGIN_MESSAGE_MAP(CAsyncSelectClientApp, CWinApp)
1349 ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
1350 END_MESSAGE_MAP()
1351
1352
1353 // CAsyncSelectClientApp construction
1354
1355 CAsyncSelectClientApp::CAsyncSelectClientApp()
1356 {
1357 // TODO: add construction code here,
1358 // Place all significant initialization in InitInstance
1359 }
1360
lap trinh manf 20 / 26
1361
1362 // The one and only CAsyncSelectClientApp object
1363
1364 CAsyncSelectClientApp theApp;
1365
1366
1367 // CAsyncSelectClientApp initialization
1368
1369 BOOL CAsyncSelectClientApp::InitInstance()
1370 {
1371 // InitCommonControlsEx() is required on Windows XP if an application
1372 // manifest specifies use of ComCtl32.dll version 6 or later to enable
1373 // visual styles. Otherwise, any window creation will fail.
1374 INITCOMMONCONTROLSEX InitCtrls;
1375 InitCtrls.dwSize = sizeof(InitCtrls);
1376 // Set this to include all the common control classes you want to use
1377 // in your application.
1378 InitCtrls.dwICC = ICC_WIN95_CLASSES;
1379 InitCommonControlsEx(&InitCtrls);
1380
1381 CWinApp::InitInstance();
1382
1383 AfxEnableControlContainer();
1384
1385 // Standard initialization
1386 // If you are not using these features and wish to reduce the size
1387 // of your final executable, you should remove from the following
1388 // the specific initialization routines you do not need
1389 // Change the registry key under which our settings are stored
1390 // TODO: You should modify this string to be something appropriate
1391 // such as the name of your company or organization
1392 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
1393
1394 CAsyncSelectClientDlg dlg;
1395 m_pMainWnd = &dlg;
1396 INT_PTR nResponse = dlg.DoModal();
1397 if (nResponse == IDOK)
1398 {
1399 // TODO: Place code here to handle when the dialog is
1400 // dismissed with OK
1401 }
1402 else if (nResponse == IDCANCEL)
1403 {
1404 // TODO: Place code here to handle when the dialog is
1405 // dismissed with Cancel
1406 }
1407
1408 // Since the dialog has been closed, return FALSE so that we exit the
1409 // application, rather than start the application's message pump.
1410 return FALSE;
1411 }
1412 // AsyncSelect.cpp : Defines the class behaviors for the application.
1413 //
1414
1415 #include "stdafx.h"
1416 #include "AsyncSelect.h"
1417 #include "AsyncSelectDlg.h"
1418
1419 #ifdef _DEBUG
1420 #define new DEBUG_NEW
1421 #endif
1422
1423
1424 // CAsyncSelectApp
1425
1426 BEGIN_MESSAGE_MAP(CAsyncSelectApp, CWinApp)
1427 ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
1428 END_MESSAGE_MAP()
lap trinh manf 21 / 26
1429
1430
1431 // CAsyncSelectApp construction
1432
1433 CAsyncSelectApp::CAsyncSelectApp()
1434 {
1435 // TODO: add construction code here,
1436 // Place all significant initialization in InitInstance
1437 }
1438
1439
1440 // The one and only CAsyncSelectApp object
1441
1442 CAsyncSelectApp theApp;
1443
1444
1445 // CAsyncSelectApp initialization
1446
1447 BOOL CAsyncSelectApp::InitInstance()
1448 {
1449 // InitCommonControlsEx() is required on Windows XP if an application
1450 // manifest specifies use of ComCtl32.dll version 6 or later to enable
1451 // visual styles. Otherwise, any window creation will fail.
1452 INITCOMMONCONTROLSEX InitCtrls;
1453 InitCtrls.dwSize = sizeof(InitCtrls);
1454 // Set this to include all the common control classes you want to use
1455 // in your application.
1456 InitCtrls.dwICC = ICC_WIN95_CLASSES;
1457 InitCommonControlsEx(&InitCtrls);
1458
1459 CWinApp::InitInstance();
1460
1461 AfxEnableControlContainer();
1462
1463 // Standard initialization
1464 // If you are not using these features and wish to reduce the size
1465 // of your final executable, you should remove from the following
1466 // the specific initialization routines you do not need
1467 // Change the registry key under which our settings are stored
1468 // TODO: You should modify this string to be something appropriate
1469 // such as the name of your company or organization
1470 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
1471
1472 CAsyncSelectDlg dlg;
1473 m_pMainWnd = &dlg;
1474 INT_PTR nResponse = dlg.DoModal();
1475 if (nResponse == IDOK)
1476 {
1477 // TODO: Place code here to handle when the dialog is
1478 // dismissed with OK
1479 }
1480 else if (nResponse == IDCANCEL)
1481 {
1482 // TODO: Place code here to handle when the dialog is
1483 // dismissed with Cancel
1484 }
1485
1486 // Since the dialog has been closed, return FALSE so that we exit the
1487 // application, rather than start the application's message pump.
1488 return FALSE;
1489 }
1490 // AsyncSelectDlg.cpp : implementation file
1491 //
1492
1493 #include "stdafx.h"
1494 #include "AsyncSelect.h"
1495 #include "AsyncSelectDlg.h"
1496
lap trinh manf 22 / 26
1497 #ifdef _DEBUG
1498 #define new DEBUG_NEW
1499 #endif
1500
1501 #define WM_SOCKET WM_USER+1
1502
1503
1504 // CAboutDlg dialog used for App About
1505
1506 class CAboutDlg : public CDialog
1507 {
1508 public:
1509 CAboutDlg();
1510
1511 // Dialog Data
1512 enum { IDD = IDD_ABOUTBOX };
1513
1514 protected:
1515 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
1516
1517 // Implementation
1518 protected:
1519 DECLARE_MESSAGE_MAP()
1520 };
1521
1522 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
1523 {
1524 }
1525
1526 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
1527 {
1528 CDialog::DoDataExchange(pDX);
1529 }
1530
1531 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
1532 END_MESSAGE_MAP()
1533
1534
1535 // CAsyncSelectDlg dialog
1536
1537
1538
1539
1540 CAsyncSelectDlg::CAsyncSelectDlg(CWnd* pParent /*=NULL*/)
1541 : CDialog(CAsyncSelectDlg::IDD, pParent)
1542 , m_sIpAddress(_T(""))
1543 {
1544 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
1545 }
1546
1547 void CAsyncSelectDlg::DoDataExchange(CDataExchange* pDX)
1548 {
1549 CDialog::DoDataExchange(pDX);
1550 DDX_Control(pDX, IDC_IPADDRESS, m_ipaddress);
1551 DDX_Text(pDX, IDC_IPADDRESS, m_sIpAddress);
1552 }
1553
1554 BEGIN_MESSAGE_MAP(CAsyncSelectDlg, CDialog)
1555 ON_WM_SYSCOMMAND()
1556 ON_WM_PAINT()
1557 ON_WM_QUERYDRAGICON()
1558 //}}AFX_MSG_MAP
1559 ON_BN_CLICKED(IDC_BUTTON1, &CAsyncSelectDlg::OnBnClickedButton1)
1560 END_MESSAGE_MAP()
1561
1562
1563 // CAsyncSelectDlg message handlers
1564
lap trinh manf 23 / 26
1565 BOOL CAsyncSelectDlg::OnInitDialog()
1566 {
1567 CDialog::OnInitDialog();
1568
1569 // Add "About..." menu item to system menu.
1570
1571 // IDM_ABOUTBOX must be in the system command range.
1572 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
1573 ASSERT(IDM_ABOUTBOX < 0xF000);
1574
1575 CMenu* pSysMenu = GetSystemMenu(FALSE);
1576 if (pSysMenu != NULL)
1577 {
1578 CString strAboutMenu;
1579 strAboutMenu.LoadString(IDS_ABOUTBOX);
1580 if (!strAboutMenu.IsEmpty())
1581 {
1582 pSysMenu->AppendMenu(MF_SEPARATOR);
1583 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
1584 }
1585 }
1586
1587 // Set the icon for this dialog. The framework does this automatically
1588 // when the application's main window is not a dialog
1589 SetIcon(m_hIcon, TRUE); // Set big icon
1590 SetIcon(m_hIcon, FALSE); // Set small icon
1591
1592 // TODO: Add extra initialization here
1593
1594 WSADATA wsaData;
1595 WORD wVersion = MAKEWORD(2,2);
1596 WSAStartup(wVersion,&wsaData);
1597
1598
1599 return TRUE; // return TRUE unless you set the focus to a control
1600 }
1601
1602 void CAsyncSelectDlg::OnSysCommand(UINT nID, LPARAM lParam)
1603 {
1604 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
1605 {
1606 CAboutDlg dlgAbout;
1607 dlgAbout.DoModal();
1608 }
1609 else
1610 {
1611 CDialog::OnSysCommand(nID, lParam);
1612 }
1613 }
1614
1615 // If you add a minimize button to your dialog, you will need the code below
1616 // to draw the icon. For MFC applications using the document/view model,
1617 // this is automatically done for you by the framework.
1618
1619 void CAsyncSelectDlg::OnPaint()
1620 {
1621 if (IsIconic())
1622 {
1623 CPaintDC dc(this); // device context for painting
1624
1625 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
1626
1627 // Center icon in client rectangle
1628 int cxIcon = GetSystemMetrics(SM_CXICON);
1629 int cyIcon = GetSystemMetrics(SM_CYICON);
1630 CRect rect;
1631 GetClientRect(&rect);
1632 int x = (rect.Width() - cxIcon + 1) / 2;
lap trinh manf 24 / 26
1633 int y = (rect.Height() - cyIcon + 1) / 2;
1634
1635 // Draw the icon
1636 dc.DrawIcon(x, y, m_hIcon);
1637 }
1638 else
1639 {
1640 CDialog::OnPaint();
1641 }
1642 }
1643
1644 // The system calls this function to obtain the cursor to display while the user drags
1645 // the minimized window.
1646 HCURSOR CAsyncSelectDlg::OnQueryDragIcon()
1647 {
1648 return static_cast<HCURSOR>(m_hIcon);
1649 }
1650
1651
1652 void CAsyncSelectDlg::OnBnClickedButton1()
1653 {
1654 // TODO: Add your control notification handler code here
1655 c = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
1656 char address[128];
1657 int len;
1658 int ret;
1659 UpdateData();
1660 len = WideCharToMultiByte(CP_ACP,0,m_sIpAddress,
1661 m_sIpAddress.GetLength(),
1662 address,128,0,0);
1663 address[len] = 0;
1664
1665
1666 ret = WSAAsyncSelect(c,this->m_hWnd,WM_SOCKET,
1667 FD_READ|FD_WRITE|FD_CONNECT|FD_CLOSE);
1668 SOCKADDR_IN serverAddr;
1669 serverAddr.sin_family = AF_INET;
1670 serverAddr.sin_port = htons(8888);
1671 serverAddr.sin_addr.s_addr = inet_addr(address);
1672 ret = connect(c,(sockaddr*)&serverAddr,sizeof(serverAddr));
1673 if (ret == SOCKET_ERROR)
1674 {
1675 ret = WSAGetLastError();
1676
1677 }
1678
1679 }
1680
1681 LRESULT CAsyncSelectDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
1682 {
1683 // TODO: Add your specialized code here and/or call the base class
1684
1685 switch(message)
1686 {
1687 case WM_SOCKET:
1688 if (WSAGETSELECTERROR(lParam))
1689 {
1690 closesocket(c);
1691 MessageBox(L"Li kt ni");
1692 break;
1693 };
1694 switch (WSAGETSELECTEVENT(lParam))
1695 {
1696 case FD_CONNECT:
1697 MessageBox(L"Kt ni thnh cng");
1698 break;
1699
1700 }
lap trinh manf 25 / 26
1701 }
1702
1703 return CDialog::WindowProc(message, wParam, lParam);
1704 }
1705
1706
lap trinh manf 26 / 26

S-ar putea să vă placă și