network programming
Notes content:
- Network Programming Overview
- Three elements of network programming
- UDP Programming
- TCP programming
Network Programming Overview:
computer network
是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
network programming
就是用来实现网络互连的==不同计算机上==运行的程序间可以进行数据交换。
Network model:
计算机网络之间以何种规则进行通信,就是网络模型研究问题。
Generally refers to the network model
OSI(Open System Interconnection开放系统互连)参考模型
TCP/IP参考模型
Network reference model of FIG.
Three elements of network programming
- IP address: InetAddress
identifies the network device, easy to remember, the host name is available - Port number
identifies the logical address, the process used to identify the different processes - Transfer Protocol
communications rules
common protocols: TCP, UDP
IP addresses
To get the network computers can communicate with each other, must specify an identification number == == for each computer to identify computers and computer designated to receive data transmitted by the identification number in the TCP / IP protocol, this identification number is the IP address.
The computer can only recognize binary data, our IP address should be a binary data, but we configured IP address is not binary, in order to facilitate representation on the IP address, so I have put it on every byte of the IP address the data is converted into decimal, then "" shown separately, this method is referred to as "dotted decimal"
IP composition: a host network number section number section +
Classification IP address:
Composition IP address: the network host number section number section +
class A: segment number is a first network segment number + the number segment three sections host
a network number: 256 256 256 = 16,777,216
Class B: before the network segment No. II host segment number + period after the Sec
a network number: 256 * 256 = 65536
class C: Three segments before the network segment number + number segment host after a period of
a network number: 256
classification IP address:
a class 1.0. 0.1 --- 127.255.255.254 (1) 10.XXX is a private address (private address is not used on the Internet, and is used in local area network address) (2) 127.XXX is reserved addresses, used cycle test use.
Class B 128.0.0.1 --- 191.255.255.254 172.16.0.0 --- 172.31.255.255 is a private address. 169.254.XX is reserved addresses.
Class C 192.0.0.1 --- 223.255.255.254 192.168.XX is a private address
like 224.0.0.1 --- 239.255.255.254 D
E class 240.0.0.1 --- 247.255.255.254
Two DOS command:
ipconfig 查看本机ip地址
ping 后面跟ip地址。测试本机与指定的ip地址间的通信是否有问题
Special IP addresses:
127.0.0.1 回环地址(表示本机)
x.x.x.255 广播地址
x.x.x.0 网络地址
So, if we acquire and operate IP address?
In order to facilitate our acquisition and operation of the IP address, java provides a class InetAddress for our use.
MAC address :( ID number):
网卡的硬件标识
Use of InetAddress
No constructor, then how to make it functional classes offered?
To grasp the function:
static InetAddress the getByName (String Host): According to the host name or IP address of the string representation of the object to get the IP address
to get the host name: getHostName ()
Gets the host Ip address: getHostAddress ()
public static void main(String[] args) throws UnknownHostException {
// public static InetAddress getByName(String host)
// InetAddress address = InetAddress.getByName("liuyi");
// InetAddress address = InetAddress.getByName("192.168.12.92");
InetAddress address = InetAddress.getByName("192.168.12.63");
// 获取两个东西:主机名,IP地址
// public String getHostName()
String name = address.getHostName();
// public String getHostAddress()
String ip = address.getHostAddress();
System.out.println(name + "---" + ip);
}
The port number
Physical port NIC port
We refer to logical port is a logical port
A:每个网络程序都会至少有一个逻辑端口
B:用于标识进程的逻辑地址,不同进程的标识
C:有效端口:0~65535,其中0~1024系统使用或保留端口。
TCP and UDP protocols
UDP
将数据源和目的==封装成数据包==中,不需要建立连接;每个数据报的大小在限制在64k;因无连接,是不可靠协议;不需要建立连接,速度快
TCP
建立连接,形成传输数据的通道,在连接中进行大数据量传输;通过三次握手完成连接,是可靠协议;必须建立连接,效率会稍低
to sum up:
UDP:
- Connectionless-oriented.
- Unreliable.
- high speed.
- The data packet transmission, the maximum data packet 64k.
Example:
chat messages, online video, video conferencing, text messaging, post office parcel.
TCP:
- Connection-oriented (the established connection path).
- Unlimited data
- Safe and reliable
Less efficient.
By three-way handshake to ensure the establishment of the connection.
For example:
download, phone calls,
Socket
Socket programming, network programming, socket programming is the same.
Socket socket:
网络上具有唯一标识的==IP地址和端口号组合==在一起才能构成唯一能识别的标识符套接字。
Socket包含了: IP地址+端口号
Socket principle mechanisms:
- Ends of the communication has Socket.
- In fact, network traffic is communication between the Socket.
- Socket IO between the two data transmission.
Scoket illustrating the principles of communication:
UDP transmission
DatagramSocket: This class represents a socket for sending and receiving datagram packets.
DatagramPacket: This class represents a datagram packet.
Establishing the transmitting side, the receiving end.
Set-up packet.
Socket method of sending and receiving calls.
Close Socket.
Transmission and the receiver are two separate program.
UDP protocol to send and receive data illustrate
UDP transmission - transmitting end Thinking
- Establish udp socket server
- The data to be transmitted is encapsulated into packets
- By udp socket server, the packet is sent out
- Close Resources
public static void main(String[] args) throws IOException {
//1.创建发送端Socket对象
//发送端的Socket对象不需要指定端口
DatagramSocket ds=new DatagramSocket();
//2.创建数据,并把数据打包
byte[] bytes="hello,java".getBytes();
//长度
int length=bytes.length;
//IP地址对象
InetAddress address=InetAddress.getByName("z-atu-72");
//端口
int port=10086;
DatagramPacket dp=new DatagramPacket(bytes,length,address,port);
//3.调用Socket对象的发送方法发送数据包
ds.send(dp);
//4.释放资源
ds.close();
}
Code optimization:
public class SendDemo {
public static void main(String[] args) throws IOException {
// 创建发送端的Socket对象
DatagramSocket ds = new DatagramSocket();
// 创建数据并打包
byte[] bys = "helloworld".getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length,
InetAddress.getByName("192.168.12.92"), 12345);
// 发送数据
ds.send(dp);
// 释放资源
ds.close();
}
}
UDP transmission - reception side ideas:
- Create a UDP Socket object receiving end
- Receiving packet data for creating
- Socket object method call receiver to receive data
- Resolution Packet
- Release resources
import java.io.IOException;
import java.net.*;
public class SendDemo {
public static void main(String[] args) throws IOException {
//1.创建发送端Socket对象
DatagramSocket ds=new DatagramSocket();
//2.创建数据,并把数据打包
byte[] bytes="hello,java".getBytes();
//长度
int length=bytes.length;
//IP地址对象
InetAddress address=InetAddress.getByName("z-atu-72");
//端口
int port=10086;
DatagramPacket dp=new DatagramPacket(bytes,length,address,port);
//3.调用Socket对象的发送方法发送数据包
ds.send(dp);
//4.释放资源
ds.close();
}
}
Code optimization:
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// 创建接收端的Socket对象
DatagramSocket ds = new DatagramSocket(12345);
// 创建一个包裹
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
// 接收数据
ds.receive(dp);
// 解析数据
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from " + ip + " data is : " + s);
// 释放资源
ds.close();
}
}
注意:
多次启动接受端会报java.net.BindException:Address already in use:Cannot bind 异常,表示端口别占用
UDP案例
从键盘录入数据进行发送,如果输入的是886那么客户端就结束输入数据。
TCP传输
Socket和ServerSocket
建立客户端和服务器端
建立连接后,通过Socket中的IO流进行数据的传输
关闭socket
同样,客户端与服务器端是两个独立的应用程序。
TCP传输-客户端思路:
Socket 客户端Socket套接字
1:建立客户端的Socket服务,并明确要连接的服务器。 (这一步如果创建成功,就说明连接已经创建成功)
2:如果连接建立成功,就表明,已经建立了数据传输的通道.就可以在该通道通过IO进行数据的读取和写入.该通道称为Socket流,Socket流中既有读取流,也有写入流.
3:通过Socket对象的方法,可以获取这两个流
4:通过流的对象可以对数据进行传输
5:如果传输数据完毕,关闭资源
/*
* TCP协议发送数据:
* A:创建发送端的Socket对象
* 这一步如果成功,就说明连接已经建立成功了。
* B:获取输出流,写数据
* C:释放资源
*
* 连接被拒绝。TCP协议一定要先开启服务器。
* java.net.ConnectException: Connection refused: connect
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
// 创建发送端的Socket对象
// Socket(InetAddress address, int port)
// Socket(String host, int port)
// Socket s = new Socket(InetAddress.getByName("192.168.12.92"), 8888);
Socket s = new Socket("192.168.12.92", 8888);
// 获取输出流,写数据
// public OutputStream getOutputStream()
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
// 释放资源
s.close();
}
}
TCP传输-服务器端思路
ServerSocket 服务端Socket套接字
1:建立服务器端的socket服务,需要一个端口,
2:服务端没有直接流的操作,而是通过accept方法获取客户端对象,在通过获取到的客户端对象的流和客户端进行通信
3:通过客户端的获取流对象的方法,读取数据或者写入数据
4:如果服务完成,需要关闭客户端,然后关闭服务器,但是,一般会关闭客户端,不会关闭服务器,因为服务端是一直提供服务的
TCP传输案例
服务器给客户端反馈
客户端键盘录入,服务器输出到控制台
客户端键盘录入,服务器输出文本文件
客户端文本文件,服务器输出到控制台
客户端文本文件,服务器输出文本文件
上传图片案例
服务器的代码用线程进行封装,这样可以模拟一个同时接收多人上传文件的服务器。(用循环也可以但是效率低,是单线程的程序)
TCP协议发送和接收数据图解
TCP传输容易出现的问题
客户端连接上服务端,两端都在等待,没有任何数据传输。
通过例程分析:
因为read方法或者readLine方法是阻塞式。
解决办法:
自定义结束标记
使用shutdownInput,shutdownOutput方法。