问题描述
- Java网络编程中不可以使用缓存吗??
-
我使用缓存总是什么都不显示,但是又不抛出异常,也没有什么错!
解决方案
java网络编程之缓存
java网络编程之缓存(三)
对使用Java Socket网络编程的详细讲解
解决方案二:
http://blog.sina.com.cn/s/blog_616e189f0100s3px.html
解决方案三:
是不是其他问题啊?可以使用缓存的,可以贴出主要代码
解决方案四:
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();
}
}
}