class Demo96
{
public static void main(String[] args) throws Exception
{
client();
System.out.println("Hello World!");
}
public static void client() throws Exception{
Socket s = new Socket("192.168.0.106", 1236);
File file = new File("f://1.txt");
if(file.exists() == false){
return;
}
//数据源
BufferedReader br = new BufferedReader(new FileReader(file));
//数据目的
BufferedWriter bw_socket =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while((line = br.readLine()) != null){
bw_socket.write(line);
bw_socket.newLine();
bw_socket.flush();
}
//接收来自服务端反馈
BufferedReader br_socket =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br_socket.readLine();
System.out.println(str);
s.close();
br.close();
}
}
class Demo97
{
public static void main(String[] args) throws Exception
{
server();
System.out.println("Hello World!");
}
public static void server()throws Exception {
ServerSocket ss = new ServerSocket(1236);
Socket s = ss.accept();
System.out.println("连接的客户端ip: "+s.getInetAddress().getHostAddress());
//File file = new File("f://1.txt");
//数据源
BufferedReader in_socket =
new BufferedReader(new InputStreamReader(s.getInputStream()));
//数据目的
BufferedWriter bw =
new BufferedWriter(new FileWriter("f://1(1).txt"));
String line = null;
//进行写数据
while((line = in_socket.readLine()) != null){
System.out.println(line);
bw.write(line);
bw.newLine();
bw.flush();
}
//反馈给客户端
BufferedWriter bw_socket =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw_socket.write("upload success!!");
//关闭客户端和复制文件的输出流
ss.close();
s.close();
}
} |