public class MyServer {
public static void main(String[] args) throws IOException {
/*
* 常见的客户端和服务端:
*
* 客户端:浏览器。
* 服务端:Tomcat
*
* 1,浏览器访问Tomcat服务器。
* 浏览器到底做了什么事情呢?
* 客户端:浏览器。
* 服务器:自定义。
*/
//1,创建服务器端对象。
ServerSocket ss = new ServerSocket(8080);
//2,获取客户端对象。
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connected");
//3,获取socket流中的输入流。
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String str = new String(buf,0,len);
System.out.println(str);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("<font color='red' size='7'>欢迎光临!</font>");
s.close();
ss.close();
/*
HTTPX 协议的请求消息。
GET / HTTP/1.1 //请求行, 请求方式 资源路径 协议版本
请求头的属性信息。键值对组成。
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.1.20:9090
Connection: Keep-Alive
空行。用于区分请求信息和请求体。
请求体。
*/
}
}
可以用浏览器访问localhost:8080/
也可以用程序
public class MyBrowser {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//1,创建客户端对象。
Socket s = new Socket("127.0.0.1",8080);
//2,将socket输出流封装到打印流中。将浏览器发送的信息通过该流打印到服务器端。
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET /myweb/1.html HTTP/1.1");
out.println("Accept: */*");
out.println("Host: 192.168.1.20:8080");
out.println("Connection: close");
out.println();
out.println();
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
/*
http的应答消息。
HTTP/1.1 200 OK //应答行 http协议版本 应答状态码 应答描述信息
应答消息的属性信息。键值对。
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"79-1364954414830"
Last-Modified: Wed, 03 Apr 2013 02:00:14 GMT
Content-Type: text/html
Content-Length: 79
Date: Wed, 03 Apr 2013 03:01:35 GMT
Connection: close
空行。
应答的主体内容
<h1>这是我的网页</h1>
<font color="red" size="7">我的首页资源演示页面</font>
*/
}
}
URL
public class URLDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// String str_url = "http://192.168.1.20:8080/myweb/1.html?name=lisi&age=20";
String str_url = "http://127.0.0.1:8080/";
//将字符串url封装成URL对象。
URL url = new URL(str_url);
//通过url对象的getXXX方法将url路径中的所需的内容获取。
System.out.println("getProtocol:"+url.getProtocol());
System.out.println("getHost:"+url.getHost());
System.out.println("getPort:"+url.getPort());
System.out.println("getPath:"+url.getPath());
System.out.println("getFile:"+url.getFile());
System.out.println("getQuery:"+url.getQuery());
//使用url的 openConnection方法获取对给资源的链接对象。
URLConnection conn = url.openConnection();
System.out.println(conn);
InputStream in = conn.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
}
CS client server.
特点:
1,客户端和服务端都需要开发。
2,后期维护较为麻烦,还需要安装。
3,将服务端的部分运算转到了客户端。
BS Browser Server
特点:
1,程序员只需要编写服务端。
2,对于客户端的维护很容易。客户端什么都不需要装,只需要有浏览器即可。
3,所有的运算都在服务端,压力较大。