学习的第34天。
总结():
获取类对象:
通过类的对象,获取类对象:
Student s= new Student();
Class c = s.getClass();
通过类名获取类对象:
Class c = 类名.class;
通过静态方法获取类对象
Class c = Class.forName(“包名.类名”);
工厂设计模式:
开发中有一个非常重要的原则“开闭原则”,对拓展开放、对修改关闭。
工厂模式主要负责对象创建的问题。
习题:
C14.5: 代码补全:
public class Client{
public static void main(String[] args) throws Exception{
Socket s;
//创建一个到“127.0.0.1:9000”的TCP连接
s = new Socket("192.0.0.1",9000);
//向TCP连接输出"Hello World"并换行
PrintWriter pw = new PrintWriter(
new OutputStreamWriter(s.getOutputStream(),"UTF-8"));
pw.println("Hello World");
//从服务器端读入一行文本,并打印出来
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream(),"UTF-8"));
System.out.println(br.read());
s.close();
}
}
class Server {
public static void main(String[] args) {
ServerSocket ss = null;
// 创建一个服务器端口对象
try {
ss = new ServerSocket(9000);
// 获得一个客户的连接
Socket s = ss.accept();
// 读入一行文本
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
String line = br.readLine();
// 在读入的文本后面加上+“From Server”
String newLine = line + "From Server";
// 把处理之后的文本向客户端输出并换行
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"));
pw.println(newLine);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭连接
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
C14.6: 多线程TCP:
常见一个多线程的TCP服务器以及客户端,完成下面的功能:读入客户端发给服务端的字符串,然后把所有字母转换成大写之后,再发送给客户端。
class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9001);
while (true) {
Socket s = ss.accept();
ServerThread st = new ServerThread(s);
st.start();
}
}
}
class ServerThread extends Thread {
private Socket s;
public ServerThread(Socket s) {
this.s = s;
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
String str = br.readLine();
String newStr = str.toUpperCase();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"));
pw.println(newStr);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("192.168.1.105", 9001);
PrintWriter pw = new PrintWriter(s.getOutputStream());
Scanner input = new Scanner(System.in);
pw.println(input.next());
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
System.out.println(br.readLine());
s.close();
}
}
C13.7: 多线程TCP:
创建一个多线程的TCP服务器以及客户端,完成下面的功能:
服务端:读入客户发给服务器端的字符串,在服务器当前目录下查找该字符串作为文件名的文件,并把该文件内容发送给客户端。
客户端:发送给服务器端一个字符串filename表示服务器上的一个文件,然后从服务器端读入文件内容,并起名叫server_filename保存在当前目录。例如,假设服务器当前目录下有个myphoto.jpg文件,则客户端发送字符串“myphoto.jpg”给服务端,然后从服务端读入myphoto.jpg文件的内容,并起名为server_myphoto.jpg保存在客户端当前目录下。
class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9007);
while (true) {
Socket s = ss.accept();
SeverThread st = new SeverThread(s);
}
}
}
class SeverThread extends Thread {
private Socket s;
private File file;
public SeverThread(Socket s) {
this.s = s;
}
public void run() {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String fileName = br.readLine();
file = new File(fileName);
if (file.exists()) {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(fileName));
byte[] cache = new byte[100];
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
int len = 0;
while ((len = bis.read(cache)) != -1) {
bos.write(cache);
}
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("192.168.1.105", 9007);
PrintWriter pw = new PrintWriter(s.getOutputStream());
System.out.println("输入文件名:");
Scanner input = new Scanner(System.in);
String name = input.next();
pw.println(name);
pw.flush();
FileOutputStream fos = new FileOutputStream("server_" + name);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
byte[] b = new byte[100];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b);
}
bos.close();
s.close();
}