qq_18295431
qq_18295431
2015-08-25 04:10
采纳率: 100%
浏览 1.6k

Java网络编程中不可以使用缓存吗??

我使用缓存总是什么都不显示,但是又不抛出异常,也没有什么错!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • beacon_2011
    beaconD 2015-08-25 08:23
    点赞 评论
  • havedream_one
    havedream_one 2015-08-25 15:06

    是不是其他问题啊?可以使用缓存的,可以贴出主要代码

    点赞 评论
  • qq_18295431
    qq_18295431 2015-08-26 02:13

    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class Fuwuqi {
    public static void main(String[] args) {
    try {
    ServerSocket ss = new ServerSocket(8888);
    System.out.println("服务器已经启动,正在等待连接。。。");
    Socket s = ss.accept();

            InputStream in = s.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            StringBuffer sb = new StringBuffer();
            char[] c = new char[2];
            int len = -1;
            while((len = br.read(c)) != -1){
                sb.append(c, 0, len);
            }System.out.println("fff");
            System.out.println(sb);
    
            OutputStream out = s.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            PrintWriter print = new PrintWriter(bos);
            print.println("嘿嘿");
            print.println("嘻嘻");
            bos.flush();
    
            print.close();
            out.close();
            br.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    }

    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class Kehuduan {
    public static void main(String[] args) {
    Socket s;
    try {
    s = new Socket("127.0.0.1", 8888);

            OutputStream out = s.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            PrintStream pw = new PrintStream(bos);
            pw.println("嘿嘿收到了吗?");
            pw.println("收到了回复!");
            bos.flush();
    
            InputStream in = s.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            StringBuffer sb = new StringBuffer();
            char[] c = new char[2];
            int len = -1;
            while((len = br.read(c)) != -1){
                sb.append(c, 0, len);
            }
            System.out.println(sb);
    
            br.close();
            in.close();
            pw.close();
            out.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    

    }

    点赞 评论

相关推荐