import javaio;
import javanet;
import javatextSimpleDateFormat;
import javautilDate;
public class Server {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(8080);
Socket socket = ssaccept();
BufferedReader in = //获取客户端发过来的消息
new BufferedReader(
new InputStreamReader(socketgetInputStream()));
PrintWriter out = //用于向客户端发送消息
new PrintWriter(
new OutputStreamWriter(
socketgetOutputStream()),true);
while(true){
String message = inreadLine();//读取消息
if("quit"equals(message))//退出命令
break;
String s[] = messagesplit("\\s+");
if("dir"equals(s[0])){
File dir = new File(s[1]);
File[] files = dirlistFiles();
//向客户端发送文件消息
for(File file:files){
Date date = new Date(filelastModified());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//日期格式化
FileInputStream is = new FileInputStream(file);
outprintln(filegetName()+"\t"+dfformat(date)+"\t"+((isavailable()+1023)/1024)+"k");
}
outprintln("ok");
}
}
socketclose();
inclose();
outclose();
ssclose();
}
}
-------------------------------------------------------------------------------------------------------------
import javaio;
import javanet;
public class Client {
public static void main(String[] args) throws Exception{
InetAddress add = InetAddressgetByName(null);
Socket socket = new Socket(add,8080);
BufferedReader in = //获取服务器端发过来的消息
new BufferedReader(
new InputStreamReader(socketgetInputStream()));
PrintWriter out = //用于向服务器端发送消息
new PrintWriter(
new OutputStreamWriter(
socketgetOutputStream()),true);
outprintln("dir d:/test");
String mes;
while(true){
mes = inreadLine();
if("ok"equals(mes))
break;
Systemoutprintln(mes);
}
outprintln("quit");
socketclose();
inclose();
outclose();
}
}
希望能帮到你。。。仍有问题可以追问或者直接HI我。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)