生活随笔
收集整理的這篇文章主要介紹了
windows编程,服务器与客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一。程序運行效果圖
二。程序源代碼
三。程序設計相關基礎知識
??? 1.計算機網絡
??? 2.IP地址
??? 3.協議
??? 4.網絡體系結構
??? 5.TCP/IP體系結構與特點
??? 6.客戶機/服務器模式
??? 7.TCP/IP特點
??? 8.套接字的引入
??? 9.面向 連接/無連接 的套接字的系統調用時序圖/流程圖
?
一。程序運行效果圖
?
二。程序源代碼
[cpp]?view plaincopy print?
?? ?? #include?<iostream>?? #include?<cstdio>?? #include?<Winsock2.h>?? ?? using?namespace?std;?? ?? int?main()?? {?? ?? ????WORD?wVersionRequested;?? ????WSADATA?wsaData;?????? ????int?err;?? ??????? ????wVersionRequested?=?MAKEWORD(?1,?1?);????? ??????? ????err?=?WSAStartup(?wVersionRequested,?&wsaData?);?? ????if?(?err?!=?0?)?{?? ????????return?-1;???????????? ????}?? ??????? ????if?(?LOBYTE(?wsaData.wVersion?)?!=?1?||?HIBYTE(?wsaData.wVersion?)?!=?1?)?{?? ?????????? ?????????? ????????WSACleanup(?);?? ????????return?-1;??? ????}?? ?????? ?? ?????? ?????? ?????? ????SOCKET?sockSrv?=?socket(AF_INET,?SOCK_STREAM,?0);????? ?? ?? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ????SOCKADDR_IN?addrSrv;?? ????addrSrv.sin_addr.S_un.S_addr?=?htonl(INADDR_ANY);??? ????addrSrv.sin_family?=?AF_INET;?? ????addrSrv.sin_port?=?htons(6000);?? ?? ????bind(sockSrv,?(SOCKADDR*)&addrSrv,?sizeof(SOCKADDR));??? ?? ?? ?????? ?????? ????listen(sockSrv,?10);?? ?? ?? ?????? ?????? ?????? ?????? ?????? ?? ?? ?? ?????? ?????? ?????? ?????? ?????? ?????? ?? ?????? ?????? ?????? ?????? ?????? ?????? ?? ????SOCKADDR_IN??addrClient;?? ????int?len?=?sizeof(SOCKADDR);?? ?? ????while(true){?????? ????????SOCKET?sockConn?=?accept(sockSrv,?(SOCKADDR*)&addrClient,?&len);?? ?????????? ????????char?sendBuf[100];?? ????????sprintf(sendBuf,?"Welcome?%s?to?the?server?program~?\nNow,?let's?start?talking...\n",?inet_ntoa(addrClient.sin_addr));?? ????????send(sockConn,?sendBuf,?strlen(sendBuf)+1,?0);???? ?? ????????char?recvBuf[100];?? ????????recv(sockConn,?recvBuf,?100,?0);?? ????????printf("%s\n",?recvBuf);?????????? ?? ????????char?*?sockConnName?=?"Client";?? ????????printf("我們可以聊五句話");?? ????????int?n?=?5;?? ????????while(n--){?? ????????????printf("還剩%d次:\n",?n+1);?? ????????????char?recvBuf[100];?? ????????????recv(sockConn,?recvBuf,?100,?0);?? ????????????printf("%s?Says:?%s\n",?sockConnName,?recvBuf);??????? ?????????????? ????????????char?talk[100];?? ????????????printf("Please?enter?what?you?want?to?say?next(\"quit\"to?exit):");?? ????????????gets(talk);?? ????????????send(sockConn,?talk,?strlen(talk)+1,?0);?????????????? ????????????printf("\n");?? ????????}????????? ????????printf("\nEnd?talking...?\n");?? ????????closesocket(sockConn);?? ????}?? ?? ????printf("\n");?? ????system("pause");?? ????return?0;?? }??
?
[cpp]?view plaincopy print?
?? ?? #include?<iostream>?? #include?<cstdio>?? #include?<Winsock2.h>?? ?? using?namespace?std;?? ?? int?main()?? {?? ?? ????WORD?wVersionRequested;?? ????WSADATA?wsaData;?????? ????int?err;?? ??????? ????wVersionRequested?=?MAKEWORD(?1,?1?);????? ??????? ????err?=?WSAStartup(?wVersionRequested,?&wsaData?);?? ????if?(?err?!=?0?)?{?? ????????return?-1;???????????? ????}?? ??????? ????if?(?LOBYTE(?wsaData.wVersion?)?!=?1?||?HIBYTE(?wsaData.wVersion?)?!=?1?)?{?? ?????????? ?????????? ????????WSACleanup(?);?? ????????return?-1;??? ????}?? ?????? ?? ?????? ?????? ?????? ????SOCKET?sockClient?=?socket(AF_INET,?SOCK_STREAM,?0);?? ?? ?? ?????? ?????? ?????? ?????? ????SOCKADDR_IN?addrSrv;?? ????addrSrv.sin_addr.S_un.S_addr?=?inet_addr("127.0.0.1");???????? ????addrSrv.sin_family?=?AF_INET;?? ????addrSrv.sin_port?=?htons(6000);?? ????connect(sockClient,?(SOCKADDR*)&addrSrv,?sizeof(SOCKADDR));?? ?? ????char?recvBuf[100];?? ????recv(sockClient,?recvBuf,?100,?0);?? ????printf("%s\n",?recvBuf);?? ?? ????send(sockClient,?"Attention:?A?Client?has?enter...\n",?strlen("Attention:?A?Client?has?enter...\n")+1,?0);?? ?? ????printf("我們可以聊五句話");?? ????int?n?=?5;?? ????do{?? ????????printf("\n還剩%d次:",?n);?? ????????char?talk[100];?? ????????printf("\nPlease?enter?what?you?want?to?say?next(\"quit\"to?exit):");?? ????????gets(talk);?? ????????send(sockClient,?talk,?strlen(talk)+1,?0);???????????? ?? ????????char?recvBuf[100];?? ????????recv(sockClient,?recvBuf,?100,?0);?? ????????printf("%s?Says:?%s\n",?"Server",?recvBuf);??????? ????}while(--n);?? ?? ????printf("End?linking...\n");?? ????closesocket(sockClient);?? ????WSACleanup();????? ?? ????printf("\n");?? ????system("pause");?? ????return?0;?? }??
?
三。程序設計相關基礎知識
1.計算機網絡
2.IP地址
3.協議
4.網絡體系結構
5.TCP/IP體系結構與特點
6.客戶機/服務器模式
7.TCP/IP特點
8.套接字的引入
9.面向 連接/無連接 的套接字的系統調用時序圖/流程圖
總結
以上是生活随笔為你收集整理的windows编程,服务器与客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。