本帖最后由 叶征东 于 2012-9-6 00:53 编辑
/*
通过网络复制一个文件
*/
import java.net.*;
import java.io.*;
class Client
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("192.168.1.105",10006);
BufferedReader br=new BufferedReader(new FileReader("Chat10.java"));//代码可以通过编译,服务端Server也可以开启.但是一开启客户端时,就提示找不到Chat10.java文件,
//但是Chat10.java文件就在当前目录下的(e:\javacx\23),不知道哪里错了,请指点我一下,谢谢!
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=br.readLine())!=null)
{
out.println(line);
}
s.shutdownOutput();
BufferedReader brIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line1=brIn.readLine();
System.out.println(line1);
s.close();
br.close();
System.out.println("Holle world");
}
}
class Server
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10006);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
BufferedReader brOut=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new FileWriter("IPDmo4_copy.txt"),true);
String line=null;
while((line=brOut.readLine())!=null)
{
pw.println(line);
}
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println("上传成功");
out.close();
s.close();
ss.close();
}
}
|