• 网络编程TCP


    先写接收端:

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

    public class ServerTcp {

        /**
         * @author xiaozhazi
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            //1.create one ServerSocket and monitor the port
            ServerSocket ss=new ServerSocket(10004);
            
            //2.get the Socket that client-side send
            Socket s=ss.accept();
            
            //3.get the inputStream  by socket
            InputStream in =s.getInputStream();
            String ip=s.getInetAddress().getHostAddress();
            System.out.println(ip+"---has connected");
            
            //4read the data
            byte[] buf= new byte[1];
            int len=-1;
            while((len=in.read(buf))!=-1){
                System.out.print(new String(buf, 0, len));
            }
            
            //close
            s.close();
            
            

        }

    }

    写客户端

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

    public class ClientTcp {
        /**
         * @author xiaozhazi
         * @param args
         * @throws IOException
         * @throws UnknownHostException
         */
        public static void main(String[] args) throws UnknownHostException, IOException {
            //1.create the Socket service
            Socket s=new Socket("192.168.2.36", 10004);
            
            
            //2.get the outputstream by this you can send data to server
            OutputStream out =s.getOutputStream();
            
            //3.send data
            out.write("xiao zha zi dashi".getBytes());
            
            //4.close
            s.close();
        
        }
    }

    我真的很想把我写的代码与你分享
  • 相关阅读:
    React 组件之间如何交流
    VMC INJECTION(开源JAVA模板框架)
    <th><td>表单用法
    弹性盒子
    骰子的布局(flex)
    javascript中的作用域
    js引用类型和基本类型、隐式类型转换以及强制类型转换面试题
    css的content属性,以及如何通过css content属性实现css计数器?
    CSS实现:一个矩形内容,有投影,有圆角,hover状态慢慢变透明
    百度元宵节动画
  • 原文地址:https://www.cnblogs.com/zhazhenyu1992/p/5617899.html
Copyright © 2020-2023  润新知