【高級網絡程序設計】Week2-1 Sockets

一、The Basics

1. Sockets

定義An abstraction of a network interface
應用

use the Socket API to create connections to remote computers

send data(bytes)

receive data(bytes)

2. Java network programming

the java network libraryimport java.net.*;
similar to reading and writing files

on a remote machine

receive/send data

二、Java I/O

1. I/O

原因

computer programs need to interact with the world

- bring in information?from an external source

- send out information to an external destination

interact 定義

Input/Output:

Input(Read): to bring in information

Output(Write): to send out information

Information

特點

anywhere;of any type

2. Streams

定義a connection to a source of data or to a destination for data (sometimes both)
作用Streams can represent any data, so a stream is a sequence of bytes that flow from a source to a destination

stream can carry data

byte streams, for machine-formatted data

? InputStream, OutputStream

? Writing and reading are very efficient.

– character streams (textual), for human-readable data ??Reader / Writer

? Require translation

應用
?
?read information from an input stream and write information to an output stream.
?A program can manage multiple streams simultaneously
流程
opening and closing a stream

Opening

? When you open a stream, you are making a connection to that external place.

? Once the connection is made, you forget about the external place and just use the stream

Closing

? A stream is an expensive resource.

There is a limit on the number of streams that you can have open at one time.

? You should not have more than one stream open on the same file.

? You must close a stream before you can open it again.?

using a stream

? Some streams can be used only for input, others only for output, others for both.

? Using a stream means doing input from it or output to it.

3. Using java I/O

read/write a text file

– involves creating multiple streams;

– the streams are connected to provide the required functionality;

– read from/write to text files require declaring and using the correct I/O streams.

writing to a Socket

?? The Socket object presents a stream to you (the programmer)

– You don’t need to worry about how the network sends bytes

– You just interact with the stream

? Java’s built-in multithreading:

– Handles multiple connections at once.

– E.g. a web server will create 1 thread for each request it receives

– So it can handle multiple clients in parallel

三、Port

1. IP addressing

作用

identifier:Identifying computers on a network

分類

Domain names: DNS (Domain Name System) form (www.qmul.ac.uk)

IP (Internet Protocol) address

- “dotted quad” format

-139.255.27.125, a 32-bit number

- java.net package:static InetAddress.getByName()

-?An object of type InetAddress that you can use to build a socket.

- 127.0.0.1 is for local machine.

DN maps to an IP address?

www.eecs.qmul.ac.uk -> 138.37.95.147

步驟:

- The Domain Name System (DNS) performs this mapping

- DNS servers handle lookups

- return the IP address that maps to a domain name

- send DNS queries to a DNS server

-?nslookup DN

//Find out your IP address?
import java.net.*;
public class IPFinder {?
? ? public static void main(String[] args) throws Exception {?
? ? ? ? String domainName = “www.qmul.ac.uk”;
? ? ? ? InetAddress a = InetAddress.getByName(domainName);?
? ? ? ? System.out.println(a);?
? ? }?
}

2. Basics of the client-server model

Network

allows two machines to connect and talk to each other.

? One machine has to stay and listen: server.

? The other machine makes requests: client.

? The client is trying to connect to the server. Once connected, there is a two way communication.

Testing programs with a network

? If your code is not working, check if both computers are online

? Open your terminal window: Type – ping www.eecs.qmul.ac.uk

Testing programs without a network

? A special address called localhost( 127.0.0.1. )

? Run both client and server on one machine (localhost)

??Producing a localhost:

InetAddress addr = InetAddress.getByName(null);? InetAddress.getByName("localhost");

InetAddress.getByName("127.0.0.1");

3. Port

原因

An IP address isn’t enough to identify a unique server:

Many services can exist on one machine

定義?A unique identifier for a particular service running on a machine.
應用

? When setting up a client or a server:

– Must choose a port.

– Both client and server agree to connect.

? The port is not a physical location in a machine, but a software abstraction.

常見System services reserve the use of ports 0 through 1023. ( Do not use them)
Usual choice for web proxy is port 8080
Usually represented as IP address: port. ——127.0.0.1:8080 (localhost:8080)

There are several standard ports that always get used for the same applications

80 for web servers (HTTP)

443 for encrypted web servers (HTTPS)

22 for secure shell (SSH)

20 and 21 for File Transfer Protocol (FTP)

25 for Simple Mail Transfer Protocol (SMTP)

Door is the IP address Letter boxes are the ports;

Allows us to talk to multiple people (services) in one house (computer)

四、Sockets

1. Sockets

原因

? When a client wants a service, it attempts the connection with the server by supplying the port number associated with the service.

? There are likely to be multiple clients waiting for the same service at the same time (e.g. web browsers wanting a web page).

? The server needs a way to distinguish between clients and keeping their communication separate.

– This is achieved by the use of sockets.

? The client creates a socket at its end of the communication link.

? The server, upon receiving the client’s initial request (on a particular port number), creates a new socket at its end, dedicated to the communication with that specific client.

2. Sockets and Java Sockets

定義

A socket is the software abstraction used to represent the “terminals” of a connection between two machines.
Socket class

? Server socket: a server is used to listen for incoming connections.

? Socket: a client is used to initiate a connection.

? Making a socket connection:

– ServerSocket returns a corresponding Socket

– via the accept() method

– through which communications will take place on the server side.

? Then it is a true Socket to Socket connection:

– May treat both ends the same way.?

InputStream and OutputStream

? Once you’ve created a socket, you can get access to its input and output streams

? To produce the corresponding InputStream and OutputStream objects from each Socket, use the methods:

– mySocket.getInputStream()

– mySocket.getOutputStream()

? Wrap inside buffers and formatting classes just like any other stream object
Creating a Socket

? Create a ServerSocket:

– Only need to give the port number.

– IP address is not necessary.

? Create a Socket:

– Give both the IP address and port number.

– Indicate where to connect.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/162314.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/162314.shtml
英文地址,請注明出處:http://en.pswp.cn/news/162314.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

pgsql常用命令總結

pgsql常用命令及相關總結 命令 命令登錄 psql -U postgres -h 127.0.0.1 -p 5432 -d vism查看所有數據庫:\l 進入某一數據庫:\c 數據庫名字 查看數據庫表:\dt 列出某一張表格的結構:\d 表名 查看某個表的所有數據:s…

學習筆記記錄

目錄 windows php一句話木馬 日志清理 DOS命令 查看用戶的SID 最高權限 常見的cmd命令 反彈shell PHPMYadmin mysql注入語句 wmic linux crontab創建隱藏后門 linux日志文件 knockd服務 ssh登錄 ssh隧道 本地轉發 遠程轉發 動態轉發 /proc: Centos 8 更…

CentOS 7 使用cJSON 庫

什么是JSON JSON是一種輕量級的數據交換格式,可讀性強、編寫簡單。鍵值對組合編寫規則,鍵名使用雙引號包裹,冒號:分隔符后面緊跟著數值,有兩種常用的數據類型是對象和數組。 對象:使用花括號{}包裹起來的…

【Rxjava詳解】(三)更好實踐異步請求

本文為更像demo的總結,在實際開發中,利用rxjava采取異步請求在一些簡單的單純請求數據上面,會顯得沒有必要,但rxjava提供的思路,在后期不論是增加功能,還是說整體代碼的工整度,都能感受到開發的…

補充:自動化測試高級應用之python多線程的使用-新方法

前段時間在網上學習多線程跑用例的時,發現一種更簡潔,優雅的使用多線程的方法,在此分享給大家。 閱讀本文前,請先閱讀前面寫的多線程跑用例的文章:【精選】第七章 第四節 自動化測試高級應用之python多線程的使用_add_test_img-CSDN博客 本文新的方法,對原有的run_al…

接口傳參數list的時候,items里面放個???????list

item里面放個list 先定義一個 list,循環add加入參數

java之switch case的用法

java之switch case的用法 Java中的switch語句是一種多路選擇結構,它允許一個變量在其值的多個可能選項之間進行選擇。這種結構可以替代一系列嵌套的if-else語句,使代碼更清晰和簡潔。 下面是switch語句的基本語法: switch (expression) { …

android keylayout鍵值適配

1、通過getevent打印查看當前keyevent數字對應事件和物理碼 2、dumpsys input 查看輸入事件對應的 KeyLayoutFile: /system/usr/keylayout/Vendor_6080_Product_8060.kl 3、通過物理碼修改鍵值映射,修改/system/usr/keylayout/目錄下的文件

CuratorFramework的blockUntilConnected方法

CuratorFramework是一個ZooKeeper客戶端庫,它提供了一些用于處理ZooKeeper連接和節點操作的高級API。其中,blockUntilConnected方法是一個阻塞方法,它會一直阻塞直到客戶端成功連接到ZooKeeper服務器。 具體來說,blockUntilConne…

(三)、基于 LangChain 實現大模型應用程序開發 | 模型鏈 Chains

😄 為什么我們需要Chains ? 鏈允許我們將多個組件組合在一起,以創建一個單一的、連貫的應用程序。鏈(Chains)通常將一個LLM(大語言模型)與提示結合在一起,使用這個構建塊&#xff0…

永久免費!N個excel表一鍵合并成一個表(excel表格合并技巧)

您是否還在用手工復制粘貼來將多個EXCEL或表的數據合并到一個表里?那就太麻煩,效率太低了,用金鳴表格文字識別的“表格合并”功能,可免費將N個excel文件或N個excel表一鍵合并到一個表里面,而且這個功能永久免費&#x…

【C++】特殊類設計 {不能被拷貝的類;只能在堆上創建的類;只能在棧上創建的類;不能被繼承的類;單例模式:懶漢模式,餓漢模式}

一、不能被拷貝的類 設計思路: 拷貝只會發生在兩個場景中:拷貝構造和賦值重載,因此想要讓一個類禁止拷貝,只需讓該類不能調用拷貝構造以及賦值重載即可。 C98方案: 將拷貝構造與賦值重載只聲明不定義,并…

FDG6306P PowerTrench? MOSFET P溝道 特點及其應用詳解

關于PowerTrench MOSFET? 它是一種MOS場效應晶體管,可以提高系統效率和功率密度。該技術采用了屏蔽柵極技術,可以減少開關損耗和導通損耗,從而提高了系統效率。此外,PowerTrench MOSFET還具有低導通電阻和高開關速度的…

三角洲雜志三角洲雜志社三角洲編輯部2023年第19期目錄

作家在線 李明聰 把寫作當成一種享受 李明聰; 2 頭條作品 冬天的童話 王排; 5-7 迎來春色換人間 王排; 8《三角洲》投稿:cnqikantg126.com 小說精選 鋼哥 曹茂炯; 9-25 重逢 莫艷陽; 26 散文現場 孩子,你相信光嗎? 趙…

前端js語音朗讀文本

<!DOCTYPE html> <html lang"zh"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>語音朗讀</title></head><body>&l…

如何滿足BMW EDI項目的PKT需求?

近期寶馬BMW&#xff08;以下簡稱BMW&#xff09;在其部分供應商之間試點推進PKT項目&#xff0c;BMW為什么要啟動 PKT 計劃呢&#xff1f; 業務系統全面升級統一全球所有寶馬工廠的流程 寶馬內部的物流供貨流程 近期BMW PKT需求主要針對其內部物流供貨流程展開&#xff1a; …

嵌入式開發--賽普拉斯cypress的鐵電存儲器FM25CL64B

嵌入式開發–賽普拉斯cypress的鐵電存儲器FM25CL64B 簡介 FM25CL64B是賽普拉斯cypress出品的一款鐵電存儲器&#xff0c;這種存儲器最大的優勢是可以像RAM一樣隨機存儲&#xff0c;和按字節寫入&#xff0c;也可以像ROM一樣掉電仍然可以保存數據&#xff0c;是一種相當優秀的…

Redis 持久化機制

client Redis[內存] --> 內存數據、磁盤數據----> 磁盤&#xff0c;Redis官方提供了兩種不同的持久化方案將內存中的數據存儲在硬盤中&#xff1a; 快照&#xff08;Snapshot&#xff09; AOF只追加日志文件。 1、快照&#xff08;Snapshot&#xff09; 1、快照的特點…

如何用CHAT解釋文章含義?

問CHAT&#xff1a;解釋“ 本身樂善好施&#xff0c;令名遠近共欽&#xff0c;待等二十左右&#xff0c;定有高親可攀&#xff1b;而且四德俱備&#xff0c;幫夫之緣亦有。主持家事不紊&#xff0c;上下亦無閑言。但四十交進&#xff0c;家內謹防口舌&#xff0c;須安家堂&…

分布式篇---第一篇

系列文章目錄 文章目錄 系列文章目錄前言一、分布式冪等性如何設計?二、簡單一次完整的 HTTP 請求所經歷的步驟?三、說說你對分布式事務的了解前言 前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到網站,這篇文章男女通用,…