Java 网络编程 浏览器客户端-Tomcat服务器
2019-10-07 本文已影响0人
一亩三分甜
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10010);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connected");
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("<font color='red' size='7'>客户端你好</font>");
s.close();
ss.close();
}
}

-----------------疑问为什么浏览器无法加载,地址栏输入192.168.22.89:10011显示safari浏览器不能连接到连接到服务器。

发现如果改为返回给客户端里面的字段带响应头,浏览器即可访问。在windows10的电脑上也试着访问,发现也需要带响应头才能正常访问。在Windows XP系统上不需要带响应头信息能直接响应网页内容,可能是IE浏览器能自动添加响应头。
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10011);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connected");
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
// out.println("<font color='red' size='7'>客户端你好!</font>");
out.println("HTTP/1.1 200 OK\r\n" + //响应头第一行
"Content-Type: text/html; charset=utf-8\r\n" + //简单放一个头部信息
"\r\n" + "<font color='red' size='7'>客户端你好</font>");
s.close();
ss.close();
}
}

启动tomcat服务器
1.在tomcat文件夹的bin目录下,找到startup.sh文件在终端运行。
Snip20191006_9.png
Snip20191006_10.png
- 2.在tomcat文件夹下的webapps目录中新建文件夹myweb,并新建index.html文件。
重要的事情说三遍,这里输入到浏览器中的地址路径是http://192.168.22.89:8080/myweb/index.html而不是http://192.168.22.89:8080/webapps/myweb/index.html。否则会报HTTP Status 404 - /webapps/myweb/index.html。

<!DOCTYPE HTML><html lang="en"><head>
<meta charset="UTF-8">
<html>
<body>
<h1>晶晶网</h1>
<font size=5 color=red>大话西游</font>
<div>
你虚度的今天,正式昨天逝去的人无限向往的明天。
</div>
</body>
</html>

浏览器客户端tomcat服务器
客户端:浏览器(telnet)
服务端:自定义。
客户端:浏览器。
服务端:Tomcat服务器。
客户端:自定义
服务端:Tomcat服务器。
查看服务器收到的请求信息。
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10011);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("<font color='red' size='7'>客户端你好</font>");
s.close();
ss.close();
}
}
//输出
192.168.22.89......connected
GET / HTTP/1.1
Host: 192.168.22.89:10011
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
Connection: keep-alive

获取浏览器访问本地tomcat服务器收到的信息
GET /myweb/index.html HTTP/1.1
Host: 192.168.22.89:10011
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
Connection: keep-alive

自定义浏览器
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MyIE {
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.22.89",8080);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET /myweb/index.html HTTP/1.1");
out.println("Accept: */*");
out.println("Accept-Language: zh-cn");
out.println("Host: 192.168.22.89:8080");
out.println("Connection: Keep-Alive");
out.println();
out.println();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = bufr.readLine())!=null)
{
System.out.println(line);
}
s.close();
}
}
//输出
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"248-1570354775000"
Last-Modified: Sun, 06 Oct 2019 09:39:35 GMT
Content-Type: text/html
Content-Length: 248
Date: Sun, 06 Oct 2019 12:30:27 GMT
<!DOCTYPE HTML><html lang="en"><head>
<meta charset="UTF-8">
<html>
<body>
<h1>晶晶网</h1>
<font size=5 color=red>大话西游</font>
<div>
你虚度的今天,正式昨天逝去的人无限向往的明天。
</div>
</body>
</html>

将Connection由Keep-Alive改为closed后,服务器不会再超时结束响应。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MyIE {
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.22.89",8080);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET /myweb/index.html HTTP/1.1");
out.println("Accept: */*");
out.println("Accept-Language: zh-cn");
out.println("Host: 192.168.22.89:8080");
out.println("Connection: closed");
out.println();
out.println();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = bufr.readLine())!=null)
{
System.out.println(line);
}
s.close();
}
}
//输出
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"248-1570354775000"
Last-Modified: Sun, 06 Oct 2019 09:39:35 GMT
Content-Type: text/html
Content-Length: 248
Date: Sun, 06 Oct 2019 12:38:29 GMT
Connection: close
<!DOCTYPE HTML><html lang="en"><head>
<meta charset="UTF-8">
<html>
<body>
<h1>晶晶网</h1>
<font size=5 color=red>大话西游</font>
<div>
你虚度的今天,正式昨天逝去的人无限向往的明天。
</div>
</body>
</html>

URL方法
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws Exception
{
URL url = new URL("http://192.168.22.40:8080/myweb/index.html");
System.out.println("getProtocol():"+url.getProtocol());
System.out.println("getHost():"+url.getHost());
System.out.println("getPort():"+url.getPort());
System.out.println("getPort():"+url.getPath());
System.out.println("getFile():"+url.getFile());
System.out.println("getQuery():"+url.getQuery());
}
}
//输出
getProtocol():http
getHost():192.168.22.40
getPort():8080
getPort():/myweb/index.html
getFile():/myweb/index.html
getQuery():null
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws Exception
{
URL url = new URL("http://192.168.22.40:8080/myweb/index.html?name=haha&age=30");
System.out.println("getProtocol():"+url.getProtocol());
System.out.println("getHost():"+url.getHost());
System.out.println("getPort():"+url.getPort());
System.out.println("getPort():"+url.getPath());
System.out.println("getFile():"+url.getFile());
System.out.println("getQuery():"+url.getQuery());
}
}
//输出
getProtocol():http
getHost():192.168.22.40
getPort():8080
getPort():/myweb/index.html
getFile():/myweb/index.html?name=haha&age=30
getQuery():name=haha&age=30
端口号若不谢,默认为-1,但是浏览器会将端口号设置为80。
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws Exception
{
URL url = new URL("http://192.168.22.40/myweb/index.html?name=haha&age=30");
System.out.println("getProtocol():"+url.getProtocol());
System.out.println("getHost():"+url.getHost());
System.out.println("getPort():"+url.getPort());
System.out.println("getPort():"+url.getPath());
System.out.println("getFile():"+url.getFile());
System.out.println("getQuery():"+url.getQuery());
}
}
//输出
getProtocol():http
getHost():192.168.22.40
getPort():-1
getPort():/myweb/index.html
getFile():/myweb/index.html?name=haha&age=30
getQuery():name=haha&age=30
URLConnection
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class URLDemo {
public static void main(String[] args) throws Exception
{
urlConnectionMethod();
}
public static void urlConnectionMethod() throws Exception
{
URL url = new URL("http://192.168.22.40:8080/myweb/index.html");
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));
}
}
//输出
sun.net.www.protocol.http.HttpURLConnection:http://192.168.22.40:8080/myweb/index.html
<!DOCTYPE HTML><html lang="en"><head>
<meta charset="UTF-8">
<html>
<body>
<h1>晶晶网</h1>
<font size=5 color=red>大话西游</font>
<div>
你虚度的今天,正式昨天逝去的人无限向往的明天。
</div>
</body>
</html>
DNS
http://192.168.22.40:8080/myweb.index.html直接输入主机名不需要走域名解析DNS。
http://www.baidu.com需要域名解析DNS,将主机名翻译成iP地址。
域名解析之前会先走本地hosts,http://127.0.0.1:8080,http://localhost:8080其实127和localhost的映射关系就在本机上。windows,c:\windows\systems\drivers\ext\hosts,Mac,/etc/hosts。