网络编程与GUI,URL基础分析
最常见的客户端和服务端:
- 客户端:浏览器
- 服务器:Tomcat
1.客户端:浏览器
在浏览器上输入网络协议,网络的IP地址,端口号及其访问应用程序的名称,包含具体的资源名称.
2.服务端:当我们打开Tomcat安装程序的conf下的sever.xml文件是放资源及其配置端口号及其其他信息.当我们想开启Tomcat服务器时,可以在bin目录下双击startup.bat
访问方式:
浏览器中: 输入http:192.168.1.3:9090/自己的应用程序名称/具体的资源名称。
案例二:
客户端:浏览器
服务端:自定义的服务器SelfDescribeServerSocket
package socket_self;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SelfDescribeServerSocket {
public static void main(String[] args) throws IOException {
System.out.println("服务端运行");
ServerSocket ss=new ServerSocket(9090);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "---------connected");
//读取客户端数据
InputStream sin = s.getInputStream();
byte[] bytes = new byte[1024];
int len = sin.read(bytes);
System.out.println(new String(bytes, 0, len));
//给客户端回馈数据
PrintWriter ps = new PrintWriter(s.getOutputStream(), true);
ps.println("<font color=\"red\" size=\"7\">java hello</font>");
s.close();
ss.close();
}
}
- 但能获取到如下信息并终止服务器
HTTP的请求消息:
GET / HTTP/1.1 //请求行: 请求方式 访问资源的路径 HTTP协议版本.
属性信息(键值对来存储)
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 支持的类型
Accept-Language: zh-CN
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763
Accept-Encoding: gzip, deflate
Host: 192.168.1.3:9090
Connection: Keep-Alive
模拟一个浏览器
- 客户端:定义一个浏览器
- 服务端:Tomcat
package gui_socket_url;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
public class GUIsocket extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUIsocket frame = new GUIsocket();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUIsocket() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(0, 0, 264, 53);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("\u63D0\u4EA4");
button.setBounds(279, 0, 123, 53);
contentPane.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
actionJButton(e);
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 62, 402, 167);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
private void actionJButton(ActionEvent a) {
try {
createServerSocket();
}catch (IOException e1){
e1.printStackTrace();
}
}
private void createServerSocket() throws IOException {
String urlName = textField.getText();
URL url=new URL(urlName);
Socket s=new Socket(url.getHost(),url.getPort());
OutputStream sOut = s.getOutputStream();
PrintWriter ps=new PrintWriter(sOut,true);
ps.println("GET+"+url.getPath()+" HTTP/1.1");
ps.println("Accept: */*");
ps.println("Host:"+url.getPort());
ps.println("Connection: close");
//空行
ps.println();
InputStream sin = s.getInputStream();
byte [] bytes=new byte[1024];
int len = sin.read(bytes);
String text=new String(bytes,0,len);
textArea.setText(text);
s.close();
}
}
URL对象
package gui_socket_url;
import java.net.MalformedURLException;
import java.net.URL;
public class UrlDemo {
public static void main(String[] args) throws MalformedURLException {
String ur="http://192.168.1.3:8080/myWeb/demo.html?name=lisi";
//将url封装成对象
URL url=new URL(ur);
System.out.println("获取协议:"+url.getProtocol());
System.out.println("获取主机:"+url.getHost());
System.out.println("获取端口:"+url.getPort());
System.out.println("获取路径:"+url.getPath());
System.out.println("获取文件:"+url.getFile());
System.out.println("获取查询功能:"+url.getQuery());
/*
获取协议:http
获取主机:192.168.1.3
获取端口:8080
获取路径:/myWeb/demo.html
获取文件:/myWeb/demo.html
获取查询功能:null
*/
}
}
使用UrlConnection对象获取流对象,优化程序
package gui_socket_url;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
public class GUIsocket extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUIsocket frame = new GUIsocket();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUIsocket() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(0, 0, 264, 53);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("\u63D0\u4EA4");
button.setBounds(279, 0, 123, 53);
contentPane.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
actionJButton(e);
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 62, 402, 167);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
private void actionJButton(ActionEvent a) {
try {
createServerSocket();
}catch (IOException e1){
e1.printStackTrace();
}
}
private void createServerSocket() throws IOException {
String urlName = textField.getText();
URL url=new URL(urlName);
URLConnection urc=url.openConnection();
InputStream uin = urc.getInputStream();
byte [] bytes=new byte[1024];
int len=uin.read(bytes);
textArea.setText(new String(bytes,0,len));
// Socket s=new Socket(url.getHost(),url.getPort());
//
// OutputStream sOut = s.getOutputStream();
// PrintWriter ps=new PrintWriter(sOut,true);
//
// ps.println("GET+"+url.getPath()+" HTTP/1.1");
// ps.println("Accept: */*");
// ps.println("Host:"+url.getPort());
// ps.println("Connection: close");
// //空行
// ps.println();
//
// InputStream sin = s.getInputStream();
// byte [] bytes=new byte[1024];
//
// int len = sin.read(bytes);
// String text=new String(bytes,0,len);
// textArea.setText(text);
// s.close();
}
}
HTTP版本性能
- HTTP1.0:一次连接一次请求
- HTTP1.1:一次连接多次请求
常见的网络架构
C/S : Client / server
特点:
1.程序员需要开发客户端和服务端
2.维护较为麻烦
3.将一部分运算转移到客户端来完成,减轻服务端的压力.B/S:browser / server
特点:
1.程序员只需要开发服务端。客户端使用已有的浏览器即可
2.维护升级很简单,只需要维护服务端即可
3.所有的运算都在服务端。
目前B/S比较流行。