socket網路編程第一講,socket第一講

來源:互聯網
上載者:User

socket網路編程第一講,socket第一講

先總結一下求職面試必看的幾本書:《編程之美》、《劍指offer》、《Unix環境編程》、《Unix網路編程》、《POSIX多線程編程》,突然感覺心裡很踏實,不再像以前迷茫,終於知道電腦專業畢業的我到底能幹什麼,既然這樣就認認真真的學習吧,爭取拿到bat的實習offer,加油。

 

網路位元組序:

由於不同的電腦系統採用不同的位元組序儲存資料,同樣一個4位元組的32位整數,在記憶體中儲存的方式就不同. 位元組序分為小尾位元組序(Little Endian)和大尾位元組序(Big Endian), Intel處理器大多數使用小尾位元組序, Motorola處理器大多數使用大尾(Big Endian)位元組序;

 

TCP/IP各層協議將位元組序定義為大尾,因此TCP/IP協議中使用的位元組序通常稱之為網路位元組序。

 

1、將IP轉換成16進位數

/*************************************************************************

> File Name: my_atoh.c

> Author: Comst

> Mail:750145240@qq.com 

> Created Time: Sat 07 Feb 2015 04:56:54 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<string.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#define IP "180.97.33.107"// B461216B

int main(int argc, char* argv[])

{

 

struct in_addr addr ;

memset(&addr, 0, sizeof(addr));

inet_aton(IP, &addr);

memset(&addr, 0, sizeof(addr));

addr.s_addr = inet_addr(IP);

char* pstr = inet_ntoa(addr);

return 0 ;

}

2、查看網域名稱的詳細資料

/*************************************************************************

> File Name: my_host.c

> Author: Comst

> Mail:750145240@qq.com 

> Created Time: Sat 07 Feb 2015 05:23:46 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<netdb.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

int main(int argc, char* argv[])

{

struct hostent* pent ;

int i ;

pent = gethostbyname(argv[1]) ;

if(pent == NULL)

{

perror("host");

}

printf("h_name: %s\n", pent ->h_name);

for(i = 0; pent ->h_aliases[i] != NULL; i ++)

{

printf("aliase %d: %s\n", i, pent ->h_aliases[i]);

}

printf("addr_type: %d, len: %d\n", pent ->h_addrtype, pent ->h_length );

for(i = 0; pent ->h_addr_list[i] != NULL; i ++)

{

printf("ip %d: %s\n ", i, inet_ntoa( *(struct in_addr*)pent ->h_addr_list[i]) );

}

 

return 0 ;

}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.