/*
需求:通过Tcp传输复制文件。
*/
import java.io.*;
import java.net.*;
class ClientDemo3
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.100",10010);
BufferedReader bufr=new BufferedReader(new FileReader("ChatDemo.java"));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String line=null;
while ((line=bufr.readLine())!=null)
{
out.println(line);
}
s.shutdownOutput();//关闭客户端输出流,相当于给一个-1结束标记。
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(bufIn.readLine());
bufr.close();
s.close();
}
}
class ServerDemo3
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10010);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"....connection");
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new FileWriter("1.txt"),true);
String line=null;
while ((line=bufIn.readLine())!=null)
{
out.println(line);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
out.close();
s.close();
ss.close();
}
}
|
|