?
?
package com.hspedu.api;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author 林然
* @version 1.0
* 演示INetAddress类的使用
*/
public class API_ {
public static void main(String[] args) throws UnknownHostException {
//获取本机的INetAddress对象
InetAddress inetAddress =InetAddress.getLocalHost();
System.out.println(inetAddress);
//根据指定的主机名获取 INetAddress对象
InetAddress host = InetAddress.getByName("Admin-Lin-Ran");
System.out.println(host);
//根据一个域名返回InetAddress对象
InetAddress host2 =InetAddress.getByName("www.baidu.com");
System.out.println(host2);
//通过InetAdress对象,获取对应的地址
String hostAddress =host2.getHostAddress();//
System.out.println(hostAddress);
//通过InetAdress对象,获取对应的主机名或者域名
String hostName =host2.getHostName();//
System.out.println(hostName);
}
}
?【SreeverSocket对应多个socket】
【Sever】
package com.hspedu.socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 林然
* @version 1.0
* 服务端
*/
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//思路
//1. 在本机 的 9999 端口监听, 等待连接
// 细节: 要求在本机没有其它服务在监听 9999
// 细节:这个 ServerSocket 可以通过 accept() 返回多个 Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在 9999 端口监听,等待连接..");
//2. 当没有客户端连接 9999 端口时,程序会 阻塞, 等待连接
// 如果有客户端连接,则会返回 Socket 对象,程序继续
Socket socket=serverSocket.accept();
System.out.println("服务端 socket =" + socket.getClass());
//3. 通过 socket.getInputStream() 读取客户端写入到数据通道的数据, 显示
InputStream inputStream = socket.getInputStream();
//4. IO 读取
byte[] buf = new byte[1024];
int readLen = 0;
while((readLen=inputStream.read(buf))!=-1){
System.out.println(new String(buf,0,readLen));//根据读取到的实际长度,显示内容
}
//5.关闭流和 socket
inputStream.close();
socket.close();
serverSocket.close();
}
}
【Client】
package com.hspedu.socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author 林然
* @version 1.0
* 客户端
* 发送hello server给服务端
*/
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//思路
//1. 连接服务端 (ip , 端口)
//解读: 连接本机的 9999 端口, 如果连接成功,返回 Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
System.out.println("客户端 socket 返回=" + socket.getClass());
//2. 连接上后,生成 Socket, 通过 socket.getOutputStream()
// 得到 和 socket 对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3. 通过输出流,写入数据到 数据通道
outputStream.write("hello,server".getBytes());
//关闭流对象和socket
outputStream.close();
socket.close();
}
}
【服务器端】
package com.hspedu.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 林然
* @version 1.0
* 服务端
*/
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
//思路
//1. 在本机 的 9999 端口监听, 等待连接
// 细节: 要求在本机没有其它服务在监听 9999
// 细节:这个 ServerSocket 可以通过 accept() 返回多个 Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在 9999 端口监听,等待连接..");
//2. 当没有客户端连接 9999 端口时,程序会 阻塞, 等待连接
// 如果有客户端连接,则会返回 Socket 对象,程序继续
Socket socket=serverSocket.accept();
System.out.println("服务端 socket =" + socket.getClass());
//3. 通过 socket.getInputStream() 读取客户端写入到数据通道的数据, 显示
InputStream inputStream = socket.getInputStream();
//4. IO 读取
byte[] buf = new byte[1024];
int readLen = 0;
while((readLen=inputStream.read(buf))!=-1){
System.out.println(new String(buf,0,readLen));//根据读取到的实际长度,显示内容
}
// 设置结束标记
socket.shutdownInput();
OutputStream outputStream = socket.getOutputStream();
String str ="hello,client";
outputStream.write(str.getBytes());
// 设置结束标记
socket.shutdownOutput();
//5.关闭流和 socket
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
?【客户端】
package com.hspedu.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author 林然
* @version 1.0
* 客户端,发送 "hello, server" 给服务端
*/
public class SocketTCP02Client {
public static void main(String[] args) throws IOException {
//思路
//1. 连接服务端 (ip , 端口)
//解读: 连接本机的 9999 端口, 如果连接成功,返回 Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
System.out.println("客户端 socket 返回=" + socket.getClass());
//2. 连接上后,生成 Socket, 通过 socket.getOutputStream()
// 得到 和 socket 对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3. 通过输出流,写入数据到 数据通道
outputStream.write("hello,server".getBytes());
// 设置结束标记
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte [] buf =new byte[1024];
int readl=0;
while ((readl=inputStream.read(buf))!=-1){
System.out.println(new String(buf,0,readl));
}
// 设置结束标记
socket.shutdownInput();
//关闭流对象和socket
inputStream.close();
outputStream.close();
socket.close();
}
}
【Sever】
package com.hspedu.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 林然
* @version 1.0
*/
public class SocketTCP03Server {
public static void main(String[] args) throws IOException {
//思路
//1. 在本机 的 9999 端口监听, 等待连接
// 细节: 要求在本机没有其它服务在监听 9999
// 细节:这个 ServerSocket 可以通过 accept() 返回多个 Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在 9999 端口监听,等待连接..");
//2. 当没有客户端连接 9999 端口时,程序会 阻塞, 等待连接
// 如果有客户端连接,则会返回 Socket 对象,程序继续
Socket socket=serverSocket.accept();
System.out.println("服务端 socket =" + socket.getClass());
//3. 通过 socket.getInputStream() 读取客户端写入到数据通道的数据, 显示
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
//4. IO 读取,使用字符流
String str1=bufferedReader.readLine();
System.out.println(str1);
// 设置结束标记
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream));
String str ="hello,client";
bufferedWriter.write(str);
// 设置结束标记
bufferedWriter.newLine();
bufferedWriter.flush();
//5.关闭流和 socket
bufferedReader.close();
bufferedWriter.close();
socket.close();
serverSocket.close();
}
}
?【client】
package com.hspedu.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author 林然
* @version 1.0
*/
public class SocketTCP03Client {
public static void main(String[] args) throws IOException {
//思路
//1. 连接服务端 (ip , 端口)
//解读: 连接本机的 9999 端口, 如果连接成功,返回 Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
System.out.println("客户端 socket 返回=" + socket.getClass());
//2. 连接上后,生成 Socket, 通过 socket.getOutputStream()
// 得到 和 socket 对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3. 通过输出流,写入数据到 数据通道,要求使用字符流
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,server 字符流");
bufferedWriter.newLine();//插入一个换行符,表示输入结束,注意要求对方用realLine()!!!
//如果使用的字符流,需要手动刷新,否则数据不会写入数据通道
bufferedWriter.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String str =bufferedReader.readLine();
System.out.println(str);
// 设置结束标记
//关闭流对象和socket
bufferedReader.close();
bufferedWriter.close();
socket.close();
}
}
?【StreamUtils】
package com.hspedu.upload;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 此类用于演示关于流的读写方法
*
*/
public class StreamUtils {
/**
* 功能:将输入流转换成byte[], 即可以把文件的内容读入到byte[]
* @param is
* @return
* @throws Exception
*/
public static byte[] streamToByteArray(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
byte[] b = new byte[1024];//字节数组
int len;
while((len=is.read(b))!=-1){//循环读取
bos.write(b, 0, len);//把读取到的数据,写入bos
}
byte[] array = bos.toByteArray();//然后将bos 转成字节数组
bos.close();
return array;
}
/**
* 功能:将InputStream转换成String
* @param is
* @return
* @throws Exception
*/
public static String streamToString(InputStream is) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder= new StringBuilder();
String line;
while((line=reader.readLine())!=null){
builder.append(line+"\r\n");
}
return builder.toString();
}
}
【服务端】
package com.hspedu.upload;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 林然
* @version 1.0
* 文件上传的服务器
*/
public class TCPFileUploadServer {
public static void main(String[] args) throws Exception {
//1.服务端在本机监听8888端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端在8888端口监听");
//2等待连接
Socket socket = serverSocket.accept();
//3 读取客户端发送的数据
//通过Socket得到输入流
InputStream inputStream =socket.getInputStream();
BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);
byte[] bytes =StreamUtils.streamToByteArray(bufferedInputStream);
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream
(new FileOutputStream("src\\jmq.jpg"));
bufferedOutputStream.write(bytes);
bufferedOutputStream.close();
//向客户端回复收到图片
//通过socket获取到输出流[字符]
BufferedWriter bufferedWriter =new BufferedWriter
(new OutputStreamWriter(socket.getOutputStream()));
bufferedWriter.write("服务端收到图片");
bufferedWriter.flush();//把内容刷新
//设置结束标记
socket.shutdownOutput();
//关闭其他资源
bufferedWriter.close();
bufferedInputStream.close();
socket.close();
serverSocket.close();
}
}
【客户端】
package com.hspedu.upload;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author 林然
* @version 1.0
* 文件上传的客户端
*/
public class TCPFileUploadClient {
public static void main(String[] args) throws Exception {
//客户端连接服务端 8888端口得到Socket对象
Socket socket=new Socket(InetAddress.getLocalHost(),8888);
//创建读取磁盘文件的输入流
String filePath="e:\\jmq.jpg";
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
//bytes就是filePath对应的字节数组
byte[] bytes= StreamUtils.streamToByteArray(bufferedInputStream);
//通过Socket获取到输出流,将bytes数据发送给服务端
OutputStream outputStream =socket.getOutputStream();
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(outputStream);
bufferedOutputStream.write(bytes);//将文件写入到数据通道
//关闭流
bufferedInputStream.close();
socket.shutdownOutput();
//设置一个结束的标志
//接收服务端的信息
InputStream inputStream=socket.getInputStream();
String message=StreamUtils.streamToString(inputStream);
System.out.println(message);
socket.shutdownInput();
//关闭相关流
inputStream.close();
bufferedOutputStream.close();
socket.close();
}
}
A端口
package com.hspedu.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @author 林然
* @version 1.0
* UDP 接收端
*/
public class UDPReceiverA {
public static void main(String[] args) throws IOException {
//1.创建一个DatagramSocket 对象,准备接受数据
DatagramSocket datagramSocket = new DatagramSocket(9999);
//2.构建一个DatagramPacket准备接受数据
//udp一个数据包大小限制在64K,不适用传递大量数据
byte [] buf =new byte[64*1024];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
//3.调用接收方法
//填充到packet对象
//提示: 当有数据包发送到 本机的 9999 端口时,就会接收到数据
// 如果没有数据包发送到 本机的 9999 端口, 就会阻塞等待
System.out.println("接收端 A 等待接收数据..");
datagramSocket.receive(datagramPacket);
//4 拆包
int length= datagramPacket.getLength();//实际接收到的数据字节长度
byte [] data =datagramPacket.getData();
String s = new String(data, 0, length);
System.out.println(s);
String message="好的,收到";
DatagramPacket datagramPacket1 =new DatagramPacket(message.getBytes(),message.getBytes().length, InetAddress.getLocalHost(),9998);
datagramSocket.send(datagramPacket1);
//关闭
datagramSocket.close();
System.out.println("A端退出");
}
}
?B端口
package com.hspedu.udp;
import java.io.IOException;
import java.net.*;
/**
* @author 林然
* @version 1.0
* 发送端 B ====> 也可以接收数据
*/
public class UDPSenderB {
public static void main(String[] args) throws IOException {
//1.创建 DatagramSocket 对象,准备在 9998 端口 接收数据
DatagramSocket datagramSocket =new DatagramSocket(9998);
//2 将需要发送的数据封装到Datagrampacket对象
byte[] bytes = "hello,明天吃火锅".getBytes();
//说明: 封装的 DatagramPacket 对象 bytes 内容字节数组 , bytes.length , 主机(IP) , 端口
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 9999);
datagramSocket.send(datagramPacket);
//3.=== 接收从 A 端回复的信息
//(1) 构建一个 DatagramPacket 对象,准备接收数据
// 在前面讲解 UDP 协议时,老师说过一个数据包最大 64k
byte [] buf=new byte[1024*64];
DatagramPacket datagramPacket1=new DatagramPacket(buf,buf.length);
// 如果没有数据包发送到 本机的 9998 端口, 就会阻塞等待.
datagramSocket.receive(datagramPacket1);
int length =datagramPacket1.getLength();
byte [] data=datagramPacket1.getData();
System.out.println(new String(data,0,length));
//关闭资源
datagramSocket.close();
System.out.println("B端退出");
}
}
【服务端】
package com.hspedu.homework;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 林然
* @version 1.0
*/
public class homework01_service {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket =new ServerSocket(9999);
System.out.println("服务器端口等待连接");
Socket socket =serverSocket.accept();
//接受数据
InputStream inputStream =socket.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
String str=bufferedReader.readLine();
System.out.println(str);
String message="";
if(str.equals("name")){
message="Linran";
}else if(str.equals("hobby")){
message="java编程";
}else {
message="你说啥呢";
}
OutputStream outputStream=socket.getOutputStream();
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(message,0,message.length());
bufferedWriter.flush();
socket.shutdownOutput();
//关闭流
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
}
}
?【客户端】
package com.hspedu.homework;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* @author 林然
* @version 1.0
*/
public class homework01_client {
public static void main(String[] args) throws IOException {
//建立连接
Socket socket=new Socket(InetAddress.getLocalHost(),9999);
Scanner scanner=new Scanner(System.in);
//用户输入
String message =scanner.next();
//获取输出流
OutputStream outputStream =socket.getOutputStream();
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(message,0,message.length());
bufferedWriter.flush();
socket.shutdownOutput();
//发送等待回复
InputStream inputStream =socket.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
String data =bufferedReader.readLine();
System.out.println(data);
//关闭流
bufferedReader.close();
bufferedWriter.close();
socket.close();
}
}
【接收端】
package com.hspedu.homework;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @author 林然
* @version 1.0
*/
public class homework02_A_receive {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket =new DatagramSocket(9999);
byte [] buf =new byte[1024*64];
DatagramPacket datagramPacket=new DatagramPacket(buf,buf.length);
System.out.println("等待;连接");
datagramSocket.receive(datagramPacket);
int length =datagramPacket.getLength();
byte [] data =datagramPacket.getData();
String message =new String(data,0,length);//注意这里第三个参数不要用data.length
System.out.println(message);
String answer =" ";
if("四大名著是哪些".equals(message)){
answer="四大名著是<红楼梦>........";
}
else {
answer="What?";
}
System.out.println(answer);
DatagramPacket datagramPacket1 =new DatagramPacket(answer.getBytes(),answer.getBytes().length, InetAddress.getLocalHost(),9998);
datagramSocket.send(datagramPacket1);
//关闭流
datagramSocket.close();
}
}
?【发送端】
package com.hspedu.homework;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
/**
* @author 林然
* @version 1.0
*/
public class homework02_B_send {
public static void main(String[] args) throws IOException {
//建立我方监听端口
DatagramSocket datagramSocket =new DatagramSocket(9998);
//等待用户输入
Scanner scanner =new Scanner(System.in);
String message=scanner.next();
//封装
DatagramPacket datagramPacket =new DatagramPacket(message.getBytes(),message.getBytes().length, InetAddress.getLocalHost(),9999);
//发送
datagramSocket.send(datagramPacket);
//获取回复信息
byte [] buf =new byte[1024*64];
DatagramPacket datagramPacket1 =new DatagramPacket(buf,buf.length);
datagramSocket.receive(datagramPacket1);
int length =datagramPacket1.getLength();
byte [] data =datagramPacket1.getData();
String answer =new String(data,0,length);
System.out.println(answer);
//关闭流
datagramSocket.close();
}
}
【服务器端】
package com.hspedu.homework;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 林然
* @version 1.0
*/
public class homework03_server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket=new ServerSocket(9999);
System.out.println("等待连接");
Socket socket =serverSocket.accept();
//获取数据
InputStream inputStream =socket.getInputStream();
//利用工具类
String filename=StreamUtils.streamToString(inputStream);
System.out.println("客户希望下载的文件名"+filename);
//判断是否存在
String filename1="src\\"+filename;
File file =new File(filename1);
System.out.println("客户希望下载的文件名"+filename1);
String send_filepath =filename1;
if(!file.exists()){
send_filepath="src\\无名.mp3";
}
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(send_filepath));
//利用工具类
byte [] bytes=StreamUtils.streamToByteArray(bufferedInputStream);
//输出
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(socket.getOutputStream());
bufferedOutputStream.write(bytes);
bufferedOutputStream.flush();
socket.shutdownOutput();
//关闭流
bufferedOutputStream.close();
bufferedInputStream.close();
socket.close();
serverSocket.close();
}
}
【客户端】
package com.hspedu.homework;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* @author 林然
* @version 1.0
*/
public class homework03_client {
public static void main(String[] args) throws Exception {
Scanner scanner =new Scanner(System.in);
//用户输入
System.out.println("请输入你要查找的文件名");
String filename=scanner.next();
//建立连接
Socket socket =new Socket(InetAddress.getLocalHost(),9999);
OutputStream outputStream =socket.getOutputStream();
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream));
//写入
bufferedWriter.write(filename);
bufferedWriter.flush();
socket.shutdownOutput();
//获取回复
InputStream inputStream =socket.getInputStream();
BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);
byte [] data =StreamUtils.streamToByteArray(bufferedInputStream);
String dest_path ="e:\\"+filename;
File file =new File(dest_path);
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(data);
bufferedOutputStream.flush();
//关闭流
bufferedOutputStream.close();
bufferedInputStream.close();
bufferedWriter.close();
socket.close();
}
}
【工具类】
package com.hspedu.homework;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 此类用于演示关于流的读写方法
*
*/
public class StreamUtils {
/**
* 功能:将输入流转换成byte[], 即可以把文件的内容读入到byte[]
* @param is
* @return
* @throws Exception
*/
public static byte[] streamToByteArray(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
byte[] b = new byte[1024];//字节数组
int len;
while((len=is.read(b))!=-1){//循环读取
bos.write(b, 0, len);//把读取到的数据,写入bos
}
byte[] array = bos.toByteArray();//然后将bos 转成字节数组
bos.close();
return array;
}
/**
* 功能:将InputStream转换成String
* @param is
* @return
* @throws Exception
*/
public static String streamToString(InputStream is) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder= new StringBuilder();
String line;
while((line=reader.readLine())!=null){
builder.append(line+"\r\n");
}
return builder.toString();
}
}