【高级网络程序设计】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,一经查实,立即删除!

相关文章

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