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();//输出流

最新文章

  1. Python:进程
  2. HTML 学习笔记 CSS3 (边框)
  3. Excel导入导出,通过datatable转存(篇一)
  4. BZOJ4113 : [Wf2015]Qanat
  5. http和htpps
  6. [OpenCV] Identify and Track Specific Object
  7. CocoStudio基础教程(3)在程序中处理cocoStudio导出动画
  8. shell 获取当前ip
  9. hadoop_并行写操作思路_2
  10. spring mvc 注解入门示例
  11. 心路历程(五)-find work and find house
  12. jquery 追加元素/jquery文档处理,插入、修改、移动、删除指定的DOM元素.
  13. 编译安装lamp环境
  14. Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )
  15. hdu 5532 (LIS) Almost Sorted Array
  16. Codeforces 352B - Jeff and Periods
  17. Python学习-24.Python中的算术运算
  18. UITableView 让 cell 被选中的颜色底色快速消失,而不是一直停留在cell上
  19. 【matlab】输出显示函数 sprintf()&amp;disp()
  20. tidb 集群扩容

热门文章

  1. SQL SERVER 2005还原差异备份、日志备份 2012-03-29 11:43
  2. teraterm中状态框statusbox
  3. 八 Struts2访问Servlet的API方式三:接口注入
  4. Spring boot 中发送邮件
  5. java list 清空列表所有元素
  6. Linux centosVMware LNMP架构介绍、MySQL安装、PHP安装、Nginx介绍
  7. nopad++将制表符替换为换行符
  8. SRS源码—— Thread笔记
  9. 「POJ3613」Cow Relays
  10. Django 学习之cookie与session