--------------------------------------------第一部分-----------------------------------------------
--------------------------------------------HttpClient.java---------------------------------------
package com.kawa.ch25;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class HttpClient {
public static void main(String[] args) {
try {
// 实例化套接字
Socket socket = new Socket("127.0.0.1", 4700);
// 向本机的4700端口发出客户请求
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// 输出请求结果
System.out.println(in.readLine());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--------------------------------------------HttpServer.java---------------------------------------
package com.kawa.ch25;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
public HttpServer() {
try {
ServerSocket server = new ServerSocket(4700);// 实例化
while (true) {
Socket socket = server.accept();// 获取请求套接字
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println(getHtml());// 输出流
out.flush();// 刷新流
out.close();// 关闭输出流
socket.close();// 关闭套接字
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new HttpServer();
}
private String getHtml() {
StringBuffer buff = new StringBuffer();
buff.append("<html>").append("<title>socket</title>").append("<body>")
.append("<h1>helllo socket !!!!!!!!!</h1>").append("</body>")
.append("</html>");
return buff.toString();
}
}
--------------------------------------------第二部分----------------------------------------------
--------------------------------------------HttpServer.java--------------------------------------
package com.kawa.ch26;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
public HttpServer() {
try {
ServerSocket ss = new ServerSocket(4800);
while (true) {
Socket s = ss.accept();
new HttpSession(s).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new HttpServer();
}
}
--------------------------------------------HttpSession.java-------------------------------------
package com.kawa.ch26;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
public class HttpSession extends Thread {
Socket socket;
PrintStream out;
public HttpSession(Socket s) {
this.socket = s;
try {
out = new PrintStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
out.println(getHtml());
out.flush();
out.close();
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getHtml() {
StringBuffer buff = new StringBuffer();
buff.append("<html>").append("<title>socket</title>").append("<body>")
.append("<h1>helllo socket !!!!!!!!!</h1>").append("</body>")
.append("</html>");
return buff.toString();
}
}
--------------------------------------------HttpClient.java---------------------------------------
package com.kawa.ch26;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class HttpClient {
public static void main(String[] args) {
try {
// 实例化套接字
Socket socket = new Socket("127.0.0.1", 4800);
// 向本机的4700端口发出客户请求
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// 输出请求结果
System.out.println(in.readLine());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--------------------------------------------结果---------------------------------------------------
<!--EndFragment-->