客户端
package cn.itcast_02;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ClientDome {
public static void main(String[] args) throws IOException {
Socket s = new Socket("10.212.31.21",45679);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
String line = null;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
s.close();
}
}
服务器输出在文本文件
package cn.itcast_02;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDome {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(45679);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
String line = null;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
s.close();
}
}