網路程式設計Client Server模型

2021-10-08 18:07:36 字數 2239 閱讀 6951

網路程式設計的基本模型client/server模型,也就是兩個程序直接進行相互通訊,其中服務端提供配置資訊(繫結的ip位址和監聽埠),客戶端通過連線操作向服務端監聽的位址發起連線請求,通過三次握手建立連線,如果連線成功,則雙方即可進行通訊(網路套接字socket)

public classserverhandler implementsrunnable

@override

public

void

run()}

catch

(exception e)

finally

catch

(ioexception e)}if

(out != null)

catch

(exception e)}if

(socket != null)

catch

(ioexception e)

} socket = null;

}}

public classserver 

catch

(exception e)

finally

catch

(ioexception e)

} server = null;

}}

public classclient 

catch

(exception e)

finally

catch

(ioexception e)}if

(out != null)

catch

(exception e)}if

(socket != null)

catch

(ioexception e)

} socket = null;}}

}

採用執行緒池和任務佇列可以實現一種偽非同步的io通訊框架。

我們學過連線池的使用和佇列的使用,其實就是將客戶端的socket封裝成乙個task任務(實現runnable介面的類)然後投遞到執行緒池中去,配置相應的佇列進行實現

public

class

serverhandler

implements

runnable

@override

public

void

run()}

catch

(exception e)

finally

catch

(exception e1)}if

(out != null)

catch

(exception e2)}if

(socket != null)

catch

(exception e3)

}socket = null;}}

}

public

class

handlerexecutorpool

public

void

execute

(runnable task)

}

public

class

server

}catch

(exception e)

finally

catch

(exception e1)}if

(out != null)

catch

(exception e2)}if

(server != null)

catch

(exception e3)

}server = null;}}

}

public

class

client

catch

(exception e)

finally

catch

(exception e1)}if

(out != null)

catch

(exception e2)}if

(socket != null)

catch

(exception e3)

}socket = null;}}

}

python網路程式設計 TCP網路程式設計

tcp程式設計 客戶端 import socket 1 套接字 tcp socket socket.socket socket.af inet,socket.sock stream 2 建立鏈結 tcp socket.connect 172.27.35.1 8080 3 傳送資訊 tcp socke...

網路程式設計 網路基礎

1.物理層 提供建立 維護和拆除物理鏈路所需的機械 電氣 功能和規程的特性 提供有關在傳輸介質上傳輸非結構的位流及物理鏈路故障檢測指示。在這一層,資料還沒有被組織,僅作為原始的位流或電氣電壓處理,單位是位元。常用協議 eia tia rs 232 eia tia rs 449 v.35 rj 45 ...

網路程式設計 網路概述

應用層 常見的協議有http協議 ftp協議 傳輸層 常見協議有tcp udp協議。網路層 常見的協議有ip協議 icmp協議 igmp協議 鏈路層 常見的協議有arp協議 rarp協議。tcp 傳輸控制協議 是一種面向連線的 可靠的 基於位元組流的傳輸層通訊協議。http 超文字傳輸協議 是網際網...