On network programming in java
1. Basic
Network programming means programs written to run on a plurality of devices (computers) of these devices are connected through a network.
In java provides the java.net package contains classes and interfaces. They provide low-level communication details. We can use these classes and interfaces to specifically address the problem.
Three cornerstones of the Internet: url html http
Provides support for two common network protocol package java.net:
UDP:. Acronym User Datagram Protocol non-connection-oriented, unsafe protocol is simple, small overhead, high efficiency, limited size, usually less than 60k
TCP: connection-oriented, safe, low efficiency based on 3-way handshake: 1. Dial 2. In response, the data transmission connection 3
2.UDP
1.UDP basic processes: sending end
Use DatagramSocket designated port, create the sender
Preparing data must be converted into a byte array
Encapsulated package DatagramPacket
Transmitting wrapping send (DatagramPacket p)
Release resources
Example 1 transmitting end:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UDPSend1 {
public static void main(String[] args) throws Exception {
System.out.println("发送方启动中...");
// 1.使用DatagramSocket指定端口,创建发送端
DatagramSocket client = new DatagramSocket(6666);
// 2.准备数据 一定要转成字节数组
String data = "落霞与孤鹜齐飞,秋水共长天一色";
byte[] datas = data.getBytes();
// 3.封装成DatagramPacket包裹
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 8888));
// 4.发送包裹send(DatagramPacket p)
client.send(packet);
// 5.释放资源
client.close();
}
}
复制代码
2.UDP basic processes: a receiving end
Use DatagramSocket designated port, create the receiving end
Ready containers, packages wrapped into DatagramPacket
Blocking receive packages receive (DatagramPacket p)
analyze data
Release resources
Example 1 a receiving end:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive1 {
public static void main(String[] args) throws Exception {
System.out.println("接收端准备中.... ");
// 1.使用DatagramSocket指定端口,创建接收端
DatagramSocket server = new DatagramSocket(8888);
// 2.准备容器,封装成DatagramPacket包裹
byte[] container = new byte[1024 * 60];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
// 3.阻塞式接收包裹receive(DatagramPacket p)
server.receive(packet);
// 4.分析数据
byte[] datas = packet.getData();
int len = packet.getLength();
System.out.println(new String(datas, 0, len));
// 5.释放资源
server.close();
}
}
复制代码
3.TCP
1.TCP basic process: Client
Custom Client Socket Socket (String host, int port) to create a stream socket to the specified port number on the specified host
Prepare data
Write IO operation
Release resources
Client Example 2:
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client01 {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("-------------Client-----------");
//1.定义客户端 Socket Socket(String host, int port)
Socket client=new Socket("127.0.0.1",7777);
//2.准备数据
String str="name=zhangsan&pwd=123";
//3.写出 直接从管道中获取流
DataOutputStream data=new DataOutputStream(client.getOutputStream());
data.writeUTF(str);
data.flush();
//4.关闭
data.close();
client.close();
}
}
复制代码
2.TCP basic processes: the server
Defined server ServerSocket ServerSocket (int port) Creates a server socket bound to the specified port
Blocking monitor Socket accept ()
Monitor connection socket and accept it
io operations
Release resources
Server Example 2:
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server01 {
public static void main(String[] args) throws IOException {
System.out.println("-------------Server-----------");
//1.定义服务端
ServerSocket server=new ServerSocket(7777);
//2.阻塞式监听 Socket accept()
Socket client=server.accept();
//3.io操作,直接从管道中获得流
DataInputStream in=new DataInputStream( client.getInputStream());
String str=in.readUTF();
System.out.println(str);
//4.释放资源
in.close();
client.close();
server.close();
}
}
复制代码