再读C++ Primer 写了个小例子——网络编程
2008-03-19 23:25
309 查看
再读C++ Primer 写了个小例子——网络编程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <Winsock2.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WORD wVersionRequested;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WSADATA wsaData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int err;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wVersionRequested =MAKEWORD(1,1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
err = WSAStartup(wVersionRequested,&wsaData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(err != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WSACleanup();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKET sockSrv =socket(AF_INET,SOCK_STREAM,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKADDR_IN addrSrv;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addrSrv.sin_family=AF_INET;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addrSrv.sin_port = htons(6000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
listen(sockSrv,5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKADDR_IN addrClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int len = sizeof(SOCKADDR);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKET sockConn =accept(sockSrv,(SOCKADDR*)&addrClient,&len);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char sendBuf[100];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sprintf(sendBuf,"Welcome %s to http://www.zhangdali.org",inet_ntoa(addrClient.sin_addr));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char recvBuf[100];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
recv(sockConn,recvBuf,100,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%s ",recvBuf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
closesocket(sockConn);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <Winsock2.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WORD wVersionRequested;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WSADATA wsaData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int err;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wVersionRequested =MAKEWORD(1,1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
err = WSAStartup(wVersionRequested,&wsaData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(err != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WSACleanup();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKET sockSrv =socket(AF_INET,SOCK_STREAM,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKADDR_IN addrSrv;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addrSrv.sin_family=AF_INET;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addrSrv.sin_port = htons(6000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
listen(sockSrv,5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKADDR_IN addrClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int len = sizeof(SOCKADDR);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SOCKET sockConn =accept(sockSrv,(SOCKADDR*)&addrClient,&len);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char sendBuf[100];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sprintf(sendBuf,"Welcome %s to http://www.zhangdali.org",inet_ntoa(addrClient.sin_addr));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char recvBuf[100];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
recv(sockConn,recvBuf,100,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%s ",recvBuf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
closesocket(sockConn);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 再读C++ Primer 写了个小例子——练习多态虚函数的特性(08-01-25)
- 再读C++ Primer 写了些小例子来熟悉C++基本语法
- 再读C++ Primer 写了个小例子(08-01-16)
- 再读C++ Primer 写了个小例子——友员类的操作
- 再读C++ Primer 写了个小例子——运算符重载(08-01-17)
- 再读C++ Primer 写了个小例子——模板的操作(08-01-18)
- 再读C++ Primer 写了个小例子——实现Vector类的自动添加元素功能(08-12-10)
- 再读C++ Primer 写了个小例子——实现stack类的压栈弹栈功能(08-12-10)
- Java Socket网络编程的经典例子(转)
- linux多线程网络编程详解(包含具体例子讲解)
- 网络编程(1)跨平台的Socket同步阻塞工作模式例子
- 一个网络编程解决问题的例子
- Python网络编程TCP服务器与客服端简单例子(调试通过)
- Java Socket网络编程的经典例子
- Java Socket网络编程的经典例子(转)
- C++ primer 3rd 第十二章泛型算法 P480 书中例子
- 复制控制例子 --参照《C++ Primer》第415页消息处理示例
- unix socket网络编程TCP简单例子
- Python网络编程UDP服务器与客服端简单例子(调试通过)
- Python网络编程TCP服务器与客服端简单例子(调试通过)