【高级网络程序设计】Week2-1 Sockets

news2024/11/16 3:42:45

一、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.coloradmin.cn/o/1246633.html

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

相关文章

VR Interaction Framework2.0使用

1 按键 ,比如按压下手柄的B键 if (InputBridge.Instance.BButtonDown){print("kkkkkkbbbbb456");} 2抓取某个物体,那么就在要抓取的那个物体上加一些组件,特别是Grabble Unity Events

都2023年了,为什么大家还都在吹捧 Python?

2023 年,Python 还可学吗? 答案当然是可。 近些年间,Python 的火热有目共睹,作为一种功能强大的高级编程语言,在 2018 年的时候它的流行程度就得到了大幅提高。 图源:Stack Overflow 网站编程语言浏览量统…

累计定点160+车型,商汤绝影凭什么领跑规模化量产?

2023广州车展火热进行,智能化技术加速“内卷”。 商汤绝影多款合作量产车型亮相2023广州车展,包括昊铂 GT、传祺ES9、E8系列和本田雅阁、捷途旅行者、极氪X等,全方位呈现在智能驾驶和智能座舱领域的最新成果,以AI“新科技”&…

「树形」样式,数据关联超便捷丨三叠云

树形样式 路径 表单设计 >> 字段属性 功能简介 「表单关联」的数据列表样式支持「树形」样式功能,关联数据选择时通过「树形」的列表方式进行数据选择,提高生产效率。 使用场景: 可以通过树形列表样式展示部门、子部门、成员的树形…

Android开发从0开始(服务)

Android后台运行的解决方案,不需要交互,长期运行。 服务基础框架: public class MyService extends Service { public MyService() { } Override public IBinder onBind(Intent intent) { //activity与service交互(需要继…

美国汽车零部件巨头 AutoZone 遭遇网络攻击

Security Affairs 网站披露,美国汽车配件零售商巨头 AutoZone 称其成为了 Clop MOVEit 文件传输网络攻击的受害者,导致大量数据泄露。 AutoZone 是美国最大的汽车零配件售后市场经销商之一,在美国、墨西哥、波多黎各、巴西和美属维尔京群岛经…

Django项目window环境部署

最近刚好有时间,把前端时间使用django框架开发的音乐网站部署到本地电脑上,在这里记录一下部署过程。 环境配置 Python 3.7.5 Django 3.2.20 Apache 2.4.39 电脑64位 生产环境配置 设置生产环境静态配置 示例如下: # 生产环境静态资源…

windows环境下使用arthas不启动服务替换文件

场景: windows环境,如果现场环境客户正在使用项目,如果要替换项目中的一个class文件,但是又不能重启服务改怎么处理,今天介绍使用arthas中的retransform命令动态替换及使用注意的事项。 环境: windows,arth…

opencv-ORB检测

ORB(Oriented FAST and Rotated BRIEF)是一种图像特征检测和描述算法,结合了 FAST 关键点检测器和 BRIEF 描述子的优点。ORB 算法具有良好的性能,特别适用于实时应用,如目标追踪、相机定位等。 以下是 ORB 算法的一般…

【Python】生死簿管理系统,估值5毛

生死簿管理系统 代码 """ 生死簿管理系统 """ import os import timefile_name data.txtdef main():while True:main_menu()choice (int)(input("请选择: "))if choice in [0, 1, 2, 3, 4, 5, 6, 7]:if choice 0:answer input(&…

2022年MathorCup高校数学建模挑战赛—大数据竞赛A题58到家家政服务订单分配问题求解全过程文档及程序

2022年MathorCup高校数学建模挑战赛—大数据竞赛 A题 58到家家政服务订单分配问题 原题再现: “58 到家”是“58 同城”旗下高品质、高效率的上门家政服务平台,平台向用户提供家政保洁、保姆、月嫂、搬家、维修等众多生活领域的服务。在家政保洁场景中…

WPF面试题中级篇

WPF中级篇[17] 15. 描述下WPF对象完整的层次结构? Object:Object 是 .NET Framework 中所有类的根类。它提供了一些基本的方法和属性,如 Equals、GetHashCode 和 ToString。所有其他类都直接或间接地继承自 Object。Dispatcher:Dispatcher…

蒸蛋器上平台销售UL1026检测报告标准

蒸蛋器是一种采用发热盘通电后,使盘内产生高温蒸汽来快速把禽蛋蒸熟的一种生活小家电。蒸蛋器最大的特点就是快速、简单,并充满乐趣。 深圳ISO 17025实验室办理蒸蛋器亚马逊美国站UL报告办理UL1026测试标准。 UL1026标准是关于蒸蛋器的安全性要求&…

CMS指纹识别方式

一、手工识别 1.robots.txt文件 robots.txt文件我们写过爬虫的就知道,这个文件是告诉我们哪些目录是禁止爬取的。但是大部分的时候我们都能通过robots.txt文件来判断出cms的类型 如: 从wp路径可以看出这个是WordPress的cms 这个就比较明显了直接告诉我们是PageAdmin cms 也…

java堆文件排查

技术主题 在之前的开发的一个项目中,因为程序的一个bug,导致一些引用的对象一直没有回收,从而导致堆内存一直在增大,老年代一直在增大,老年代进行堆积,后来的排查思路是通过dump堆的文件,然后对…

易点易动固定资产管理系统:实现固定资产与财务系统的高效对接

在企业的日常运营中,固定资产的管理和财务账目的记录是两项不可或缺的任务。然而,由于传统的管理方式存在数据孤岛和信息不一致等问题,往往导致工作效率低下和管理混乱。为了解决这一问题,易点易动固定资产管理系统应运而生。该系…

每日一题 2824. 统计和小于目标的下标对数目(简单)

简单题&#xff0c;走流程 class Solution:def countPairs(self, nums: List[int], target: int) -> int:ans 0for i in range(len(nums)):for j in range(i 1, len(nums)):if nums[i] nums[j] < target:ans 1return ans

2023年ESG投资研究报告

第一章 ESG投资概况 1.1 定义 ESG投资&#xff0c;亦称负责任投资&#xff0c;是一种融合环境&#xff08;Environment&#xff09;、社会&#xff08;Social&#xff09;和治理&#xff08;Governance&#xff09;考量的投资方法&#xff0c;旨在通过综合这些因素来优化投资…

30㎡新中式大横厅|方寸之间,诉说东方写意生活。福州中宅装饰,福州装修

今天要分享的是一个新中式风格的客厅装修&#xff0c;它的开间是4.5米&#xff0c;进深是6.5米。设计中有许多亮点&#xff0c;让我们一起来看看。 1️⃣ 首先&#xff0c;这个客厅采用了双眼皮无主灯吊顶的设计&#xff0c;让整个空间看起来更加高挑宽敞。吊顶的边缘线条简洁明…

Jmeter基础和概念

JMeter 介绍&#xff1a; 一个非常优秀的开源的性能测试工具。 优点&#xff1a;你用着用着就会发现它的重多优点&#xff0c;当然不足点也会呈现出来。 从性能工具的原理划分&#xff1a; Jmeter工具和其他性能工具在原理上完全一致&#xff0c;工具包含4个部分&#xff1a; …