????????????????紫????????????????socket????????????????????????????λ??????????????????????????Щ??????????????????/??????????????????????????????/???????????а????????????????????????????????????????
????1.????????

 

package sterning;
import  java .io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
int port = 8821;
void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// ?????д???????
String filePath = "D:\lib.rar";
File fi = new File(filePath);
System.out.println("???????:" + (int) fi.length());
// public Socket accept() throws
// IOException????????????????????????????????????????????????
s = ss.accept();
System.out.println("????socket????");
DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
dis.readByte();
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(s.getOutputStream());
//????????????????????????????????????????????????????????????????????????????μ?Think In Java 4th???????????
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf?? 0?? read);
}
ps.flush();
// ?????socket??????????????????server???????????
// ???socket????????????????????
fis.close();
s.close();
System.out.println("??????????");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new ServerTest().start();
}
}