【高级网络程序设计】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 library | import 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 |
![]() | |
|
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. |
相关文章:
【高级网络程序设计】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…...
quickapp_快应用_requestHeader
和客户端相同,在进行请求交互中,后端会需要获取当前设备信息,此时需要使用应用上下文app与设备信息 应用版本号 const app require(system.app)app.getInfo().versionName // versionName:应用版本名称 (manifest.json中versio…...
FPGA----ZCU106使用petalinux 2019.1的第一个app开发
1、petalinux在zcu106上的构建参见前文 FPGA----ZCU106使用petalinux 2019.1(全网最详)-CSDN博客文章浏览阅读31次。本文完成了Vivado 2019.1版本下的基于ZCU106的全部linux系统移植https://blog.csdn.net/qq_37912811/article/details/1345197352、我们…...
华为ac+fit漫游配置案例
Ap漫游配置: 其它配置上面一样,ap管理dhcp和业务dhcp全在汇聚交换机 R1: interface GigabitEthernet0/0/0 ip address 11.1.1.1 255.255.255.0 ip route-static 12.2.2.0 255.255.255.0 11.1.1.2 ip route-static 192.168.0.0 255.255.0.0 11.1.1.2 lsw1: vlan batch 100 200…...
Jenkins 配置节点交换内存
查看交换内存 free -hswapon -s创建swap文件 dd if/dev/zero of/mnt/swap bs1M count1024启用交换文件 设置权限 chmod 600 /mnt/swap设置为交换空间 mkswap /mnt/swap启用交换 swapon /mnt/swap设置用户组 chown root:root /mnt/swap查看 swapon -s重启系统也能生效还需要修…...
二百零七、Flume——Flume实时采集5分钟频率的Kafka数据直接写入ODS层表的HDFS文件路径下
一、目的 在离线数仓中,需要用Flume去采集Kafka中的数据,然后写入HDFS中。 由于每种数据类型的频率、数据大小、数据规模不同,因此每种数据的采集需要不同的Flume配置文件。玩了几天Flume,感觉Flume的使用难点就是配置文件 二、…...
【实验】配置用户自动获取IPv6地址的案例
【赠送】IT技术视频教程,白拿不谢!思科、华为、红帽、数据库、云计算等等编辑https://xmws-it.blog.csdn.net/article/details/117297837?spm1001.2014.3001.5502https://xmws-it.blog.csdn.net/article/details/117297837?spm1001.2014.3001.5502【…...
手撕A*算法(详解A*算法)
A*算法原理 全局路径规划算法,根据给定的起点和终点在全局地图上进行总体路径规划。 导航中使用A*算法计算出机器人到目标位置的最优路线,一般作为规划的参考路线 // 定义地图上的点 struct Point {int x,y; // 栅格行列Point(int x, int y):x(x),y(y){…...
1688API如何获取商品详情信息(关键词搜索商品列表),1688API接口开发系列
1688商品详情接口是指1688平台提供的API接口,用于获取商品详情信息。通过该接口,您可以获取到商品的详细信息,包括商品标题、价格、库存、描述、图片等。 要使用1688商品详情接口,您需要先申请1688的API权限,并获取ac…...
〖大前端 - 基础入门三大核心之JS篇㊶〗- DOM事件传播和事件监听方法addEventListener()
说明:该文属于 大前端全栈架构白宝书专栏,目前阶段免费,如需要项目实战或者是体系化资源,文末名片加V!作者:不渴望力量的哈士奇(哈哥),十余年工作经验, 从事过全栈研发、产品经理等工作…...
Cartographer实现双雷达建图
Urdf修改 <?xml version="1.0" ?> <robot name="robot"><link name="base_link" /><link name="laser_1" /><link name="laser_2" /><link name="laser_link" /><join…...
(离散数学)主析取范式
...
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Connect timed out. 在本地IDEA中和DBeaver中连接阿里云上MySQL数据库,报以上错误。 在本地可以ping通阿里云公网地址&am…...
XC3320 离线式、无电感交流输入线性稳压器 可替代KP3310 固定5V输出电压
XC3320 是一款紧凑型无电感设计的离线式线性稳压器。XC3320 可获得 5V输出电压。XC3320 是一种简单可靠的获得偏置供电的离线式电源解决方案。XC3320 集成了 650V 功率 MOSFET,启动控制电路,VDD 电压控制电路,AC 交流信号同步检测电路,低压差稳压器等。该…...
导购APP、淘客查券机器人与淘客系统:全面对比与选择
导购APP、淘客机器人与淘客系统:全面对比与选择 在互联网购物的时代,导购APP、淘客机器人和微赚淘客系统成为了消费者们的三大重要工具。它们各具优势,但也存在一些问题。本文将为您详细对比这三种工具,帮助您在购物时做出最合适…...
飞翔的鸟游戏
一.准备工作 首先创建一个新的Java项目命名为“飞翔的鸟”,并在src中创建一个包命名为“com.qiku.bird",在这个包内分别创建4个类命名为“Bird”、“BirdGame”、“Column”、“Ground”,并向需要的图片素材导入到包内。 二.代码呈现 pa…...
【SpringCloud】为什么选择微服务?
一般的平台会遇到的问题: 服务配置复杂。基础服务多,服务的资源配置复杂,传统方式管理服务复杂 服务之间调用复杂。检索服务、用户中心服务等,服务之间的调用复杂,依赖多 服务监控难度大。服务比较多,…...
基于Python实现汽车销售数据可视化+预测【500010086.1】
导入模块 import numpy as np import pandas as pd from pylab import mpl import plotly.express as px import matplotlib.pyplot as plt import seaborn as sns设置全局字体 plt.rcParams[font.sans-serif][kaiti]获取数据 total_sales_df pd.read_excel(r"./data/中…...
干货分享:好用的两款封面制作工具
无论你是图文博主还是视频博主,做封面都是必不可少的。关于做封面的产品,我推荐两个:如果你使用手机,我推荐使用醒图;如果你使用电脑,我则推荐稿定设计。 01 醒图 为什么醒图如此出色呢? 首先…...
模版模式 设计模式
设计模式 总目录 https://preparedata.blog.csdn.net/article/details/134512591 文章目录 设计模式 总目录一、案例二、抽象类模版 AbstractOrderTemplate(顶层的订单抽象类)三、执行模版的实现类3.1 默认执行模版 DefaultOrder3.2 其他执行模版 Simlp…...
铭豹扩展坞 USB转网口 突然无法识别解决方法
当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...
【磁盘】每天掌握一个Linux命令 - iostat
目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat(I/O Statistics)是Linux系统下用于监视系统输入输出设备和CPU使…...
Robots.txt 文件
什么是robots.txt? robots.txt 是一个位于网站根目录下的文本文件(如:https://example.com/robots.txt),它用于指导网络爬虫(如搜索引擎的蜘蛛程序)如何抓取该网站的内容。这个文件遵循 Robots…...
AGain DB和倍数增益的关系
我在设置一款索尼CMOS芯片时,Again增益0db变化为6DB,画面的变化只有2倍DN的增益,比如10变为20。 这与dB和线性增益的关系以及传感器处理流程有关。以下是具体原因分析: 1. dB与线性增益的换算关系 6dB对应的理论线性增益应为&…...
Razor编程中@Html的方法使用大全
文章目录 1. 基础HTML辅助方法1.1 Html.ActionLink()1.2 Html.RouteLink()1.3 Html.Display() / Html.DisplayFor()1.4 Html.Editor() / Html.EditorFor()1.5 Html.Label() / Html.LabelFor()1.6 Html.TextBox() / Html.TextBoxFor() 2. 表单相关辅助方法2.1 Html.BeginForm() …...
vue3 daterange正则踩坑
<el-form-item label"空置时间" prop"vacantTime"> <el-date-picker v-model"form.vacantTime" type"daterange" start-placeholder"开始日期" end-placeholder"结束日期" clearable :editable"fal…...
0x-3-Oracle 23 ai-sqlcl 25.1 集成安装-配置和优化
是不是受够了安装了oracle database之后sqlplus的简陋,无法删除无法上下翻页的苦恼。 可以安装readline和rlwrap插件的话,配置.bahs_profile后也能解决上下翻页这些,但是很多生产环境无法安装rpm包。 oracle提供了sqlcl免费许可,…...
《Offer来了:Java面试核心知识点精讲》大纲
文章目录 一、《Offer来了:Java面试核心知识点精讲》的典型大纲框架Java基础并发编程JVM原理数据库与缓存分布式架构系统设计二、《Offer来了:Java面试核心知识点精讲(原理篇)》技术文章大纲核心主题:Java基础原理与面试高频考点Java虚拟机(JVM)原理Java并发编程原理Jav…...
医疗AI模型可解释性编程研究:基于SHAP、LIME与Anchor
1 医疗树模型与可解释人工智能基础 医疗领域的人工智能应用正迅速从理论研究转向临床实践,在这一过程中,模型可解释性已成为确保AI系统被医疗专业人员接受和信任的关键因素。基于树模型的集成算法(如RandomForest、XGBoost、LightGBM)因其卓越的预测性能和相对良好的解释性…...


