/*
从客户端上传一个文本数据到服务端。
实际上就是两台电脑间的传输。
*/
import java.io.*;
import java.net.*;
class TcpClient4
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.100",10006);
BufferedReader bufr = new BufferedReader(new FileReader("IPDemo.java"));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
s.shutdownOutput();//关闭客户端的输出流。相当于给流中加入一个结束标记-1
BufferedReader bufrIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufrIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class TcpServer4
{
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 bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufw = new BufferedWriter(new FileWriter("copy_IPDemo.java"));
String line = null;
while((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
BufferedWriter bufwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bufwOut.write("上传成功");
bufwOut.newLine();
bufwOut.flush();
bufw.close();
s.close();
}
}