//Return the local host name and IP address;
InetAddress i = InetAddress.getLocalHost();
i.getHostAddress();//IP
i.getAddress();//???
//Get computer related information through computer name;
InetAddress i = InetAddress.getByName("Livingstone-PC");
//Get host related information through domain name
InetAddress ibaidu = InetAddress.getByName("www.baidu.com");
URL url = new URL("http://localhost:8080/demo.html");
url.getHost();
Socket(client):
When the object is established, you can connect to the specified host. Because TCP is connection-oriented, when establishing the socket service, a server must exist and the connection is successful. After the connection is established, data transmission is performed on the channel;
//Create the client's socket service and specify the target host and port;
Socket s = new Socket("cj-PC", 10003);
// In order to send data, the output stream in the socket stream should be obtained;
OutputStream out = s.getOutputStream();
//PrintWriter out = new PrintWriter(s.getOutputStream(),true);out.println("Hello");
out.write("hello".getBytes());
//Receive the reply message
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn, 0, num));
s.close();//The stream object in is encapsulated in the socket and automatically closes the stream object;
ServerSocket (server):
Establish the server-side socket service, ServerSocket, and listen to a port;
Get the connected client object through the object's accept method. If there is no connection, it will wait (blocking):
If the client sends data, the server must use the read stream of the corresponding client object to obtain the data sent by the client;
ServerSocket ss = new ServerSocket(10003);
//ServerSocket(int port,int backlog); backlog is the maximum number of connections;
Socket s = ss.accept();
InputStream in = s.getInputStream();
byte[] buf = new byte[2014];
int len = in.read(buf);
String rec = new String(buf, 0, len);
System.out.println(rec);
// send back message
OutputStream out = s.getOutputStream();
out.write("received".getBytes());
s.close();//The server will automatically close the client;
(1)Client:
Establish a socket service and specify the host and port to connect to;
Get the output stream in the socket stream, write the data to the stream, and send it to the server through the network;
Obtain the input stream in the socket stream, obtain the data fed back by the server, and close the client resources;
DatagramSocket: Send a piece of text data through UDP transmission;
Send:
//Create udp service, create a DatagramSocket object and give a product number;
DatagramSocket socket = new DatagramSocket(8888);
// Determine the data and encapsulate it into a data packet, DatagramPacket (the port number of the destination machine needs to be specified);
byte[] buf = "udp I'm coming".getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("Machine-Name"), 10086);
// send;
socket.send(dp);
// closure;
socket.close();
Receive:
//Define udpsocket service. Usually a port is monitored, which actually defines a digital ID for the receiving network application;
DatagramSocket socket = new DatagramSocket(10086);
while (true) { //The purpose is to continuously monitor
byte[] buf = new byte[1024];
//Define data package for storing data;
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// Store the received data into the data packet through the receive method of the service;
socket.receive(dp);//Blocking method, stuck here if there is no data;
// Obtain the data through the data packet method;
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
int port = dp.getPort();
}
Socket s = new Socket("Machine-Name", 10005);
OutputStream out = s.getOutputStream();
FileInputStream fis = new FileInputStream("awf.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
out.write(buf, 0, len);
}
// Stop sending data
s.shutdownOutput();
ServerSocket ss = new ServerSocket(10005); //ss.accept() method has blocking effect;
// A separate thread can be created for each Socket obtained by the accept() method;