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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/188378.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

面试:双线程交替打印奇偶数

代码如下: package practice1;/*** 0-100的奇数偶数打印* 1、通过对象的wait和notify进行线程阻塞* 2、通过对num%2的结果进行奇数偶数的判断输出**/ public class JiOuOne {private static volatile int num 0;private static final int max 100;public static …

【Docker】从零开始:12.容器数据卷

【Docker】从零开始:12.容器数据卷 1.什么是容器数据库卷2.数据的覆盖问题3.为什么要用数据卷4.Docker提供了两种卷:5.两种卷的区别6.bind mount7.Docker managed volumevolume 语法volume 操作参数 1.什么是容器数据库卷 卷 就是目录或文件&#xff0c…

js粒子效果(一)

效果: 代码: <!doctype html> <html> <head><meta charset"utf-8"><title>HTML5鼠标经过粒子散开动画特效</title><style>html, body {position: absolute;overflow: hidden;margin: 0;padding: 0;width: 100%;height: 1…

本地部署 ComfyUI

本地部署 ComfyUI ComfyUI 介绍ComfyUI Github 地址部署 ComfyUI配置模型地址 or 下载模型启动 ComfyUI访问 ComfyUI使用技巧页面底部显示图片预览改变连接线的格式配置 prompt 自动补全 安装 ComfyUI-Manager安装 AIGODLIKE-COMFYUI-TRANSLATION安装 ComfyUI-Custom-Scripts安…

【 拓扑排序】

文章目录 拓扑排序AOV-网拓扑排序的方法拓扑排序的一个重要应用&#xff1a;拓扑排序的算法 拓扑排序 AOV-网 无环的有向图称作有向无环图。 这种用顶点表示活动&#xff0c;用弧表示活动间的优先关系的有向图称为以顶点为活动的网&#xff08;Activity On Vertex Network&am…

centos7搭建ftp服务

一、安装 yum -y install vsftpd vi /etc/vsftpd/vsftpd.conf二、编辑配置文件 /etc/vsftpd/vsftpd.conf 内容如下 #是否允许匿名&#xff0c;默认no anonymous_enableNO#这个设定值必须要为YES 时&#xff0c;在/etc/passwd内的账号才能以实体用户的方式登入我们的vsftpd主机…

运行软件报错找不到vcruntime140_1.dll无法继续执行代码如何解决?-常见问题

关于vcruntime140_1.dll丢失的6个解决方法。在我们使用电脑的过程中&#xff0c;有时候会遇到一些错误提示&#xff0c;其中之一就是“vcruntime140_1.dll丢失”。那么&#xff0c;究竟什么是vcruntime140_1.dll文件呢&#xff1f;又是什么原因导致了它的丢失&#xff1f;接下来…

软件测试 | MySQL 唯一约束详解

&#x1f4e2;专注于分享软件测试干货内容&#xff0c;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;交流讨论&#xff1a;欢迎加入我们一起学习&#xff01;&#x1f4e2;资源分享&#xff1a;耗时200小时精选的「软件测试」资…

DBeaver连接Oracle时报错:Undefined Error

连接信息检查了很多遍&#xff0c;应该是没问题的&#xff0c;而且驱动也正常下载了&#xff0c;但是就是连不上。 找了好久&#xff0c;终于找到一个可用的方式了&#xff0c;记录一下。 在安装目录修改dbeave.ini文件&#xff0c;最后一行添加 -Duser.nameTest。重启就可以…

Python基础语法之判断语句

1.布尔类型和比较运算符 布尔类型&#xff1a;数字类型的一种。 比较运算符&#xff1a; > < > < ! 2.if语句基本格式 if 要判断的条件&#xff1a; 条件成立&#xff0c;即做~ 例子&#xff1a; 注意&#xff1a;格式上冒号和缩进 3.if else组合…

每日一题(LeetCode)----链表--链表中的下一个更大节点

每日一题(LeetCode)----链表–链表中的下一个更大节点 1.题目&#xff08;1019. 链表中的下一个更大节点&#xff09; 给定一个长度为 n 的链表 head 对于列表中的每个节点&#xff0c;查找下一个 更大节点 的值。也就是说&#xff0c;对于每个节点&#xff0c;找到它旁边的第…

基于单片机压力传感器MPX4115检测-报警系统proteus仿真+源程序

一、系统方案 1、本设计采用这51单片机作为主控器。 2、MPX4115采集压力值、DS18B20采集温度值送到液晶1602显示。 3、按键设置报警值。 4、蜂鸣器报警。 二、硬件设计 原理图如下&#xff1a; 三、单片机软件设计 1、首先是系统初始化 /*********************************…

【小沐学写作】原型设计工具汇总(Axure RP)

文章目录 1、简介2、Axure RP2.1 工具简介2.2 工具特点2.2.1 互动事件2.2.2 条件逻辑2.2.4 工作表格2.2.5 多状态容器2.2.6 数据驱动接口2.2.7 自适应视图2.2.8 流程图 2.3 工具安装2.3.1 安装2.3.2 运行 2.4 使用费用2.5 工具体验2.5.1 登陆框制作 3、其他3.1 Figma3.2 Adobe …

原生JS实现计算器(内含源码)

前言 本文讲解了JavaScript如何在一小时内实现一个简易计算器&#xff0c;这里最大的亮点就在于&#xff0c;我在JS中只用了一个事件&#xff0c;就实现了计算器的效果和功能&#xff0c;那么好文本正式开始。 布局和样式流程 首先是HTMLCSS结构&#xff1a;这里主要用到的…

c语言-字符函数和字符串函数详解

文章目录 1. 字符分类函数2. 字符转换函数3. strlen的使用和模拟实现4. strcpy的使用和模拟实现5. strncpy函数的使用6. strcat的使用和模拟实现7. strncat函数的使用8. strcmp的使用和模拟实现9. strncmp函数的使用10. strstr的使用和模拟实现11. strtok函数的使用12. strerro…

CTA-GAN:基于生成对抗性网络的主动脉和颈动脉非集中CT血管造影 CT到增强CT的合成技术

Generative Adversarial Network–based Noncontrast CT Angiography for Aorta and Carotid Arteries 基于生成对抗性网络的主动脉和颈动脉非集中CT血管造影背景贡献实验方法损失函数Thinking 基于生成对抗性网络的主动脉和颈动脉非集中CT血管造影 https://github.com/ying-f…

4-20mA高精度采集方案

下载链接&#xff01;https://mp.weixin.qq.com/s?__bizMzU2OTc4ODA4OA&mid2247557466&idx1&snb5a323285c2629a41d2a896764db27eb&chksmfcfaf28dcb8d7b9bb6211030d9bda53db63ab51f765b4165d9fa630e54301f0406efdabff0fb&token976581939&langzh_CN#rd …

kafka精准一次、事务、幂等性

Kafka事务 消息中间件的消息保障的3个级别 At most once 至多一次。数据丢失。At last once 至少一次。数据冗余Exactly one 精准一次。好&#xff01;&#xff01;&#xff01; 如何区分只要盯准提交位移、消费消息这两个动作的时机就可以了。 当&#xff1a;先消费消息、…

计算机中由于找不到vcruntime140.dll无法继续执行代码无法打开软件怎么解决分享

关于如何解决vcruntime140.dll无法继续执行代码的6个教程。在这个科技日新月异的时代&#xff0c;电脑已经是我们日常和工作中必不可少的电子产品&#xff0c;然后我们在使用过程中经常会遇到不一样的问题&#xff0c;比如vcruntime140.dll文件丢失&#xff0c;那么vcruntime14…

selenium的基础语法

&#x1f4d1;打牌 &#xff1a; da pai ge的个人主页 &#x1f324;️个人专栏 &#xff1a; da pai ge的博客专栏 ☁️山水速疾来去易&#xff0c;襄樊镇固永难开 ☁️定位页面的元素 参数:抽象类By里…