網路 網路套接字基本概念!!

2021-08-19 11:32:36 字數 4718 閱讀 5387

ip

埠號

在每一台主機裡面執行的程序不是唯一的,所以在程序使用網路資源的時候就需要使用一種標記來標記同乙個主機裡面的使用網路資源的程序,即埠號。

tcp協議(transmission control protocol 傳輸控制協議)

udp協議p(user datagram protocol 使用者資料報協議)

#include

uint32_t htonl(uint32_t hostlong);

uint16_t htons(uint16_t hostshort);

uint32_t ntohl(uint32_t netlong);

uint16_t ntohs(uint16_t netshort);

注:n表示network ,h表示host ,l表示long ,s表示short。

socket常見介面

建立socket檔案描述符

#include /* see notes */

#include

int socket(int domain, int type, int protocol);

繫結伺服器ip和埠

#include /* see notes */

#include

int bind(int sockfd, const

struct sockaddr *addr,

socklen_t addrlen);

監聽socket

#include /* see notes */

#include

intlisten(int sockfd, int backlog);

用於接受請求

#include /* see notes */

#include

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

連線客戶端和伺服器

#include /* see notes */

#include

int connect(int sockfd, const

struct sockaddr *addr,

socklen_t addrlen);

sockaddr結構

通常使用的是sockaddr_in結構體,裡面定義了sin_family來標記型別,sin_port儲存埠號,sin_addr裡面的s_addr來儲存ip位址。

注:使用的時候需要強制轉換成sockaddr*

位址轉換函式

字串轉ip函式

#include

#include

#include

int inet_aton(const

char *cp, struct in_addr *inp);

in_addr_t inet_addr(const

char *cp);

in_addr_t inet_network(const

char *cp);

ip轉字串函式

char *inet_ntoa(struct in_addr in);

struct in_addr inet_makeaddr(int net, int host);

in_addr_t inet_lnaof(struct in_addr in);

in_addr_t inet_netof(struct in_addr in);

簡單udp程式server.c

#include

#include

#include

#include

#include

#include

#include

int main(int argc,char* argv)

struct sockaddr_in local;

local.sin_family = af_inet;

local.sin_port = htons(atoi(argv[2]));

local.sin_addr.s_addr = inet_addr(argv[1]);

if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)

char buf[1024];

struct sockaddr_in client;

while(1)

}return

0;}

client.c

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc,char* argv)

struct sockaddr_in server;

server.sin_family = af_inet;

server.sin_port = htons(atoi(argv[2]));

server.sin_addr.s_addr = inet_addr(argv[1]);

struct sockaddr_in peer;

char buf[1024];

while(1)}}

return

0;}

簡單tcp程式server.c

#include

#include

#include

#include

#include

#include

#include

int main(int argc,char* argv)

struct sockaddr_in local;

local.sin_family = af_inet;

local.sin_port = htons(atoi(argv[2]));

local.sin_addr.s_addr = inet_addr(argv[1]);

if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)

if(listen(sock,10) < 0)

struct sockaddr_in client;

for(;;)

char buf_ip[16];

memset(buf_ip,'\0',sizeof(buf_ip));

inet_ntop(af_inet,&client.sin_addr,buf_ip,sizeof(buf_ip));

printf("get a connet :ip-->%s port-->%d\n",buf_ip,ntohs(client.sin_port));

while(1)

return

0; }

}

client.c

#include

#include

#include

#include

#include

#include

#include

int main(int argc,char* argv)

struct sockaddr_in peer;

peer.sin_family = af_inet;

peer.sin_port = htons(atoi(argv[2]));

peer.sin_addr.s_addr = inet_addr(argv[1]);

socklen_t len = sizeof(peer);

int ret = connect(sock,(struct sockaddr*)&peer,len);

if(ret < 0)

printf("connet success\n");

char buf[1024];

while(1)

buf[s-1] = '\0';

write(sock,buf,strlen(buf));

if(strcmp(buf,"quit") == 0)

read(sock,buf,sizeof(buf));

printf("server:# %s\n",buf);

}close(sock);

return

0;}

網路基本概念

網路 網路就是幾台計算機主機或者網路印表機之類的藉口裝置,通過網線或者無線網路技術將這些主機或者裝置連線起來,使得資料可以通過網路媒介來傳輸的一種方式 埠 網路聯機是雙向的,客戶端想連線到主機端,主機端必須啟動乙個埠進行監聽 主機端監聽 listen 主機所啟用的埠其實是某些網路服務所啟動的,如ww...

網路基本概念

網路基本概念 所謂計算機網路,就是把分布在不同地理區域的計算機與專門的外部裝置用通訊線路互聯成乙個規模大 功能強的網路系統。從而使更多的計算機可以方便的互相傳遞資訊,共享 硬體 軟體 資料資訊等資源。計算機網路是現代通訊技術與計算機技術相結合的產物。提供了資源共享 資訊傳輸與集中處理 均衡負荷與分布...

網路基本概念

get請求 在請求url後面以?的形式加上給伺服器的引數,多個引數之間用 隔開 由於瀏覽器和伺服器對url長度有限制,因此在url後面附帶的引數是有限制的,通常不能超過1kb post請求 發給伺服器的引數全部放在請求體中 理論上,post傳遞的資料量沒有限制 具體還得看伺服器的處理能 get和po...