import java.net.InetAddress;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
System.out.println(inetAddress1.getHostName());
System.out.println(inetAddress1.getHostAddress());
System.out.println("--------------------------------------");
InetAddress inetAddress2 = InetAddress.getByName("61.135.169.125");
System.out.println(inetAddress2);
System.out.println(inetAddress2.getHostName());
System.out.println(inetAddress2.getHostAddress());
System.out.println("--------------------------------------");
InetAddress[] inetAddresses = InetAddress.getAllByName("www.baidu.com");
for(InetAddress inetAddress: inetAddresses){
System.out.println(inetAddress);
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
System.out.println();
}
System.out.println("--------------------------------------");
}
}
www.baidu.com/61.135.169.125
www.baidu.com
61.135.169.125
--------------------------------------
/61.135.169.125
61.135.169.125
61.135.169.125
--------------------------------------
www.baidu.com/61.135.169.125
www.baidu.com
61.135.169.125www.baidu.com/61.135.169.121
www.baidu.com
61.135.169.121
--------------------------------------
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress1 = InetAddress.getByName("localhost");
System.out.println(inetAddress1);
System.out.println(inetAddress1.getHostName());
System.out.println(inetAddress1.getHostAddress());
System.out.println("--------------------------------------");
InetAddress inetAddress2 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress2);
System.out.println(inetAddress1.getHostName());
System.out.println(inetAddress1.getHostAddress());
System.out.println("--------------------------------------");
InetAddress inetAddress3 =InetAddress.getLocalHost();
System.out.println(inetAddress3);
System.out.println(inetAddress3.getHostName());
System.out.println(inetAddress3.getHostAddress());
System.out.println("--------------------------------------");
InetAddress inetAddress4 = InetAddress.getLoopbackAddress();
System.out.println(inetAddress4);
System.out.println(inetAddress4.getHostName());
System.out.println(inetAddress4.getHostAddress());
System.out.println("--------------------------------------");
}
}
localhost/127.0.0.1
localhost
127.0.0.1
--------------------------------------
/127.0.0.1
localhost
127.0.0.1
--------------------------------------
PC-Lee/172.27.47.241
PC-Lee
172.27.47.241
--------------------------------------
localhost/127.0.0.1
localhost
127.0.0.1
--------------------------------------
package UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Sender {
public static void main(String[] args) throws IOException {
byte[] msg = "Hello world!".getBytes();
InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); // 目的地址
DatagramPacket packet = new DatagramPacket(msg, msg.length, inetAddress, 6000); // 将数据,目的地址,端口号等信息封装为数据报
DatagramSocket socket = new DatagramSocket(); // 数据报传输套接字
socket.send(packet);
socket.close();
}
}
package UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receiver {
public static void main(String[] args) throws IOException {
byte[] msg = new byte[1024];
DatagramPacket packet = new DatagramPacket(msg, msg.length); // 数据报对象
DatagramSocket socket = new DatagramSocket(6000); // 数据报传输套接字
socket.receive(packet);
int length = packet.getLength(); // 获取数据报中的数据部分长度
String message = new String(msg, 0, length);
System.out.println(message);
socket.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try (Socket socket = new Socket("127.0.0.1", 6522);
FileInputStream fileInputStream = new FileInputStream("d:\\a.log")) {
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
outputStream.write("Client is ready to transfer files.".getBytes()); // 通知服务器客户端已准备就绪
byte[] buf = new byte[1024];
int len = inputStream.read(buf); // 接收服务器准备就绪信息
System.out.println(new String(buf, 0, len));
System.out.println("Transferring...");
while((len = fileInputStream.read(buf))!=-1){
outputStream.write(buf,0,len);
}
socket.shutdownOutput(); // 向服务器写入一个终止标志
// 接收服务器的传输成功
len = inputStream.read(buf);
System.out.println(new String(buf,0,len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Server {
public static void main(String[] args) {
//规则: 域名+毫秒值+6位随机数
String filename = "lee" + System.currentTimeMillis() + String.format("%06d", new Random().nextInt(999999)) + ".log";
try (ServerSocket serverSocket = new ServerSocket(6522);
FileOutputStream fileOutputStream = new FileOutputStream("d:\\" + filename)) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
byte[] buf = new byte[1024];
int len = inputStream.read(buf);
System.out.println(new String(buf, 0, len));
outputStream.write("Server is ready to receive files.".getBytes());
System.out.println("Receiving...");
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
System.out.println("File Received.");
outputStream.write("File Received.".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
加入多线程的服务器
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class MultiThreadServer {
public static void main(String[] args) {
try(ServerSocket serverSocket = new ServerSocket(6522)){
while (true) {
Socket socket = serverSocket.accept();
new Thread(new Upload(socket)).start();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
class Upload implements Runnable {
private Socket socket;
Upload(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
//规则: 域名+毫秒值+6位随机数
String filename = "lee" + System.currentTimeMillis() +
String.format("%06d", new Random().nextInt(999999)) + ".log";
try (FileOutputStream fileOutputStream = new FileOutputStream("d:\\" + filename)) {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
byte[] buf = new byte[1024];
int len = inputStream.read(buf); // 接收客户端准备就绪信息
System.out.println(new String(buf, 0, len));
outputStream.write("Server is ready to receive files.".getBytes()); // 通知服务器已准备就绪
System.out.println("Receiving...");
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
System.out.println("File Received.");
outputStream.write("File Received.".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用线程池的服务器
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadPoolServer {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
try (ServerSocket serverSocket = new ServerSocket(6522)) {
while (true) {
Socket socket = serverSocket.accept();
executorService.execute(new Upload(socket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Upload implements Runnable {
private Socket socket;
Upload(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
//规则: 域名+毫秒值+6位随机数
String filename = "lee" + System.currentTimeMillis() +
String.format("%06d", new Random().nextInt(999999)) + ".log";
try (FileOutputStream fileOutputStream = new FileOutputStream("d:\\" + filename)) {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
byte[] buf = new byte[1024];
int len = inputStream.read(buf); // 接收客户端准备就绪信息
System.out.println(new String(buf, 0, len));
outputStream.write("Server is ready to receive files.".getBytes()); // 通知服务器已准备就绪
System.out.println("Receiving...");
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
System.out.println("File Received.");
outputStream.write("File Received.".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
class URLTest {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("http://localhost:8080/examples/hello.txt");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream("test.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
}
参考书籍:《疯狂Java讲义》
参考文章:http://www.runoob.com/java/java-tutorial.html