zoukankan      html  css  js  c++  java
  • 网络编程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();
        
        }
    }

    我真的很想把我写的代码与你分享
  • 相关阅读:
    二分查找 && 三分查找
    LeetCode || 大杂烩w
    LeetCode || 递归 / 回溯
    LeetCode || 双指针 / 单调栈
    HITICS || 2018大作业 程序人生 Hello's P2P
    思维 || Make It Equal
    国庆集训 || Wannafly Day4
    国庆集训 || Wannafly Day1
    区间DP || HDU 6249 Alice’s Stamps
    10进制转k进制
  • 原文地址:https://www.cnblogs.com/zhazhenyu1992/p/5617899.html
Copyright © 2011-2022 走看看