网络参考模型
七层简述
1.层物理层:主要定义物理设备标准,如网线的接口类型、光纤的接口类型、各种传输介质的传输速率等。它的主要作用是传输比特流(就是由1、0转化为电流强弱来进行传输,到达目的地后在转化为1、0,也就是我们常说的数模转换与模数转换)。这一层的数据叫做比特。
2.层数据链路层:主要将从物理层接收的数据进行MAC地址(网卡的地址)的封装与解封装。常把这一层的数据叫做帧。在这一层工作的设备是交换机,数据通过交换机来传输。
3.层网络层:主要将从下层接收到的数据进行IP地址(例192.168.0.1)的封装与解封装。在这一层工作的设备是路由器,常把这一层的数据叫做数据包。
4.层传输层:定义了一些传输数据的协议和端口号(WWW端口80等),如:TCP(传输控制协议,传输效率低,可靠性强,用于传输可靠性要求高,数据量大的数据),UDP(用户数据报协议,与TCP特性恰恰相反,用于传输可靠性要求不高,数据量小的数据,如QQ聊天数据就是通过这种方式传输的)。主要是将从下层接收的数据进行分段和传输,到达目的地址后再进行重组。常常把这一层数据叫做段。
5.会话层:通过传输层(端口号:传输端口与接收端口)建立数据传输的通路。主要在你的系统之间发起会话或者接受会话请求(设备之间需要互相认识可以是IP也可以是MAC或者是主机名)
6.表示层:主要是进行对接收的数据进行解释、加密与解密、压缩与解压缩等(也就是把计算机能够识别的东西转换成人能够能识别的东西(如图片、声音等)。
7.应用层: 主要是一些终端的应用,比如说FTP(各种文件下载),WEB(IE浏览),QQ之类的(可以把它理解成我们在电脑屏幕上可以看到的东西.就是终端应用)。
网络通讯要素
public static void main(String[] args) throws UnknownHostException {
// InetAddress ip = InetAddress.getLocalHost();
InetAddress ip = InetAddress.getByName("www.baidu.com");
System.out.println("ip:"+ip.getHostAddress());
System.out.println("name:"+ip.getHostName());
}
TCP和UDP
Tcp:
面向连接。
安全可靠
效率稍低。
通过三次握手确保连接的建立。
下载。
比喻:打电话。
通过管道传输石油。
Udp:
面向无连接。
不可靠。
速度快。
将数据封包传输,数据包最大64k。
聊天,在线视频,视频会议。
比喻:步话机。邮局包裹。
通过油桶传输石油。
Socket
UDP传输
发送端与接收端是两个独立的运行程序。
DatagramSocketds = new DatagramSocket();
byte[]by = “hello,udp”.getBytes();
DatagramPacketdp = new DatagramPacket(by,0,by.length,
InetAddress.getByName(“127.0.0.1”),10000);
ds.send(dp);
ds.close();
DatagramSocketds = new DatagramSocket(10000);
byte[]by = new byte[1024];
DatagramPacketdp = new DatagramPacket(by,by.length);
ds.receive(dp);
Stringstr = new String(dp.getData(),0,dp.getLength());
System.out.println(str+"--"+dp.getAddress());
ds.close();
public static void main(String[] args) throws IOException {
/*
* 需求:建立udp的接收端。
* 思路
* 1,必须有udp的端点。
* 2,接收数据,
* 3,并解析数据。
* 4,将数据打印出来看到,并结束接收动作。
*/
System.out.println("udp接收端开启.....");
//1,创建一个udp的端点。注意:接收端一定要明确具体的端口。否则收不到数据。
DatagramSocket ds = new DatagramSocket(10000);
//2,接收数据。 一般使用socket服务的动作完成。将数据存储到数据包对象中。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
//3,通过数据包对象的方法对数据进行解析。
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+port+"--"+text);
//4,关闭资源。
ds.close();
System.out.println("udp接收端关闭.....");
}
public static void main(String[] args) throws IOException {
/*
* 需求:创建一个udp的发送端。
* 思路:
* 1,要先创建udp端点。也就是udp的socket。
* 2,将数据封装成数据包。
* 3,通过udp的端点将数据发送出去。就哦了。
*
*/
System.out.println("udp发送开始......");
//1,创建udp的端点。
DatagramSocket ds = new DatagramSocket(9527);
//2,将数据封装成数据包。
String str = "udp发送数据演示!";
byte[] buf = str.getBytes();
//2.1创建数据包对象。
DatagramPacket dp =
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.200"),10000);
//3,使用udp的socket服务将数据包发出去。
ds.send(dp);
//4,关闭资源。
ds.close();
System.out.println("udp发送结束.....");
}
例从键盘录入数据public class UDPReceiver2 {
public static void main(String[] args) throws IOException {
/*
* 接收发送端的数据,并打印到屏幕上。
*/
//1,建立接收端的udpsocket服务,必须明确端口号。
DatagramSocket ds = new DatagramSocket(10001);
while(true){
//2,创建数据包。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3,使用接收方法将接收到的数据存储到数据包中。
ds.receive(dp);
//4,对数据进行解析。
String ip = dp.getAddress().getHostAddress();
String text = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+text);
}
// ds.close();
}
}
public class UDPSender2 {
public static void main(String[] args) throws IOException {
/*
* 频繁获取键盘录入的数据。并发送给接收端。
*/
// 1,建立udpsocket服务端点。
DatagramSocket ds = new DatagramSocket(8888);
// 2,明确数据,将数据封装成数据包。
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
if ("over".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("127.0.0.1"), 10001);
ds.send(dp);
}
ds.close();
}
}
群聊程序
public class ChatDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
DatagramSocket send = new DatagramSocket(9999);
DatagramSocket rece = new DatagramSocket(10002);
new Thread(new Send(send)).start();
new Thread(new Rece(rece)).start();
}
}
/*
* 群聊程序,一边发一边收。 使用两个内容:1,使用广播 192.168.1.255,使用多线程。
*/
// 建立发送的任务。
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("10.212.169.255"), 10002);
ds.send(dp);
if ("88".equals(line)) {
break;
}
}
ds.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 建立接收任务。
class Rece implements Runnable {
private DatagramSocket ds;
public Rece(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
try {
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String text = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + "::" + text);
if ("88".equals(text)) {
System.out.println(ip + ".....离开聊天室!!!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TCP传输
同样,客户端与服务器端是两个独立的应用程序。
Sockets = new Socket(“192.168.1.1”,9999);
OutputStreamout = s.getOutputStream();
out.write(“hello”.getBytes());
s.close();
服务端ServerSocketss = new ServerSocket(9999);
Sockets = ss.accept ();
InputStreamin = s.getInputStream();
byte[]buf = new byte[1024];
intnum = in.read(buf);
Stringstr = new String(buf,0,num);
System.out.println(s.getInetAddress().toString()+”:”+str);
s.close();
ss.close();
public class TCPClient {
public static void main(String[] args) throws UnknownHostException, IOException {
/*
* 需求:基于tcp,创建一个客户端。
* 并将数据发送给服务端。
*/
System.out.println("客户端开启......");
//1,必须先建立tcp的客户端socket服务。
Socket s = new Socket("192.168.1.200",10003);
//2,通过socket获取socket流中的输出流对象。
OutputStream out = s.getOutputStream();
out.write("hi,server,哥们来了!".getBytes());
s.close();
System.out.println("客户端关闭......");
}
}
public class TCPServer {
public static void main(String[] args) throws IOException {
/*
* 创建服务端,接收客户端的数据。并打印在控制台上。
*/
System.out.println("服务端启动......");
//1,创建tcp的服务端socket服务。服务端必须指定端口。
ServerSocket ss = new ServerSocket(10003);
//2,获取连接过来的客户端的对象。
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"....connected");
//3,通过客户端对象的读取流,读取客户端发来的数据。
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
ss.close();
System.out.println("服务端关闭......");
}
}
public class TCPServer2 {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
//1,创建tcp服务端的socket服务,并监听一个端口。
ServerSocket ss = new ServerSocket(10004);
//2,获取客户端对象。
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connected");
//3,通过客户端对象获取socket流中的输入流。
InputStream in = s.getInputStream();
//4,定义缓冲区,读取数据。
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
//5,通过客户端对象获取socket流中的输出流。
OutputStream out = s.getOutputStream();
out.write("哈哈,我已收到!哦也!".getBytes());
s.close();
ss.close();
}
}
public class TCPClient2 {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
/*
* 需求:客户端将数据发送到服务端,服务端收到后,将应答的数据反馈给客户端。
*/
//1,创建tcp客户端socket服务。
Socket s = new Socket("127.0.0.2",10004);
//2,获取socket流的输出流对象。
OutputStream out = s.getOutputStream();
//3,将数据写入到的socket流。
out.write("hi,server,哥们又来了!".getBytes());
//4,获取服务端发挥的数据,需要用到socket流中的输入流。
InputStream in = s.getInputStream();
//5,创建缓冲区,读取数据。
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}
public class TransServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,创建服务端socket服务。
ServerSocket ss = new ServerSocket(10005);
//2,获取socket对象。
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"....connected");
//3,明确源,网络。Socket输入流。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
//4,明确目的,网络,socket输出流。
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//5,就是熟悉的读写过程。
String line = null;
while((line=bufIn.readLine())!=null){
out.println(line.toUpperCase());
}
s.close();
ss.close();
}
}
public class TransClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
/*
* 需求:通过客户端给服务器端发送单词数据,服务端将单词转成大写返回客户端。
* 如果客户端发送over,则客户端结束发送。其实就是创建了一个大写服务器。
*/
//1,创建客户端socket服务。
Socket s = new Socket("127.0.0.1",10005);
//2,客户端读取键盘录入的单词。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//3,明确目的,网络流。
// BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//4,明确源。来自于网络。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
out.println(line);
String str = bufIn.readLine();
System.out.println("trans : "+str);
}
s.close();
}
}
Tcp传输最容易出现的问题
public class UploadPic implements Runnable {
private Socket s;
public UploadPic(Socket s) {
this.s = s;
}
@Override
public void run() {
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
try {
InputStream in = s.getInputStream();
File dir = new File("e:\\pic");
if (!dir.exists()) {
dir.mkdirs();
}
int count = 1;
File file = new File(dir, ip + "("+count+").jpg");
while(file.exists()){
file = new File(dir, ip + "("+(++count)+").jpg");
}
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
} catch (IOException e) {
}
}
}
public class UploadPicClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("192.168.1.200",10007);
//客户端要读取图片。
FileInputStream fis = new FileInputStream("temp\\1.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}
//告诉服务端已经写完。
s.shutdownOutput();
InputStream in = s.getInputStream();
//读取上传成功字样。
byte[] bufIn = new byte[1024];
int lenIn = in.read(bufIn);
System.out.println(new String(bufIn,0,lenIn));
fis.close();
s.close();
}
}
public class UploadPicServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10007);
while (true) {
Socket s = ss.accept();
new Thread(new UploadPic(s)).start();
}
// ss.close();
}
}