VC++深入详解笔记_第14章(网络编程).pdf
Lesson 14:API:WSAStartup socket bind listen accept send recv connect recvfrom sendto htons u_shortTCP/IP.htonl u_longTCP/IP.inet_addr:IP,.INADDR_ANY,.inet_ntoa:inet_addr:in_addr,IP.:SOCKADDR SOCKADDR_IN IN_ADDR 1.TCP():Link ws2_32.lib :1 2(socket)3(bind)4(listen)5(accept)6(send/recv)7 8 : 1 2(socket)3(connect)4(send/recv)5 :(TCP):#include#include void main()WORD wVersionRequested;WSADATA wsaData;int err;/MAKEWORDWinsock,.wVersionRequested=MAKEWORD(1,1);/,socket err=WSAStartup(wVersionRequested,&wsaData);/,0.if(err!=0)return;/,1.1 if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();/,Winsock return;/,TCP(SOCK_STREAM)SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);SOCKADDR_IN addrSrv;/SOCKADDR_INsin_addrin_addr(S_un)./sin_addripu_long,/S_addr.INADDR_ANY.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,5);/,5 SOCKADDR_IN addrClient;int len=sizeof(SOCKADDR);while(1)/accept SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);char sendBuf100;/sprintf(sendBuf,hello world!,inet_ntoa(addrClient.sin_addr);send(sockConn,sendBuf,strlen(sendBuf)+1,0);/char recvBuf100;/recv(sockConn,recvBuf,100,0);/printf(%sn,recvBuf);/closesocket(sockConn);(TCP):#include#include void main()/.WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0)return;if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1) WSACleanup();return;/.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 recvBuf100;recv(sockClient,recvBuf,100,0);/printf(%sn,recvBuf);/send(sockClient,This is lisi,strlen(This is lisi)+1,0);/closesocket(sockClient);WSACleanup();2.UDP:1 socket 2 bind 3 recvfrom 4 :1 socket 2 sendto 3 (UDP):#include#include void main()/WORD wVersionRequested; WSADATA wsaData;int err;wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0)return;if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return;/SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,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);/SOCKADDR_IN addrClient;int len=sizeof(SOCKADDR);char recvBuf100;recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);printf(%sn,recvBuf);/closesocket(sockSrv);WSACleanup();(UDP):#include#include void main()/WORD wVersionRequested;WSADATA wsaData;int err; wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0)return;if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return;/SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,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);/sendto(sockClient,Hello,strlen(Hello)+1,0,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR);/closesocket(sockClient);WSACleanup();3.UDP (1)#include#include void main()/WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0) return;if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return;/SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,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);char recvBuf100;char sendBuf100;char tempBuf200;SOCKADDR_IN addrClient;int len=sizeof(SOCKADDR);while(1)/recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);if(q=recvBuf0)/q,sendto(sockSrv,q,strlen(q)+1,0,(SOCKADDR*)&addrClient,len);printf(Chat end!n);break;sprintf(tempBuf,%s say:%s,inet_ntoa(addrClient.sin_addr),recvBuf);printf(%sn,tempBuf);printf(Please input data:n);gets(sendBuf);/,sendBuf/sendto(sockSrv,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrClient,len)/closesocket(sockSrv);WSACleanup(); (2)#include#include void main()/WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0)return;if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return;/SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,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);char recvBuf100;char sendBuf100;char tempBuf200;int len=sizeof(SOCKADDR);while(1)printf(Please input data:n);gets(sendBuf);sendto(sockClient,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrSrv,len); recvfrom(sockClient,recvBuf,100,0,(SOCKADDR*)&addrSrv,&len);if(q=recvBuf0)sendto(sockClient,q,strlen(q)+1,0,(SOCKADDR*)&addrSrv,len);printf(Chat end!n);break;sprintf(tempBuf,%s say:%s,inet_ntoa(addrSrv.sin_addr),recvBuf);printf(%sn,tempBuf);/closesocket(sockClient);WSACleanup();This document was created with Win2PDF available at http:/.The unregistered version of Win2PDF is for evaluation or non-commercial use only.This page will not be added after purchasing Win2PDF.