• TCP通信的实现代码


    TCP通信

    概念

    传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议。

    从百科定义中就可以看出,TCP通信的基本条件是连接,传输的是字节流。

    • 通信基本流程(类似打招呼的方式):
      1.三次握手
      2.四次挥手

    客户端

        //客户端
        public class TCPDemo01 {
            public static void main(String[] args) throws Exception {
                //1.要知道服务器的ip,端口号
                InetAddress ip = InetAddress.getByName("127.0.0.1");//ip
                int port = 9999;//端口号
                //创建socket
                Socket socket = new Socket(ip,port);
                //发送消息
                OutputStream os = socket.getOutputStream();
        
                os.write("你好".getBytes());
        
                //关闭资源
                os.close();
                socket.close();
        
            }
        
        }
    

    服务端

        //服务端
        public class TCPDemo02 {
            public static void main(String[] args) throws Exception {
                //1.有一个端口号
                ServerSocket serverSocket = new ServerSocket(9999);
                //2.等待客户端连接
                Socket socket = serverSocket.accept();//客户端的socket
                //3.读取客户端消息
                InputStream is = socket.getInputStream();
        
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int len;
                while ((len=is.read(buf))!=-1){
                    baos.write(buf,0,len);
                }
                System.out.println(baos.toString());
        
                //关闭资源
                baos.close();
                is.close();
                socket.close();
                serverSocket.close();
        
            }
        }
    
    • 用TCP协议实现上传文件到服务端
      客户端
        public class client01 {
            public static void main(String[] args) throws Exception {
                //1.获取服务端ip和端口
                InetAddress ip = InetAddress.getByName("127.0.0.1");
                int port = 9999;
                //2.创建socket对象
                Socket socket = new Socket(ip,port);
                //3.获取输出流
                OutputStream os = socket.getOutputStream();
                //4.创建文件输入流
                FileInputStream fis = new FileInputStream("F:/桌面文件/a.txt");
                //5.写文件
                byte[] buf = new byte[1024];//缓冲区
                int len;
                while ((len=fis.read(buf))!=-1){
                    os.write(buf,0,len);
                }
                //通知传输结束
                socket.shutdownOutput();//输出完了
        
                //等服务端接收完
                InputStream is = socket.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buf2 = new byte[1024];
                int len2;
                while ((len2=is.read(buf2))!=-1){
                    baos.write(buf2,0,len2);
                }
                System.out.println(baos.toString());
                //6.关闭资源
                baos.close();
                is.close();
                fis.close();
                os.close();
                socket.close();
            }
        }
    

    服务端

       public class servlet01 {
           public static void main(String[] args) throws IOException {
               //1.创建服务端接口
               ServerSocket serverSocket = new ServerSocket(9999);
               //2.等待连接
               Socket socket = serverSocket.accept();
               //3.输入流
               InputStream is = socket.getInputStream();
               //4.创建文件输出流
               FileOutputStream fos = new FileOutputStream("receive.txt");
               //5.输出文件
               byte[] buf = new byte[1024];
               int len;
               while ((len=is.read(buf))!=-1){
                   fos.write(buf,0,len);
               }
               //通知客户端,接收完毕
               OutputStream os = socket.getOutputStream();
               os.write("接受完毕".getBytes());
               //6.关闭资源
               fos.close();
               is.close();
               socket.close();
               serverSocket.close();
       
           }
       
       }
    
  • 相关阅读:
    Maven: 每次更新Maven Project ,JAVA 版本都变为1.5 规格严格
    OpenFeign 出现The bean xxx.FeignClientSpecification could not be registered问题解决 规格严格
    富文本编辑保存的时候报错Data truncation: Data too long for column 'content' at row 1 规格严格
    cPickle.so:: PyUnicodeUCS2_DecodeUTF8 规格严格
    Poitl Documentation 规格严格
    maven指定编译版本 规格严格
    SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型
    SQL Server 2008空间数据应用系列四:基础空间对象与函数应用
    SQL Server 2008空间数据应用系列二:空间索引(Spatial Index)基础
    SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型
  • 原文地址:https://www.cnblogs.com/shimmernight/p/13441711.html
Copyright © 2020-2023  润新知