day018客户端服务端传输

tcp ip udp

网络编程

url

udp 只发送

tcp 三次握手 A 你在吗 B我在 A 我送你东西 建立连接

​ 四次挥手 A 我要断开了 B要断开了么 B 我要断开了 你真的要断开了么 A 我断开了 断开连接

任何网络资源都是流

服务端

package com.tcpip;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 20:05 2021/1/23
 * @ Description:服务端
 * @ Modified By:
 * @Version: $version$
 */
public class TcpServer1 {
    public static void main(String[] args) {
        int port = 8000;
        ServerSocket serverSocket =null;
        Socket socket =null;
        InputStream is =null;
        ByteArrayOutputStream bos =null;
        try {
            serverSocket = new ServerSocket(port);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
                System.out.println(bos.toString()+"******");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

客户端

package com.tcpip;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 20:05 2021/1/23
 * @ Description:客户端
 * @ Modified By:
 * @Version: $version$
 */
public class TcpCLient1 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 8000;
            socket = new Socket(inetAddress,port);
            os = socket.getOutputStream();
            os.write("我是你妹".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!= null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

传文件客户端

package com.tcpip;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 20:30 2021/1/23
 * @ Description:客户端
 * @ Modified By:
 * @Version: $version$
 */
public class ClientDemo {
    public static void main(String[] args) {

        //创建连接
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            socket = new Socket(InetAddress.getByName("localhost"), 9000);
            //创建输出流
              os = socket.getOutputStream();
            //读取文件
              fis = new FileInputStream(new File("zhq.jpg"));//这里的文件最好放在项目根目录否则路径写不全会找不到文件
              if (fis == null){
                  System.out.println("文件不存在");
                  return;
              }
            byte[] bytes = new byte[1024];
            int len;
            if((len = fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
                fis.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

传文件服务端

package com.tcpip;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 20:30 2021/1/23
 * @ Description:服务端
 * @ Modified By:
 * @Version: $version$
 */
public class ServerDemo {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        FileOutputStream fos = null;
        InputStream is = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(9000);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            if (is == null){
                System.out.println("is null");
                return;
            }
            fos = new FileOutputStream("zhq000.jpg");
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes))!=-1){
                fos.write(bytes,0,len);

            }
            System.out.println("下载图片完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }   finally {
            try {
                is.close();
                fos.close();
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
上一篇:批量图片处理:如何给多个图片都加上连续的序号?


下一篇:rabbitmq java客户端如何使用