zoukankan      html  css  js  c++  java
  • socket经典案例-发送数据

    一:客户端向服务端发送数据。

    服务端:

    package com.company.s;
    
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
        public static void main(String[] args)  throws IOException{
            char[] charArray=new char[3];
            ServerSocket serverSocket=new ServerSocket(8088);
            System.out.println("accept begin="+System.currentTimeMillis());
            Socket socket=serverSocket.accept();
            System.out.println("accept end="+System.currentTimeMillis());
            InputStream inputStream=socket.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            System.out.println("read begin "+System.currentTimeMillis());
            int readLength=inputStreamReader.read(charArray);//阻塞
            while (readLength!=-1){
                String newString=new String(charArray,0,readLength);
                System.out.println(newString);
                readLength=inputStreamReader.read(charArray);
            }
            System.out.println("read end "+System.currentTimeMillis());
            inputStream.close();
            socket.close();
            serverSocket.close();
        }
    }

    2.客户端代码:

    package com.company.s;
    
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class Client {
        public static void main(String[] args) throws Exception{
            System.out.println("socket begin "+System.currentTimeMillis());
            Socket socket=new Socket("localhost",8088);
            System.out.println("socket end "+System.currentTimeMillis());
            Thread.sleep(3000);
            OutputStream outputStream=socket.getOutputStream();
            outputStream.write("我是中国人".getBytes());
            outputStream.close();
            socket.close();
        }
    }

     二:服务端向客户端发送数据

    1.server.java

    package com.company.s;
    
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
        public static void main(String[] args)  throws IOException{
            ServerSocket serverSocket=new ServerSocket(8088);
            System.out.println("accept begin="+System.currentTimeMillis());
            Socket socket=serverSocket.accept();
            System.out.println("accept end="+System.currentTimeMillis());
           OutputStream outputStream=socket.getOutputStream();
           outputStream.write("我是思思博士".getBytes());
           outputStream.close();
           socket.close();
           serverSocket.close();
        }
    }

    2.客户端代码:

    package com.company.s;
    
    
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class Client {
        public static void main(String[] args) throws Exception{
            System.out.println("socket begin "+System.currentTimeMillis());
            Socket socket=new Socket("localhost",8088);
            System.out.println("socket end "+System.currentTimeMillis());
            char[] charBuffer=new char[3];
            InputStream inputStream=socket.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            System.out.println("serverB begin "+System.currentTimeMillis());
            int readLength=inputStreamReader.read(charBuffer);
            System.out.println("serverB end "+System.currentTimeMillis());
            while (readLength!=-1){
                System.out.println(new String(charBuffer,0,readLength));
                readLength=inputStreamReader.read(charBuffer);
            }
            System.out.println();
            inputStream.close();
            socket.close();
    
            System.out.println("client 运行结束="+System.currentTimeMillis());
    
        }
    }
  • 相关阅读:
    js或css文件后面的参数是什么意思?
    查看mysql语句运行时间的2种方法
    lumia手机wp系统应用列表如何设置按照拼音
    每一个你不满意的现在,都有一个你没有努力的曾经。
    一行代码实现防盗链!
    请写一个php函数,可以接受任意数量的参数
    ajax处理缓冲问题
    Textarea自动适用高度且无滚动条解决方案
    PHP Warning exec() has been disabled for security reasons怎么办
    各种字符编码方式详解及由来(ANSI,UNICODE,UTF-8,GB2312,GBK)
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/10971323.html
Copyright © 2011-2022 走看看