Java笔记--网络编程
1、IP地址:InetAddress类
--唯一的标识Internet上的计算机
--本地回环地址(hostAddress)127.0.0.1 主机名(hostName):localhost
//根据域名获取IP地址信息
InetAddress inet = InetAddress.getByName("localhost");
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress()); //获取本机IP地址
inet = InetAddress.getLocalHost();
System.out.println(inet);
2、端口号:标识正在计算机上运行的进程。
3、网络通信协议:计算机网络中实现通信必须有一些约定,即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准;
--传输层中的两个重要协议:1、传输控制协议TCP(Transmission Control Protocol); 2、用户数据报协议UDP(User DatagramProtocol)。4、网络套接字Socket:端口号与IP地址的组合。
--网络通信就是Socket间的通信,Socket允许程序把网络程序当成一个流,数据在两个Socket间通过IO传输。
--主动发起通信的应用程序成为客户端,等待通信请求的成为服务端。
4、Socket编程例子(TCP):
--Client:
public void client() {
Socket s = null;
OutputStream os = null;
InputStream is = null;
try{
//1.创建一个Socket对象,通过构造器指明服务端的IP地址和接收程序的端口号
s = new Socket("127.0.0.1", 9090);
//2.getOutputStream获得OutputStream对象后发送数据
os = s.getOutputStream();
os.write("This is Client".getBytes());
//3.发送完毕之后关闭发送
s.shutdownOutput();
//4.getInoutStream获得InoutStream对象,接收服务器返回的数据
is = s.getInputStream();
byte[] b = new byte[20];
int len = 0;
while((len = is.read(b)) != -1){
System.out.println(new String(b, 0, len));
}
}catch(IOException e){
e.printStackTrace();
}finally{
//5.关闭相应的资源
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s. close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
--Server:
public void Server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try{
ss = new ServerSocket(9090);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len = 0;
while((len = is.read(b)) != -1){
System.out.println(new String(b, 0, len));
}
os = s.getOutputStream();
os.write("Accepted".getBytes());
}catch(IOException e){
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s. close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5、类DatagramSocket和DataPacket实现了基于UDP协议的网络程序:
--UDP数据包通过数据报套接字DatagramSocket发送和接收,系统不保证UDP数据报一定能够安全送达目的地,也不确定什么时候可以到达;
--DatagramPacket类封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号;
--UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送放与接收方的连接。
6、DatagramSocket编程(UDP):
--Client:
public void client() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
byte[] b = "This is Client".getBytes();
DatagramPacket dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 8787);
ds.send(dp);
}catch (IOException e) {
e.printStackTrace();
}finally{
ds.close();
}
}
--Server:
public void server(String[] args) {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(8787);
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, 0, b.length);
ds.receive(dp);
System.out.println(new String(b, 0, dp.getLength()));
}catch (IOException e) {
e.printStackTrace();
}finally{
ds.close();
}
}
7、URL编程(Uniform Resource Locator:统一资源定位符)
--构成:<传输协议>://<主机名>:端口号/<文件名>;
--URL类:一个URL对象对应着网络上的一个资源;
--URL类常用方法:1)public String getProtocol();//获取该URL的协议
2)public String getHost();//获取该URL的主机名
3)public String getPort();//获取该URL的端口号
4)public String getPath();//获取该URL的文件路径
5)public String getFile();//获取文件名
6)public String getRef();//获取文件的相对位置
7)public String getQuery();//获取该URL的查询名
--URL的openStream()方法能够从网络上读取数据。
8、URLConnection:表示URL所引用的远程对象的连接:
--UrlConnection urlConnection = url.openConnection();
--InputStream is = urlConnection.getInputStream();//输入流
--OutputStream os = urlConnection.getOutputStream();//输出流
Java笔记--网络编程的更多相关文章
- Netty | 第1章 Java NIO 网络编程《Netty In Action》
目录 前言 1. Java 网络编程 1.1 Javs NIO 基本介绍 1.2 缓冲区 Buffer 1.2 通道 Channel 1.3 选择器 Selector 1.4 NIO 非阻塞网络编程原 ...
- 二十三、Java基础--------网络编程
Java中另一个重要技术就是网络编程了,为了更好的学习web方向的知识,有必要对java之网络编程好好学习,本文将围绕网络编程技术进行分析. 常见的网络协议:UDP.TCP UDP 1. 将数据源和目 ...
- JAVA的网络编程
网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...
- Java Socket 网络编程心跳设计概念
Java Socket 网络编程心跳设计概念 1.一般是用来判断对方(设备,进程或其它网元)是否正常动行,一 般采用定时发送简单的通讯包,如果在指定时间段内未收到对方响应,则判断对方已经当掉.用于 ...
- 20145325张梓靖 实验五 "JAVA的网络编程"
20145325张梓靖 实验五 "JAVA的网络编程" 实验内容 使用 JVAV语言 进行网络编程 对明文进行加密 设计过程 我完成的是客户端,服务端同伴 20145308刘昊阳 ...
- 【转】JAVA之网络编程
转自:火之光 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者 ...
- JAVA的网络编程【转】
JAVA的网络编程[转] Posted on 2009-12-03 18:04 火之光 阅读(93441) 评论(20) 编辑 收藏 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能, ...
- Java面向对象 网络编程 下
Java面向对象 网络编程 下 知识概要: (1)Tcp 练习 (2)客户端向服务端上传一个图片. (3) 请求登陆 (4)url 需求:上传图片. 客户端: ...
- Java面向对象 网络编程 上
Java面向对象 网络编程 上 知识概要: (1)网络模型 (2)网络通讯要素 (3)UDP TCP 概念 (4)Socket (5)UDP TCP 传输 ...
随机推荐
- Go语言学习笔记(四)
一.字符串 1.字符串截取 可以使用len(字符串变量)获取字符串的字节长度,其中英文占1个字节长度,中文占用3个字节长度 可以使用变量名[n]获取到字符串第n+1个字节,返回这个字节对应的Unico ...
- 多Python版本共存
Python 3.4 和 3.7 共存 我的电脑上同时安装了 Python 3.4 和 Python 3.7 两个 Python 版本.现在打开终端窗口进入指定的版本. py -3.4 py -3.7 ...
- Python开发:Python运算符
运算符 1.算数运算: 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘或是返 ...
- JavaScript - 运行机制,作用域,作用域链(Scope chain)
参考 https://www.jianshu.com/p/3b5f0cb59344 https://jingyan.baidu.com/article/4f34706e18745be386b56d46 ...
- Python - 工具
Anaconda - 自带Conda,可以自定义环境 Pycharm zeal - API离线查看,类似于Dash
- Sonic_cli常用命令
用户名:admin 密码:YourPaSsWoRd //change password1>admin@sonic:~$ passwdChanging password for admin.(cu ...
- 01初步启动Hadoop服务
1.rz命令将hadoop压缩包上传至Linux服务器中 2.tar -zxvf hadoop-2.7.7.tar.gz(解压即可用) 3.将解压出来的hadoop移到想要放的位置 mv hadoop ...
- js 原生url编码
参考:http://www.runoob.com/jsref/jsref-decodeuricomponent.html
- springboot 不停服动态更新定时任务时间(转)
转 https://blog.csdn.net/u012129558/article/details/80834303 Spring框架自3.0版本起,自带了任务调度功能,好比是一个轻量级的Quart ...
- 「Luogu P3680 凸轮廓线」
一道神奇的计算几何题 前置芝士 正三角形,正方形,圆:什么,您都会,那真是太好了. 三角函数的运用:因为我不是很想在这一块写太多,具体可以自行百度. 推导公式 对于一串是圆和正方形开头和结尾时是十分好 ...