• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            Socket學(xué)習筆記之二(常用基本函數(shù))

            函數(shù):
            u_long htonl(u_long hostlong)
            u_short htons(u_short hostshort)
            u_long ntohl(u_long netlong)
            u_short ntohs(u_short netshort)

            這上面四個函數(shù)類似,功能相似,都用來轉(zhuǎn)換數(shù)據(jù)格式。用來實現(xiàn)處理器中short,long數(shù)據(jù)類型與網(wǎng)絡(luò)中的轉(zhuǎn)換。在網(wǎng)絡(luò)中傳輸均以字節(jié)為單位(除了bit外就是最小的單位了)。一個short占兩字節(jié),一個long占四個字節(jié)。一個short從一臺機子傳到另外一臺機子上要能夠還原,則必須統(tǒng)一規(guī)定高低字節(jié)順序。在TCP/IP協(xié)議規(guī)范中short的高位在低字節(jié),低位在高字節(jié)。這與有些處理器中或者系統(tǒng)中表示不一樣。例如在windows中:
            ?unsigned short hs = 0x0102;
            ?unsigned short ns = htons( hs );
            ?printf( "0x%04x",ns);//ns:0x0201
            所以對于網(wǎng)絡(luò)端口等數(shù)據(jù)在使用前必須進行統(tǒng)一,例如
            unsigned short port = 2088;
            m_sockaddr.sin_port = htons( port );
            等等。這四個函數(shù),我開始不知道為什么取這些名字總是記不住,后來想明白了
            htons表示host to net short,ntohs表示net to host short
            htonl表示host to net long,ntohl表示net to host long
            這樣不需要刻意去記也就明白了。

            unsigned long inet_addr(const char* cp);
            char* FAR inet_ntoa(struct in_addr in);

            這兩個函數(shù)用來把表示ip地址的字符串(如:202.114.14.12)跟表示ip的long或者結(jié)構(gòu)之間轉(zhuǎn)換。
            inet_addr得到的是已經(jīng)統(tǒng)一字節(jié)順序的,可以直接賦值給in_addr里的s_adr,例如:
            ?char *pHost = "202.114.14.12"
            ?m_sockaddr.sin_addr.s_addr?=?inet_addr( pHost );

            int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSAData);
            加載winsock庫。返回0表示成功,非0表示錯誤。第一個參數(shù)是版本號,現(xiàn)在最高版本是2.2版,不過Win CE好像只支持1.1版,高位字節(jié)表示副版本號,低位字節(jié)表示高版本號,所以這里一般就是0x0202或者0x0101或者使用宏定義MAKEWORD(2,2)或者MAKEWORD(1,1),第二個參數(shù)是一個與winsock庫信息有關(guān)的結(jié)構(gòu),一把不用去管它。在使用winsock前,都必須加載,一般在程序初始化時候做這個事情。一般使用如下:
            ?WSADATA data;
            ?if ( WSAStartup(0x0202,&data) != 0 )
            ?{
            ??printf("error id: %d",GetLastError());
            ?}
            上面的GetLastError()是使用非常頻繁的函數(shù)。很多函數(shù)的返回值得意義都是表示函數(shù)執(zhí)行是否出錯。當winsock的函數(shù)發(fā)生錯誤時,用GetLastError()可以得到最后一次發(fā)生錯誤的錯誤號。在.Net中有個工具:查找錯誤,輸入錯誤號,得到錯誤信息。

            int gethostname(char* name,int namelen);
            獲取主機名。輸入?yún)?shù)為緩沖區(qū)地址和大小。
            struct hostent* FAR gethostbyname(const char* name);
            由主機名得到主機信息,可以用來解析域名。需要注意的是MSDN中的一段話“The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls”這說明了一般用法的時候,要把數(shù)據(jù)拷貝出來,看看下面代碼:
            ?char???name[255];
            ?PHOSTENT??phostinfo;
            ?PHOSTENT??phostinfo1;
            ?WSADATA data;
            ?struct in_addr** addrPtr;
            ?struct in_addr** addrPtr1;

            ?if ( WSAStartup(0x0202,&data) != 0 )
            ?{
            ??printf("error id: %d",GetLastError());
            ?}

            ?if( gethostname ( name, sizeof(name)) == 0)
            ?{
            ??printf("%s",name);
            ??if((phostinfo = gethostbyname(name)) != NULL)
            ??{
            ???for (addrPtr = (struct in_addr **)phostinfo->h_addr_list;*addrPtr;addrPtr++)
            ???{//顯示我機子的ip
            ????printf("%s\n", inet_ntoa(**addrPtr));
            ???}
            ??}
            ??else
            ???printf("error id : %d",GetLastError());
            ?}
            ?else
            ?{
            ??printf("error id :%d",GetLastError());
            ?}

            ?if((phostinfo1 = gethostbyname("bbs.whnet.edu.cn")) != NULL)
            ?{
            ??for (addrPtr1 = (struct in_addr **)phostinfo->h_addr_list;*addrPtr1;addrPtr1++)
            ??{//顯示bbs.whnet.edu.cn的ip:202.114.0.248
            ???printf("%s\n", inet_ntoa(**addrPtr1));
            ??}
            ?}
            ?else
            ??printf("error id : %d",GetLastError());

            ?for (addrPtr = (struct in_addr **)phostinfo->h_addr_list;*addrPtr;addrPtr++)
            ?{//在這里顯示的是bbs.whnet.edu.cn的ip:202.114.0.248,不是我機子的ip
            ??printf("%s\n", inet_ntoa(**addrPtr));
            ?}

            struct HOSTENT* FAR gethostbyaddr(const char* addr,int len,int type);
            根據(jù)ip地址得到主機信息。這里的ip必須是網(wǎng)絡(luò)字節(jié)順序的。示例代碼:
            ?PHOSTENT phostinfo2;
            ?unsigned long ip = inet_addr("127.0.0.1");
            ?if ( (phostinfo2 = gethostbyaddr((char*)&ip,sizeof(ip),AF_INET)) != NULL)
            ?{//顯示localhost
            ??printf("host name : %s",phostinfo2->h_name );
            ?}
            ?else
            ??printf("error id : %d",GetLastError());

            int getsockname(SOCKET s,struct sockaddr* name,int* namelen);
            這個函數(shù)用來的得到socket的本地地址,但是前提是socket必須已經(jīng)bind或者已經(jīng)是連接上的。如果socket不是面向連接的話,比如udp,那得socket上有數(shù)據(jù)才行。
            int getpeername(SOCKET s,struct sockaddr* name,int* namelen);
            這個函數(shù)用來得到socket那一邊上的地址,顯然socket必須得連接上?!癟he getpeername function can be used only on a connected socket. For datagram sockets, only the name of a peer specified in a previous connect call will be returned—any name specified by a previous sendto call will not be returned by getpeername.”

            至于bind,listen,accept,recv,send等后面學(xué)select模型的時候再仔細研究。



            Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=551760

            posted on 2006-04-20 17:27 楊粼波 閱讀(849) 評論(0)  編輯 收藏 引用 所屬分類: 網(wǎng)絡(luò)編程

            国产91色综合久久免费分享| 亚洲愉拍99热成人精品热久久| 2022年国产精品久久久久| 久久精品人人做人人爽电影蜜月| 国产精品一久久香蕉国产线看| 国产成人综合久久久久久| 日韩欧美亚洲综合久久影院Ds| 无码伊人66久久大杳蕉网站谷歌| 久久精品国产秦先生| 久久这里有精品| 国产福利电影一区二区三区久久久久成人精品综合 | 久久久久久av无码免费看大片| 久久久无码精品亚洲日韩蜜臀浪潮| 91精品国产91久久久久福利| 热久久国产欧美一区二区精品| 精品国产乱码久久久久久郑州公司 | 狠狠人妻久久久久久综合蜜桃 | 91精品国产91久久久久久| 狠狠色噜噜色狠狠狠综合久久 | 久久久久国产精品麻豆AR影院| 亚洲午夜久久久影院| 久久久噜噜噜久久中文字幕色伊伊| 午夜精品久久久久久中宇| 中文字幕无码久久精品青草 | 久久久久久亚洲精品无码| 国产精品久久午夜夜伦鲁鲁| 久久无码AV中文出轨人妻| 久久影视综合亚洲| 久久久精品波多野结衣| 品成人欧美大片久久国产欧美...| 97久久超碰成人精品网站| 久久亚洲精品国产精品| 18岁日韩内射颜射午夜久久成人| 污污内射久久一区二区欧美日韩| 999久久久国产精品| 狠狠色丁香久久婷婷综| 久久成人精品视频| 韩国三级中文字幕hd久久精品| 国产高潮久久免费观看| 欧美激情精品久久久久久久 | 精品久久久久久无码专区|