一、网络基础(TCP/IP详解)

1、IP协议(Internet Protocol):网络层,支持网间数据报通信。无连接数据报传送,数据报路由选择和差错控制。

IPv4 32位(4字节),IPv6 128位(16字节)。P

ping ICMP协议

2、TCP协议、UDP协议

(1)TCP(transmission control protocol 打电话):专门设计用于在不可靠的因特网上提供可靠的、端到端的字节流通信的协议。它是一种面向连接的协议。有三次握手。慢

(2)UDP(user data protocol 寄信):提供了一种发送封装的原始IP数据报的方法、并且发送时无需建立连接,是一种不可靠的连接。快

二、Socket

两个Java应用程序可以通过一个双向的网络通信连接实现数据交换,这个双向链路的一端称为一个Socket。

Socket通常用来实现client-server连接。

java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向连接(TCP连接)的client和server端。

建立连接时所需的寻址信息为远程计算机的IP地址和端口号(Port number)。端口号2字节,可以区分不同的应用程序。端口号又分TCP端口和UDP端口,每个都是65536个端口。

例如:

收邮件 POP3 110

STMP 25

FTP 21

HTTP 80

1、TCP Socket通信模型

这只是练习,实际上的网络编程都是异步式的。System.in,accept(),readUTF()都是阻塞式的(非重点)

例1

import java.net.*;
import java.io.*;
public class TCPServer{
public static void main(String args[]) throws Exception{
ServerSocket ss = new ServerSocket(6666);//端口号6666
while(true){
Socket s = ss.accept();
System.out.println("a client connect!");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
}
}
}
import java.net.*;
import java.io.*;
public class TCPClient{
public static void main(String args[]) throws Exception{
Socket s = new Socket("127.0.0.1",6666);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello server!");
dos.flush();
dos.close();
s.close();
}
}

例2

import java.io.*;
import java.net.*;
public class TestServer{
public static void main(String[] args){
try{
ServerSocket s = new ServerSocket(8888);//服务器端口号8888
while(true){
Socket s1 = s.accept();
OutputStream os = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("Hello,"+s1.getInetAddress()+"port#"+s1.getPort()+" byebye!");//客户端的IP地址和端口号
dos.close();
s1.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestClient{
public static void main(String[] args){
try{
Socket s1 = new Socket("127.0.0.1",8888);//服务器地址和端口号
InputStream is = s1.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s1.close();
}catch(ConnectException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

例3

import java.io.*;
import java.net.*;
public class TestServer{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
try{
ServerSocket ss = new ServerSocket(5888);//设置端口号
Socket s1 = ss.accept();
in = s1.getInputStream();
out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
DataInputStream dis = new DataInputStream(in);
String s = null;
if((s=dis.readUTF())!=null){
System.out.println(s);
System.out.println("from: "+s1.getInetAddress());
System.out.println("Port: "+s1.getPort());
}
dos.writeUTF("hi,hello");
dis.close();
dos.close();
s1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestClient{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
try{
Socket s1 = new Socket("localhost",5888);//服务器地址和端口号
in = s1.getInputStream();
out = s1.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
dos.writeUTF("hey");
String s = null;
if((s=dis.readUTF())!=null){
System.out.println(s);
}
dos.close();
dis.close();
s1.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

练习4:(by myself)

import java.io.*;
import java.net.*;
public class TestServer{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
String si = null;
String so = "";
try{
ServerSocket ss = new ServerSocket(5888);//设置端口号
while(true){
Socket s1 = ss.accept();
in = s1.getInputStream();//接收数据
out = s1.getOutputStream();//发送数据
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
//从键盘读入
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader br2 = new BufferedReader(isr2);
while(!so.equals("exit")){
if((so=br2.readLine())!=null&&!so.equals("exit")){
System.out.println("Server:"+so);
dos.writeUTF(so);
}
if((si=dis.readUTF())!=null){
System.out.println("Client:"+si);
}
}
dis.close();
br2.close();
dos.close();
s1.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestClient{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
String si = null;
String so= "";
try{
Socket s1 = new Socket("127.0.0.1",5888);//服务器地址和端口号
in = s1.getInputStream();//接收
out = s1.getOutputStream();//发送
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
//从键盘读入
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader br2 = new BufferedReader(isr2);
while(!so.equals("exit")){
if((si=dis.readUTF())!=null){
System.out.println("Server:"+si);
}
if((so=br2.readLine())!=null&&!so.equals("exit")){
System.out.println("Client:"+so);
dos.writeUTF(so);
}
}
dis.close();
br2.close();
dos.close();
s1.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

2、UDP Socket通信模型

没有server,client的概念,不区分两者的socket。receive()方法也是阻塞式的。

例1

import java.net.*;
public class TestUDPServer{
public static void main(String args[]) throws Exception{
byte buf[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true){
ds.receive(dp);
System.out.println(new String(buf, 0, dp.getLength()));
}
}
}
import java.net.*;
public class TestUDPClient{
public static void main(String args[]) throws Exception{
byte[] buf = (new String("Hello")).getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}

例2

import java.net.*;
import java.io.*;
public class TestUDPServer{
public static void main(String args[]) throws Exception{
byte buf[] = new byte[1024];
ByteArrayInputStream bais = new ByteArrayInputStream(buf);//从字节数组读数据
DataInputStream dis = new DataInputStream(bais);
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true){
ds.receive(dp);
long l = dis.readLong();
System.out.println(l);
}
}
}
import java.net.*;
import java.io.*;
public class TestUDPClient{
public static void main(String args[]) throws Exception{
long n = 10000L;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(n);
byte[] buf = baos.toByteArray();
System.out.println(buf.length);
DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}

JAVA笔记26-网络编程(不等于网站编程)的更多相关文章

  1. Java笔记 - Socket编程

    两个Java应用程序可以通过一个双向的网络通讯连接实现数据交换,这个双向链路的一端称为一个Socket.java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向链路的c ...

  2. Java 学习之网络编程案例

    网络编程案例 一,概念 1,网络编程不等于网站编程 2,编程只和传输层打交道,即TCP和UDP两个协议 二,案例 1,TCP实现点对点的聊天 Server端:两个输入流:读客户端和控制台,一个输出端: ...

  3. 大数据学习笔记——Java篇之网络编程基础

    Java网络编程学习笔记 1. 网络编程基础知识 1.1 网络分层图 网络分层分为两种模型:OSI模型以及TCP/IP网络模型,前者模型分为7层,是一个理论的,参考的模型:后者为实际应用的模型,具体对 ...

  4. JAVA自学笔记26

    JAVA自学笔记26 1.网络编程 1)用来实现网络互联的不同计算机上运行的程序可以进行数据交换 2)网络模型一般泛指 OSI:(Open System Interconnection)开放系统互联参 ...

  5. Java学习之网络编程实例

    转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616413.html 多谢分享 网络编程 网络编程对于很多的初学者来说,都是很向往的一 ...

  6. Java进阶之网络编程

    网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...

  7. java第九节 网络编程的基础知识

    /** * * 网络编程的基础知识 * 网络协议与TCP/IP * IP地址和Port(端口号) * 本地回路的IP地址:127.0.0.1 * 端口号的范围为0-65535之间,0-1023之间的端 ...

  8. 20165324 Java实验五 网络编程与安全

    20165324 Java实验五 网络编程与安全 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年5月28日 实 ...

  9. Java网络编程 -- NIO非阻塞网络编程

    从Java1.4开始,为了替代Java IO和网络相关的API,提高程序的运行速度,Java提供了新的IO操作非阻塞的API即Java NIO.NIO中有三大核心组件:Buffer(缓冲区),Chan ...

随机推荐

  1. javascript 数据类型之数值转换

    数值转换 一.有3个函数可以把非数值转换为数值: Number() parse Int() parse Float() 说明: 1.Number()可以用于任何数据类型,强转类型,如果不能把指转成数值 ...

  2. excel常用公式--计算统计类

    Count/Countif/Countifs:条件计数. 注:count只能对数值进行统计 sum/sumif/sumifs:条件求和.   Average/Averageifs:  返回参数的平均值 ...

  3. Spring Boot常用功能

    1.Spring Boot打war包配置 利用IDEA将SpringBoot的项目打包成war文件

  4. Element el-table-column组件列宽度设置百分比无效

    问题 使用Element table组件时,给列设置百分比宽度无效(width="30%") 解决 用属性min-width="3"代替属性width=&quo ...

  5. Codeforces Round #587 (Div. 3)

    https://codeforces.com/contest/1216/problem/A A. Prefixes 题意大概就是每个偶数位置前面的ab数目要相等,很水,被自己坑了 1是没看见要输出修改 ...

  6. CodeForces-431D Random Task

    题目描述 求一个\(n\),使得\(n+1\)到\(2n\)这些数的二进制中恰好有\(k\)个\(1\)的数有\(m\)个. Input 输入包含两个正整数\(m,k\).$(0<=m<= ...

  7. mysql 聚合函数(2)

    平均 svg select avg(sal + IFNULL(comm,0)) as avg_sal from t_emp 总和 sum select sum(sal + IFNULL(comm,0) ...

  8. mysql-5.7.25安装以及使用

    1. wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz 2. ...

  9. 由对称性解2-SAT问题

    由对称性解2-SAT问题 (by 伍昱,03年IOI国家集训队论文ppt) 2-SAT: 2-SAT就是2判定性问题,是一种特殊的逻辑判定问题. 2-SAT问题有何特殊性?该如何求解? 我们从一道例题 ...

  10. [.net core]10.请求静态文件, 自定义默认文件名

    何谓静态文件,文件系统上的文件,  css, javascript , image. html  这些都属于静态文件, .net core web app 默认是不处理文件请求的.  我们来做一个实验 ...