39 網路程式設計

2021-07-04 15:57:39 字數 3906 閱讀 9835

http協議,hyper text transfer protocol(超文字傳輸協議)是用於從全球資訊網伺服器傳送超文字到本地瀏覽器的傳輸議,http是乙個應用層協議,由請求和響應構成,是乙個標準的客戶端伺服器模型。

c/s模式:client和server常常分別處在相距很遠的兩台計算機上,client程式的任務是將使用者的要求提交給server程式,再將server程式返回的結果以特定的形式顯示給使用者;server程式的任務是接收客戶程式提出的服務請求,進行相應的處理,再將結果返回給客戶程式

url,統一資源定位符,也被稱為**,網際網路上標準的資源**,乙個典型的url:

url的符語法: 協議://授權/路徑?查詢

協議:ftp://(檔案傳輸協議) http://(超文字傳輸協議) https://(安全超文字傳輸協議) file://(本地檔案協議)

url作為**字串包含很多請求引數,nsurl對**字串進行封裝,可以使用nsurl物件獲取相應的引數。absolutestring:

兩種請求方式get和post的相同點:都能給伺服器傳輸資料

不同點: 1)給伺服器傳輸資料的方式: get:通過**字串, post:通過data ) 2)傳輸資料的大⼩: get:**字串最多255位元組。 post:使用nsdata,容量超過1g 3)安全性: get:所有傳輸給伺服器的資料,顯示在⺴址裡,類似於密碼的明⽂輸入,直接可見。post:資料被轉成nsdata(二進位制資料),類似於密碼的密文輸入,無法直接讀取。

nsurlresponse:根據網路請求協議,封裝了伺服器返回資料的部分資訊。伺服器響應網路請求後,返回資料的部分資訊及資料本身。expectedcontentlength !!suggestedfilename !!mimetype !!textencodingname !!url

nshttpurlresponse:nsurlresponse的子類,封裝有關http協議的響應資訊。儲存了http協議的響應頭資訊,狀態碼。allheaderfields ,statuscode如果網路請求時使用http協議,nsurlresponse就是 nshttpurlresponse

伺服器端php介面的簡單實現和介紹

1) 網路請求方式分為:get、post,是由伺服器的實現方式決定,分為:get,post和get或post

2) 伺服器端php簡單介面:php中使用不同的函式獲取資料,支援不同的請求方式 : get請求:使用 ge

t["k

ey"]

獲取資料

**中的

資料,p

ost請

求:使⽤

_post[「key」] 獲取上傳data中的資料 , get、post請求:使用 $_request[「key」] 獲取資料

在ios平台使用nsurl、nsurlrequest等物件完成與php介面的互動 , 網路請求的步驟:1、nsurl;2、nsurlrequest;3、nsurlconnection;4、處理error或者返回資料

同步非同步連線

//同步連線:--->程式容易出現卡死現象

+ (nsdata *)sendsynchronousrequest:(nsurlrequest *)request returningresponse:(nsurlresponse **)response error:(nserror **)error;

//非同步連線有兩種實現方式:設定**接收資料和實現block:--->等待資料返回

//設定nsurlconnection**,實現相應的**方法:開始響應接收資料、接收資料、成功、失敗

- (void)connection:(nsurlconnection *)connection didreceiveresponse: (nsurlresponse *)response

- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data

- (void)connectiondidfinishloading:(nsurlconnection *)connection

- (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error

//非同步連線(block)

+ (void)sendasynchronousrequest:(nsurlrequest*)request queue:(nsoperationqueue*)queue completionhandler:(void (^)(nsurlresponse* response, nsdata* data, nserror* connectionerror))handler

viewcontroller.m檔案

#import "viewcontroller.h"

@inte***ce

viewcontroller ()

@property(nonatomic,retain)uiimageview *imageview;

@property(nonatomic,retain)nsmutabledata *data;

- (ibaction)synget:(id)sender;

- (ibaction)synpost:(id)sender;

- (ibaction)asynget:(id)sender;

- (ibaction)asynpost:(id)sender;

- (ibaction)blockget:(id)sender;

@end

@implementation

viewcontroller

- (void)dealloc

- (void)viewdidload

#warning 網路請求總結步驟:1.根據**的字串,建立乙個nsurl物件 2.根據這個url物件建立乙個請求 3.傳送請求,然後過去請求物件 同步和非同步的區別就是在請求的方法選用有差別,其他都一樣

#warning post就是個get請求多乙個,需要給request新增乙個body

1.get同步請求

- (ibaction)synget:(id)sender 

}

2.post同步請求

- (ibaction)synpost:(id)sender 

}

3.get非同步請求

- (ibaction)asynget:(id)sender 

#pragma mark 同步和非同步get請求在步驟上完全相同,只是在第三步同步使用的是sendsyn的方法,非同步使用的是**方法,非同步是基於同步進行的操作

- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response

- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data

- (void)connectiondidfinishloading:(nsurlconnection *)connection

4.post非同步請求

- (ibaction)asynpost:(id)sender 

}];}

5.get非同步通過block方式

- (ibaction)blockget:(id)sender ];

}

39 網路IO模型

我們這裡研究的io模型都是針對網路io的 blocking io 阻塞io nonblocking io 非阻塞io io multiplexing io多路復用 signal driven io 訊號驅動io asynchronous io 非同步io 由signal driven io 訊號驅動...

2018 4 15 網路程式設計

單純的客戶端服務端有個侷限性,a客戶端連線上以後,未處理完之前,b客戶端連線只有等待 所以服務端最好將每個客戶端封裝到乙個執行緒中 class picthread implements runnable public void run outputstream out s.getoutputstre...

013 網路程式設計

ip 位址就是標識網路中裝置的乙個位址,好比現實生活中的家庭位址。ipv4 是目前使用的ip位址 ipv6 是未來使用的ip位址 ipv4 是由點分十進位制組成 ipv6 是由冒號十六進製制組成 ip 位址的作用是標識網路中唯一的一台裝置的,也就是說通過ip位址能夠找到網路中某台裝置。說明 ifco...