1.InetAddress的应用
import java.util.List;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack; import javax.naming.spi.DirStateFactory.Result; import org.omg.PortableInterceptor.DISCARDING; import java.util.Map;
import java.util.Queue;
/**
* InetAddress类
* @author 16114
*
*/
public class Main{
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("计算机名:" + address.getHostName());
System.out.println("IP地址:" + address.getHostAddress());
byte[] bytes = address.getAddress();
System.out.println("字节数组形式的IP:" + Arrays.toString(bytes));
System.out.println(address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
InetAddress address2 = InetAddress.getByName("Roni");
System.out.println(address2.getHostName());
System.out.println(address2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
} }
}
2.URL
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack; import javax.naming.spi.DirStateFactory.Result; import org.omg.PortableInterceptor.DISCARDING; import java.util.Map;
import java.util.Queue;
/**
* URL类
* @author 16114
*
*/
public class Main{
public static void main(String[] args) { try {
URL url = new URL("http://www.baidu.com");
try {
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String data = br.readLine();
while(data!=null) {
System.out.println(data);
data=br.readLine();
}
br.close();
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace(); } } catch (MalformedURLException e) {
e.printStackTrace();
} }
}
3.客户端向服务器端的通信
Server
package com.zrl.thread; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; /**
* 基于TCP协议的Socket协议
* 服务端
* @author 16114
*
*/
public class Server {
public static void main(String[] args) {
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口
try {
ServerSocket serverSocket = new ServerSocket(8888);
//2.调用accept()方法开始监听,等待客户端的连接
System.out.println("***服务器即将启动,等待客户端的链接***");
Socket socket = serverSocket.accept();
//3.获取输入流,并读取客户端信息
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String info = null;
while((info=br.readLine())!=null) {
System.out.println("我是服务器,客户端说:" + info);
}
socket.shutdownInput();
//4.关闭相关资源
br.close();
isr.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client
package com.zrl.thread; import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException; public class Client {
public static void main(String[] args) {
//1.创建客户端Socket,指定服务器端地址和端口
Socket socket;
try {
socket = new Socket("localhost", 8888);
//2.获取输出流,向服务器发送消息
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
pw.write("用户名:admin;密码:123");
pw.flush();
socket.shutdownInput();
//3.关闭资源
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}
一个服务端,多个客户端
Server.java
package com.zrl.thread; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; /**
* 基于TCP协议的Socket协议
* 服务端
* @author 16114
*
*/ public class Server {
public static void main(String[] args) {
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口
try {
ServerSocket serverSocket = new ServerSocket(8888);
//2.调用accept()方法开始监听,等待客户端的连接
System.out.println("***服务器即将启动,等待客户端的链接***");
Socket socket = null;
//记录客户端的数量
int cnt = 0;
//循环监听等待客户端的连接
while(true) {
//调用accept()方法开始监听,等待客户端的连接
socket = serverSocket.accept();
//创建一个新线程
ServerThread serverThread = new ServerThread(socket);
//启动线程
serverThread.start();
cnt++;
System.out.println("客户端的数量:" + cnt);
InetAddress address = socket.getInetAddress();
System.out.println("当前客户端的IP:" + address.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
package com.zrl.thread; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException; public class Client {
public static void main(String[] args) {
//1.创建客户端Socket,指定服务器端地址和端口
Socket socket;
try {
socket = new Socket("localhost", 8888);
//2.获取输出流,向服务器发送消息
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
pw.write("用户名:admin; 密码:456");
pw.flush();
socket.shutdownOutput();
//3.获取输入流并且读取服务器端的响应信息
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String info = null;
while((info=br.readLine())!=null) {
System.out.println("我是客户端,服务端说:" + info);
}
//4.关闭资源
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}
ServerThread.java
package com.zrl.thread;
/**
* 服务器端线程处理类
* @author 16114
*
*/ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket; public class ServerThread extends Thread {
//和本线程相关的Socket
Socket socket = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
//线程执行的操作,响应客户端的请求
@Override
public void run() {
//获取输入流,并读取客户端信息
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os =null;
PrintWriter pw = null;
try {
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String info = null;
while((info=br.readLine())!=null) {
System.out.println("我是服务器,客户端说:" + info);
}
socket.shutdownInput();
//获取输出流,响应客户端的请求
os = socket.getOutputStream();
pw = new PrintWriter(os); //包装为打印流
pw.write("欢迎您!");
pw.flush(); //调用flush将缓冲输出 } catch (IOException e) {
e.printStackTrace();
}finally {//关闭相关资源
try {
if(pw!=null) pw.close();
if(os!=null) os.close();
if(br!=null) br.close();
if(isr!=null) isr.close();
if(is!=null) is.close();
if(socket!=null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}