#include<iostream>  
#include<Winsock2.h>  
#include<string>  
using namespace std;  

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 sockserver=socket(AF_INET,SOCK_STREAM,0);  
    SOCKADDR_IN addrserver;  
    addrserver.sin_addr.S_un.S_addr=htonl(ADDR_ANY);//转化为字节序  
		addrserver.sin_family=AF_INET;  
    addrserver.sin_port=htons(6000);//转化为字节序  
    bind(sockserver,(SOCKADDR*)&addrserver,sizeof(SOCKADDR));  
    listen(sockserver,5);//监听队列最多出现5个客户端  
    SOCKADDR_IN addrclient;  
    int len=sizeof(SOCKADDR);   //accept最后一个参数实为[in/out]  
    while(1)  
    {  
        SOCKET sockConn=accept(sockserver,(SOCKADDR*)&addrclient,&len);  
        string str="hello,I am server,i know your ip:";  
        string ip=inet_ntoa(addrclient.sin_addr);//转化为char*类型  
        send(sockConn,(str+ip).c_str(),str.length()+ip.size()+1,0);  
        char receiveBuf[100];  
        recv(sockConn,receiveBuf,100,0);  
        cout<<"receive from the client:"<<receiveBuf<<endl;  
        closesocket(sockConn);  
    }  
    system("pause");  
}