当前位置: 首页 > news >正文

【高级网络程序设计】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.

相关文章:

【高级网络程序设计】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!作者:不渴望力量的哈士奇(哈哥),十余年工作经验, 从事过全栈研发、产品经理等工作&#xf…...

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数据库&#xff0c;报以上错误。 在本地可以ping通阿里云公网地址&am…...

XC3320 离线式、无电感交流输入线性稳压器 可替代KP3310 固定5V输出电压

XC3320 是一款紧凑型无电感设计的离线式线性稳压器。XC3320 可获得 5V输出电压。XC3320 是一种简单可靠的获得偏置供电的离线式电源解决方案。XC3320 集成了 650V 功率 MOSFET&#xff0c;启动控制电路,VDD 电压控制电路,AC 交流信号同步检测电路&#xff0c;低压差稳压器等。该…...

导购APP、淘客查券机器人与淘客系统:全面对比与选择

导购APP、淘客机器人与淘客系统&#xff1a;全面对比与选择 在互联网购物的时代&#xff0c;导购APP、淘客机器人和微赚淘客系统成为了消费者们的三大重要工具。它们各具优势&#xff0c;但也存在一些问题。本文将为您详细对比这三种工具&#xff0c;帮助您在购物时做出最合适…...

飞翔的鸟游戏

一.准备工作 首先创建一个新的Java项目命名为“飞翔的鸟”&#xff0c;并在src中创建一个包命名为“com.qiku.bird"&#xff0c;在这个包内分别创建4个类命名为“Bird”、“BirdGame”、“Column”、“Ground”&#xff0c;并向需要的图片素材导入到包内。 二.代码呈现 pa…...

【SpringCloud】为什么选择微服务?

一般的平台会遇到的问题&#xff1a; 服务配置复杂。基础服务多&#xff0c;服务的资源配置复杂&#xff0c;传统方式管理服务复杂 服务之间调用复杂。检索服务、用户中心服务等&#xff0c;服务之间的调用复杂&#xff0c;依赖多 服务监控难度大。服务比较多&#xff0c;…...

基于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/中…...

干货分享:好用的两款封面制作工具

无论你是图文博主还是视频博主&#xff0c;做封面都是必不可少的。关于做封面的产品&#xff0c;我推荐两个&#xff1a;如果你使用手机&#xff0c;我推荐使用醒图&#xff1b;如果你使用电脑&#xff0c;我则推荐稿定设计。 01 醒图 为什么醒图如此出色呢&#xff1f; 首先…...

模版模式 设计模式

设计模式 总目录 https://preparedata.blog.csdn.net/article/details/134512591 文章目录 设计模式 总目录一、案例二、抽象类模版 AbstractOrderTemplate&#xff08;顶层的订单抽象类&#xff09;三、执行模版的实现类3.1 默认执行模版 DefaultOrder3.2 其他执行模版 Simlp…...

Hermes Agent 深度解析:开源自进化 AI 智能体的架构革命

标签&#xff1a;Hermes Agent 自主AI智能体 Nous Research 持久记忆 MCP协议 AI Agent架构 摘要&#xff1a;本文深入剖析 Hermes Agent 的模块化架构、自进化学习机制与企业级部署方案&#xff0c;结合 DeepSeek V4 与 GPT-5.5 的最新进展&#xff0c;为开发者提供完整的 AI …...

3步实现网页到Figma设计稿的终极转换指南:打破设计与开发壁垒

3步实现网页到Figma设计稿的终极转换指南&#xff1a;打破设计与开发壁垒 【免费下载链接】figma-html Convert any website to editable Figma designs 项目地址: https://gitcode.com/gh_mirrors/fi/figma-html 你是否曾遇到过这样的困境&#xff1a;看到一款精美的网…...

Advanced React APIs 状态优化:10个提升应用性能的关键技巧

Advanced React APIs 状态优化&#xff1a;10个提升应用性能的关键技巧 【免费下载链接】advanced-react-apis Learn Advanced React Hooks workshop 项目地址: https://gitcode.com/gh_mirrors/ad/advanced-react-apis 在React开发中&#xff0c;随着应用规模增长&…...

像素史诗·智识终端Dify低代码平台集成:快速构建AI工作流应用

像素史诗智识终端Dify低代码平台集成&#xff1a;快速构建AI工作流应用 1. 引言&#xff1a;低代码时代的AI应用开发 想象一下&#xff0c;你是一家电商公司的产品经理&#xff0c;需要快速搭建一个能自动回答客户问题的智能客服系统。传统开发方式可能需要组建技术团队、购买…...

马斯克五步法实战:用Notion和飞书搭建你的个人效率系统(附模板)

马斯克五步法实战&#xff1a;用Notion和飞书搭建你的个人效率系统&#xff08;附模板&#xff09; 在信息爆炸的时代&#xff0c;个人知识管理和团队协作效率成为职场竞争力的关键分水岭。埃隆马斯克创立的五步工作法&#xff08;需求验证→流程简化→持续优化→快速迭代→全面…...

5个简单步骤解决Windows热键冲突:热键侦探让你告别按键失灵烦恼

5个简单步骤解决Windows热键冲突&#xff1a;热键侦探让你告别按键失灵烦恼 【免费下载链接】hotkey-detective A small program for investigating stolen key combinations under Windows 7 and later. 项目地址: https://gitcode.com/gh_mirrors/ho/hotkey-detective …...

频谱仪进阶功能完全指南:从窄脉冲测量到非线性测试

这不是一篇入门帖。如果你已经会看谱线、会测功率,但对窄脉冲该怎么测、相位噪声的底噪从哪来、TOI 和 ACPR 之间是什么关系仍存疑问,这篇文章就是为你准备的。全文聚焦于频谱仪的进阶功能——即从脉冲测量、Zero Span、相位噪声、噪声系数,到非线性测试与通信指标的综合应用…...

2026智造进化论:从人工排程到AI智能排产,制造业生产模式正在如何变革?实在Agent技术解决方案

站在2026年4月的时点回望&#xff0c;全球制造业正经历一场由“确定性逻辑”向“预测性逻辑”的范式跃迁。 传统依赖计划员个人经验、基于Excel或静态MES系统的排产模式&#xff0c;在多品种、小批量、高频插单的复杂市场环境下已显出颓势。 AI智能排产不再仅仅是一个算法插件&…...

05华夏之光永存・开源:黄大年茶思屋榜文解法「23期 5题」 【分布式收发机设计专项完整解法】

05华夏之光永存・开源&#xff1a;黄大年茶思屋榜文解法「23期 5题」 【分布式收发机设计专项完整解法】 一、摘要 分布式收发机设计与低秩/稀疏优化赛道&#xff0c;全球现代工程技术已触达绝对性能天花板。传统集中式均衡、单模块预编码、固定流量分配的技术框架&#xff0c;…...

专业高考美术如何拿高分?拆解历年教学成果背后的质检工序

美术生的高分作品&#xff0c;往往是“质检”出来的很多家长认为艺术创作全凭感觉&#xff0c;但在高考美术的竞技场上&#xff0c;高分卷其实是高度标准化的产物。一份出色的历年教学成果&#xff0c;核心不在于学生画了多少张&#xff0c;而在于每一张画经历了怎样的“质检”…...