分类: C/C++
2009-09-19 13:02:21
#include
#include
int listenfd = socket(AF_INET, SOCK_STREAM, 0);//创建socket fd
if(listenfd < 0){
std::cout<<"create socket error!\n";
sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//地址为INADDR_ANY表示监听本机所有网络接口
servaddr.sin_port = htons(1500);//监听端口,客户端程序必须连接这个端口才能与服务器交互
if(bind(listenfd, (sockaddr*)(&servaddr), sizeof(servaddr)) < 0){
std::cout<<"bind error!\n";
if(listen(listenfd, 5) < 0){
std::cout<<"listen error!\n";
int connfd = accept(listenfd, 0, 0);
if(connfd < 0){
std::cout<<"accept error!\n";
std::cout<<"a client connect me\n";
time_t ticks = std::time(0);
std::stringstream strout;
int n = write(connfd, strout.str().c_str(),strout.str().size());
if(n < 0){
std::cout<<"write error!\n";
std::cout<<"send
"<