-
- /**
- * 模拟客户端
- */
- public class Client {
- public static void main(String[] args) throws UnknownHostException {
- Socket socket=null;
- OutputStream outputStream=null;
- try {
- //1、 创建socket对象 指明服务器ip地址和端口号
- InetAddress inet=InetAddress.getByName("localhost");//调用的地址
- socket=new Socket(inet,9999);//调用的端口
- //2、获取一个输入流 用于输出数据
- outputStream= socket.getOutputStream();
- //3、写入数据
- outputStream.write("客户端xxxx发送".getBytes(StandardCharsets.UTF_8));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- //4、关闭资源
- try{
- if (outputStream!=null)
- outputStream.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- try{
- if (socket!=null)
- socket.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- // System.out.println(InetAddress.getLocalHost());
- }
- }
-
- /**
- * 服务端(先开启)
- */
- public class Server {
- public static void main(String[] args) throws IOException {
- InputStream inputStream=null;
- ServerSocket socketServer=new ServerSocket(9999);//设置服务器端口
-
- Socket accept = socketServer.accept();//获取获取 客户端的消息 获取sicket对象
- inputStream = accept.getInputStream();//将消息写入流
-
- int len;
- byte [] buf=new byte[5];
- ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
- //将网络传输的流读取下来
- while ((len=inputStream.read(buf))!=-1){
- arrayOutputStream.write(buf,0,len);
- }
- System.out.println(accept.getInetAddress());
- System.out.println(arrayOutputStream.toString("utf-8"));
- //关闭资源
- arrayOutputStream.close();
- inputStream.close();
- accept.close();
- socketServer.close();
-
- }
- }
实现客户端服务器发送文件 并发通知
-
- /**
- * 网络 发送文件 将文件上传
- */
- public class ClentToFile {
- public static void main(String[] args) throws Exception {
- //创建网络通信
- Socket socket = null;
- //创建 输出流 写入网络通信
- OutputStream outputStream = null;
- //指定要传输的对象
- socket = new Socket("127.0.0.1", 9999);
- outputStream = socket.getOutputStream();
- //获取 要传输的 文件流
- FileInputStream fileInputStream = new FileInputStream(new File("E:\\springboot_dome\\IOdome\\dbcp.txt"));
- byte[] buf = new byte[5];
- int len;
- while ((len = fileInputStream.read(buf)) != -1) {
- //把输入流 写到 网络通信的输出流
- outputStream.write(buf, 0, len);
- }
- System.out.println("图片传输完成");
- //关闭数据流传输 告诉服务器我的数据流传输完成
- socket.shutdownOutput();
-
- //5.收到源自服务器的数据,打印纸控制台
- InputStream atServerStream= socket.getInputStream();
- ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
- byte[] bufc = new byte[5];
- int lenc;
- while ((lenc=atServerStream.read(bufc))!=-1){
- byteArrayOutputStream.write(bufc,0,lenc);
- }
- System.out.println(byteArrayOutputStream.toString());
-
-
- //建议用try catch
- outputStream.close();
- fileInputStream.close();
- socket.close();
- byteArrayOutputStream.close();
- atServerStream.close();
-
- }
- }
-
- /**
- * 网络 发送文件 将文件下载
- */
- public class ServerToFile {
- public static void main(String[] args)throws Exception {
- ServerSocket serverSocket=new ServerSocket(9999);
- InputStream inputStream=null;
-
- Socket accept = serverSocket.accept();
- inputStream = accept.getInputStream();
- // 文件写出的目标文件
- FileOutputStream fileOutputStream=new FileOutputStream("SocketFile1.txt");
-
- byte [] buf=new byte[1024];
- int len;
- while ((len=inputStream.read(buf))!=-1){
- fileOutputStream.write(buf,0,len);
- }
- //服务器反馈给客户端信息
- OutputStream osToClient= accept.getOutputStream();
- osToClient.write("服务器已收到".getBytes(StandardCharsets.UTF_8));
-
- fileOutputStream.close();
- inputStream.close();
- serverSocket.close();
- osToClient.close();
- accept.close();
-
- }
- }