【高级网络程序设计】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.mzph.cn/news/162314.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CentOS 7 使用cJSON 库

什么是JSON JSON是一种轻量级的数据交换格式,可读性强、编写简单。键值对组合编写规则,键名使用双引号包裹,冒号:分隔符后面紧跟着数值,有两种常用的数据类型是对象和数组。 对象:使用花括号{}包裹起来的…

接口传参数list的时候,items里面放个​​​​​​​list

item里面放个list 先定义一个 list,循环add加入参数

android keylayout键值适配

1、通过getevent打印查看当前keyevent数字对应事件和物理码 2、dumpsys input 查看输入事件对应的 KeyLayoutFile: /system/usr/keylayout/Vendor_6080_Product_8060.kl 3、通过物理码修改键值映射,修改/system/usr/keylayout/目录下的文件

永久免费!N个excel表一键合并成一个表(excel表格合并技巧)

您是否还在用手工复制粘贴来将多个EXCEL或表的数据合并到一个表里?那就太麻烦,效率太低了,用金鸣表格文字识别的“表格合并”功能,可免费将N个excel文件或N个excel表一键合并到一个表里面,而且这个功能永久免费&#x…

【C++】特殊类设计 {不能被拷贝的类;只能在堆上创建的类;只能在栈上创建的类;不能被继承的类;单例模式:懒汉模式,饿汉模式}

一、不能被拷贝的类 设计思路: 拷贝只会发生在两个场景中:拷贝构造和赋值重载,因此想要让一个类禁止拷贝,只需让该类不能调用拷贝构造以及赋值重载即可。 C98方案: 将拷贝构造与赋值重载只声明不定义,并…

FDG6306P PowerTrench® MOSFET P沟道 特点及其应用详解

关于PowerTrench MOSFET? 它是一种MOS场效应晶体管,可以提高系统效率和功率密度。该技术采用了屏蔽栅极技术,可以减少开关损耗和导通损耗,从而提高了系统效率。此外,PowerTrench MOSFET还具有低导通电阻和高开关速度的…

前端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 请求所经历的步骤?三、说说你对分布式事务的了解前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男女通用,…

非遗之光:十八数藏柏松数字保护的璀璨之路

随着数字技术的崛起&#xff0c;非物质文化遗产的保护进入了一个新的纪元。在这个时代的先锋中&#xff0c;十八数藏以其对传统工艺的数字保护而独领风骚。这是一条璀璨之路&#xff0c;通过数字技术的应用&#xff0c;为传统工艺注入了新的活力。 十八数藏柏松将数字创新融入传…

软件包管理器yum和git

目录 一、Linux软件包管理器yum 1、Linux下的软件安装方法 2、了解yum 1、实际例子引入 2、yum 3、查找软件包 4、安装软件包 5、卸载软件 二、git 一、Linux软件包管理器yum 1、Linux下的软件安装方法 1、在Linux下安装软件&#xff0c;一个通常的办法是下载到程序的源…

经典百搭女童加绒卫衣,看的见的时尚

经典版型套头卫衣 宽松百搭不挑人穿 单穿内搭都可以 胸口处有精美的小熊印花 面料是复合柔软奥利绒 暖和又不显臃肿哦&#xff01;&#xff01;

Jenkins+Maven+Gitlab+Tomcat 自动化构建打包、部署

JenkinsMavenGitlabTomcat 自动化构建打包、部署 1、环境需求 本帖针对的是Linux环境&#xff0c;Windows或其他系统也可借鉴。具体只讲述Jenkins配置以及整个流程的实现。 1.JDK&#xff08;或JRE&#xff09;及Java环境变量配置&#xff0c;我用的是JDK1.8.0_144&#xff0…

排序算法--快速排序

实现逻辑 ① 从数列中挑出一个元素&#xff0c;称为 “基准”&#xff08;pivot&#xff09;&#xff0c; ② 重新排序数列&#xff0c;所有元素比基准值小的摆放在基准前面&#xff0c;所有元素比基准值大的摆在基准的后面&#xff08;相同的数可以到任一边&#xff09;。在这…

2023年度openGauss标杆应用实践案例征集

标杆应用实践案例征集 2023 openGauss 数据库作为企业IT系统的核心组成部分&#xff0c;是数字基础设施建设的关键&#xff0c;是实现数据安全稳定的保障。openGauss顺应开源发展趋势&#xff0c;强化核心技术突破&#xff0c;着力打造自主根社区&#xff0c;携手产业伙伴共同…

【开源】基于JAVA的高校实验室管理系统

项目编号&#xff1a; S 015 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S015&#xff0c;文末获取源码。} 项目编号&#xff1a;S015&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、研究内容2.1 实验室类型模块2.2 实验室模块2.3 实…

PTA-用天平找小球

三个球A、B、C&#xff0c;大小形状相同且其中有一个球与其他球重量不同。要求找出这个不一样的球。 输入格式&#xff1a; 输入在一行中给出3个正整数&#xff0c;顺序对应球A、B、C的重量。 输出格式&#xff1a; 在一行中输出唯一的那个不一样的球。 输入样例&#xff…