☆ C/S ( Client/Server ) 客户端和服务端的特点

1、客户端和服务端的软件都需要程序员进行编写。
2、客户端维护起来较为麻烦。
3、客户端的存在可以将一部分运算分离到客户端来运行,减轻了服务器端的压力。

☆ B/S ( Browse/Server ) 浏览器和服务端的特点

1、客户端不用程序员编写,直接使用系统中具备的浏览器软件作为客户端即可。程序员只需要编写服务器端就OK了。
2、维护起来也很容易,因为只要维护服务器即可。
3、所有的运算都在服务器端,相对压力较大。

 

编程练习(一):自定义一个服务器,接收浏览器发来的信息。显示浏览器发送了什么信息,并向浏览器发送简单的网页信息。

~~~~这里考虑用多线程来做,允许多个用户来操作!

package cn.hucu.bs;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;

public class MyServer {
public static void main(String[] args) {
ServerSocket server = null;
Socket s = null;
PrintWriter out = null;

try {
server = new ServerSocket(3333); //需要一个端口号,这里自定义了9999
s = server.accept(); // 阻塞
String ip = s.getInetAddress().getHostAddress(); // 获取对方IP
byte buf[] = new byte[1024];
InputStream in = s.getInputStream(); //获取Socket内容
// int len = in.read(buf);
// String str = new String(buf, 0, len);
// System.out.println(str);
int len= 0;
while((len=in.read(buf))!=-1){
String txt = new String(buf,0,len);
System.out.println(txt);
}
// -给浏览器发个简单的网页,要采用http协议即要给浏览器发送相应的响应头
out = new PrintWriter(s.getOutputStream(), true);
out.println("HTTP/1.1 200 OK");// HTTP协议中的响应行
out.println("Content-Type: text/html;charset=GBK"); // 设置编码
out.println("Connection:keep-alive");
out.println();
//以下是一个HTML网页
out.println("<html>");
out.println("<h2>欢迎光临....</h2>");
out.println("<font size=15 color='blue'>这是我用java程序写的服务器,给你发网页,很辛苦....<br/>");
out.println("<table border = 2 cellspacing = 0 bordercolor = 'red'>");
out.println("<tr> <td>姓名</td> <td>年龄</td> </tr>");
out.println("<tr> <td>Jack</td> <td>22</td> </tr>");
out.println("<tr> <td>Rose</td> <td>23</td> </tr>");
out.println("</table>");
out.println("<br/>友情链接:<a href='http://blog.net/shylogo'>博客地址</a></font>");
out.println("</html>");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "网页发送失败!");
return;
} finally {
if (out != null) {
out.close();
}
if (server != null) {
try {
server.close();
} catch (IOException e) {
throw new RuntimeException("关流失败,很危险!!!");
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
throw new RuntimeException("关流失败,很危险!!!");
}
}
}

}
}

浏览器给出来应答结果:

 

127.0.0.1  connection.....
GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:3333
Connection: Keep-Alive

 

 

编程练习(二):

模拟一个浏览器客户端向服务器发请求,接收并显示响应消息。

package cn.hucu.bs;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MyBrowse {
public static void main(String[] args) {
Socket s=null;
try {
s = new Socket("news.baidu.com", 80);
//向服务器发送请求(主页)
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET / HTTP/1.1");
out.println("Accept: text/html, application/xhtml+xml, */*");
out.println("Host:news.baidu.com");
out.println("Connection: Keep-Alive");
out.println();
//读取服务器的应答
InputStream in = s.getInputStream();
byte buf[] = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1){
String str = new String(buf,0,len,"GBK");
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
}

}

}

结果为:网页源码.

编程练习(三):3、网络蜘蛛,收集网页中的邮箱地址信息。

@Test //到网络页面中收集(爬)邮箱地址
public void getMails2() throws IOException{
//源
//URL url = new URL("http://www.sina.com");
URL url = new URL("http://acm.hdu.edu.cn");
BufferedReader br = new BufferedReader( new InputStreamReader(url.openStream(), "utf-8")); //指定编码以及拿到数据源

//正则工具
String regex ="\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(regex);

String line = null;
while((line=br.readLine())!=null){
//用正则工具进行搜索,结果在m中
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}

}


acm@hdu.edu.cn
acm@hdu.edu.cn
gjavac@gmail.com
gl8997@gmail.com