1.java.net.InetAddress类
概述:一个该类的对象就代表一个IP地址对象
成员方法:
- public static InetAddress getLocalHost():获取本机IP地址对象
- public static InetAddress getByName(String host):根据ip地址字符出啊或主机名称获得对应的ip地址对象
- String getHostName():获得主机名称
- String getHostAddress():获得IP地址字符串
// 获得本地主机IP地址对象
InetAddress ip1 = InetAddress.getLocalHost();
System.out.println(ip1);
// 根据ip地址字符出啊或主机名称获得对应的ip地址对象
InetAddress ip2 = InetAddress.getByName("192.168.0.111");
System.out.println(ip2);
// 获取网络中设备的ip地址对象
InetAddress ip3 = InetAddress.getByName("www.baidu.com");
System.out.println(ip3);
// 获取主机名称
String hostName = ip3.getHostName();
System.out.println(hostName);
// 获取ip地址
String hostAddress = ip3.getHostAddress();
System.out.println(hostAddress);
2.客户端向服务器端发送字符串数据
客户端
public class Client {
public static void main(String[] args) throws IOException {
// 1.创建Socket对象,指定要连接的服务器ip和端口号
Socket socket = new Socket("127.0.0.1",6666);
// 2.通过Socket对象获取输出流
OutputStream os = socket.getOutputStream();
// 3.写出数据到服务器端
os.write("服务器,你好啊,出来约啊?".getBytes());
// 4.关闭Socket
socket.close();
}
}
服务器端
public class Server {
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定服务器的端口号
ServerSocket ss = new ServerSocket(6666);
// 2.调用accept()方法,接受客户端的请求,返回Socket对象
Socket socket = ss.accept();
// 3.使用返回的Socker对象获得输入流
InputStream in = socket.getInputStream();
// 4.读取客户端写过来的数据
byte[] bytes = new byte[8192];
int len;
while((len = in.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));
}
// 5.关闭ServerSocket(一般不关闭)
ss.close();
}
}
3.服务器回写字符串数给客户端
客户端
public class Client {
public static void main(String[] args) throws IOException {
// 1.创建Socket对象,指定要连接的服务器ip和端口号
Socket socket = new Socket("127.0.0.1",6666);
while(true){
// 2.通过Socket对象获取输出流
OutputStream os = socket.getOutputStream();
// 3.写出数据到服务器端
Scanner s = new Scanner(System.in);
String next = s.next();
os.write(next.getBytes());
// 4.通过Socket对象获得输入流
InputStream in = socket.getInputStream();
// 5.读取服务器写过来的数据
byte[] bytes = new byte[8192];
int len = in.read(bytes);
System.out.println(new String(bytes,0,len));
}
// 6.关闭Socket
//socket.close();
}
}
服务器端
public class Server {
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定服务器的端口号
ServerSocket ss = new ServerSocket(6666);
// 2.调用accept()方法,接受客户端的请求,返回Socket对象
Socket socket = ss.accept();
while(true){
// 3.使用返回的Socker对象获得输入流
InputStream in = socket.getInputStream();
// 4.读取客户端写过来的数据
byte[] bytes = new byte[8192];
int len = in.read(bytes);
System.out.println(new String(bytes,0,len));
// 5.使用返回的Socket对象获得输出流
OutputStream out = socket.getOutputStream();
// 6.写出数据到客户端
Scanner s = new Scanner(System.in);
String next = s.next();
out.write(next.getBytes());
}
// 7.关闭ServerSocket(一般不关闭)
//ss.close();
}
}
4.文件上传案例
客户端
public class Client {
public static void main(String[] args) throws IOException {
// 1.创建Socket对象,指定要连接的服务器的ip和端口号
Socket socket = new Socket("127.0.0.1",6666);
// 2.创建字节输入流对象,关联数据源文件路径
FileInputStream fis = new FileInputStream("day14_网络编程\\resources\\11.jpg");
// 3.通过Socket对象获取输出流
OutputStream os = socket.getOutputStream();
// 4.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 5.定义一个int变量,用来存储读取到的字节个数
int len;
// 6.循环读取数据
while( (len = fis.read(bytes)) != -1 ){
// 7.在循环中,写出数据
os.write(bytes,0,len);
}
// 8.关闭流
fis.close();
socket.close();
}
}
服务器端
public class Server {
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定端口号即可
ServerSocket ss = new ServerSocket(6666);
// 2.调用accept()方法接受客户端的请求,得到Socket对象
Socket socket = ss.accept();
// 3.通过返回的Socket对象获得输入流
InputStream is = socket.getInputStream();
// 4.创建字节输出流对象,关联目的地文件路径
FileOutputStream fos = new FileOutputStream("day14_网络编程\\resources2\\11_copy.jpg");
// 5.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 6.定义一个int变量,用来存储读取到的字节个数
int len;
// 7.循环读取数据
while( (len = is.read(bytes)) != -1 ){
// 8.在循环中,写出数据
fos.write(bytes,0,len);
}
// 9.关闭流
fos.close();
ss.close();
}
}
5.文件上传成功后服务器写回字符串数据
客户端
public class Client {
public static void main(String[] args) throws IOException {
// 1.创建Socket对象,指定要连接的服务器的ip和端口号
Socket socket = new Socket("127.0.0.1",6666);
// 2.创建字节输入流对象,关联数据源文件路径
FileInputStream fis = new FileInputStream("day14_网络编程\\resources\\11.jpg");
// 3.通过Socket对象获取输出流
OutputStream os = socket.getOutputStream();
// 4.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 5.定义一个int变量,用来存储读取到的字节个数
int len;
// 6.循环读取数据
while( (len = fis.read(bytes)) != -1 ){
// 7.在循环中,写出数据
os.write(bytes,0,len);
}
/*
* 在文件上传时,客户端从文件中读不到数据,就会停止发送,文件上传完毕
* 但是服务器不知道客户端停止了写数据(客户端已经发送完毕)
* 那么服务器会一直等待接收客户端继续写数据过来
* 所以,我们需要在客户端上传数据完毕之后,通知服务器发送结束
* */
// 通知服务器发送结束
socket.shutdownOutput();
System.out.println("=====开始接收服务器回写的数据=====");
// 8. 通过socket对象获得输入流
InputStream inputStream = socket.getInputStream();
// 9.读取服务器端回写的字符串数据
int read = inputStream.read(bytes);
System.out.println(new String(bytes,0,read));
// 8.关闭流
fis.close();
socket.close();
}
}
服务器端
public class Server {
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定端口号即可
ServerSocket ss = new ServerSocket(6666);
// 2.调用accept()方法接受客户端的请求,得到Socket对象
Socket socket = ss.accept();
// 3.通过返回的Socket对象获得输入流
InputStream is = socket.getInputStream();
// 4.创建字节输出流对象,关联目的地文件路径
FileOutputStream fos = new FileOutputStream("day14_网络编程\\resources2\\11_copy1.jpg");
// 5.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 6.定义一个int变量,用来存储读取到的字节个数
int len;
// 7.循环读取数据
while( (len = is.read(bytes)) != -1 ){
// 8.在循环中,写出数据
fos.write(bytes,0,len);
}
System.out.println("=====开始写回数据到客户端=====");
// 9.通过Socket对象获取输出流
OutputStream outputStream = socket.getOutputStream();
// 10.写出字符串给客户端
outputStream.write("文件上传成功".getBytes());
// 9.关闭流
fos.close();
ss.close();
}
}
6.优化文件上传案例_文件名优化
客户端
public class Client {
public static void main(String[] args) throws IOException {
// 1.创建Socket对象,指定要连接的服务器的ip和端口号
Socket socket = new Socket("127.0.0.1",6666);
// 2.创建字节输入流对象,关联数据源文件路径
FileInputStream fis = new FileInputStream("day14_网络编程\\resources\\11.jpg");
// 3.通过Socket对象获取输出流
OutputStream os = socket.getOutputStream();
// 4.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 5.定义一个int变量,用来存储读取到的字节个数
int len;
// 6.循环读取数据
while( (len = fis.read(bytes)) != -1 ){
// 7.在循环中,写出数据
os.write(bytes,0,len);
}
/*
* 在文件上传时,客户端从文件中读不到数据,就会停止发送,文件上传完毕
* 但是服务器不知道客户端停止了写数据(客户端已经发送完毕)
* 那么服务器会一直等待接收客户端继续写数据过来
* 所以,我们需要在客户端上传数据完毕之后,通知服务器发送结束
* */
// 通知服务器发送结束
socket.shutdownOutput();
System.out.println("=====开始接收服务器回写的数据=====");
// 8. 通过socket对象获得输入流
InputStream inputStream = socket.getInputStream();
// 9.读取服务器端回写的字符串数据
int read = inputStream.read(bytes);
System.out.println(new String(bytes,0,read));
// 8.关闭流
fis.close();
socket.close();
}
}
服务器端
public class Server {
// 该方法返回当前时间
public static String now_date() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String format = sdf.format(date);
return format;
}
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定端口号即可
ServerSocket ss = new ServerSocket(6666);
// 2.调用accept()方法接受客户端的请求,得到Socket对象
Socket socket = ss.accept();
// 3.通过返回的Socket对象获得输入流
InputStream is = socket.getInputStream();
// 4.创建字节输出流对象,关联目的地文件路径
FileOutputStream fos = new FileOutputStream("day14_网络编程\\resources2\\"+now_date()+".jpg");
// 5.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 6.定义一个int变量,用来存储读取到的字节个数
int len;
// 7.循环读取数据
while( (len = is.read(bytes)) != -1 ){
// 8.在循环中,写出数据
fos.write(bytes,0,len);
}
System.out.println("=====开始写回数据到客户端=====");
// 9.通过Socket对象获取输出流
OutputStream outputStream = socket.getOutputStream();
// 10.写出字符串给客户端
outputStream.write("文件上传成功".getBytes());
// 9.关闭流
fos.close();
ss.close();
}
}
测试类
public class Tests {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String format = sdf.format(date);
System.out.println(format);
}
}
7.优化服务器只能接收一次文件上传的问题
public class Server {
// 该方法返回当前时间
public static String now_date() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String format = sdf.format(date);
return format;
}
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定端口号即可
ServerSocket ss = new ServerSocket(6666);
while(true){
// 2.调用accept()方法接受客户端的请求,得到Socket对象
Socket socket = ss.accept();
// 3.通过返回的Socket对象获得输入流
InputStream is = socket.getInputStream();
// 4.创建字节输出流对象,关联目的地文件路径
FileOutputStream fos = new FileOutputStream("day14_网络编程\\resources2\\"+now_date()+".jpg");
// 5.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 6.定义一个int变量,用来存储读取到的字节个数
int len;
// 7.循环读取数据
while( (len = is.read(bytes)) != -1 ){
// 8.在循环中,写出数据
fos.write(bytes,0,len);
}
System.out.println("=====开始写回数据到客户端=====");
// 9.通过Socket对象获取输出流
OutputStream outputStream = socket.getOutputStream();
// 10.写出字符串给客户端
outputStream.write("文件上传成功".getBytes());
// 9.关闭流
fos.close();
socket.close();
//ss.close();
}
}
}
8.线程优化并发操作
public class Server {
// 该方法返回当前时间
public static String now_date() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String format = sdf.format(date);
return format;
}
public static void main(String[] args) throws IOException {
// 1.创建ServerSocket对象,指定端口号即可
ServerSocket ss = new ServerSocket(6666);
while(true){
// 2.调用accept()方法接受客户端的请求,得到Socket对象
Socket socket = ss.accept();
// 只要有一个客户端进来,就立马开辟一个线程去操作
new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream fos = null;
try {
// 3.通过返回的Socket对象获得输入流
InputStream is = socket.getInputStream();
// 4.创建字节输出流对象,关联目的地文件路径
fos = new FileOutputStream("day14_网络编程\\resources2\\"+now_date()+".jpg");
// 5.定义一个字节数组,用来存储读取到的字节数据
byte[] bytes = new byte[8192];
// 6.定义一个int变量,用来存储读取到的字节个数
int len;
// 7.循环读取数据
while( (len = is.read(bytes)) != -1 ){
// 8.在循环中,写出数据
fos.write(bytes,0,len);
}
System.out.println("=====开始写回数据到客户端=====");
// 9.通过Socket对象获取输出流
OutputStream outputStream = socket.getOutputStream();
// 10.写出字符串给客户端
outputStream.write("文件上传成功".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 9.关闭流
try {
fos.close();
socket.close();
//ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}