B:TCP
面向连接,效率低,安全性高
Client:
//创建客户端对象
Socket s = new Socket("127.0.0.1", 10086);
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
OutputStream os = s.getOutputStream();
//读写
int len;
byte[] bys = new byte[1024];
while((len=fis.read(bys)) !=-1){
os.write(bys, 0, len);
}
//写完后,跟服务器结束标记
s.shutdownOutput();
fis.close();
//读取服务器回执消息
InputStream is = s.getInputStream();
byte[] bbs = new byte[1024];
int len2 = is.read(bbs);
System.out.println(new String(bbs,0,len2));
s.close();
Serive:
ServerSocket ss = new ServerSocket(10086);
while(true){
final Socket s = ss.accept();
//多线程
new Thread(){
public void run() {
try {
//流对象
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream("b"+System.currentTimeMillis()+".txt");