/*
演示客户端和服务端。
1.
客户端:浏览器(telnet)
服务端:自定义服务端
2.
客户端:浏览器
服务端:tomcat服务器
3.
客户端:自定义(要向服务端发送什么请求呢?)
服务端:tomcat服务器
*/
import java.io.*;
import java.net.*;
class TcpClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.101",8080);
InputStream in = s.getInputStream();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET /myweb/demo.html HTTP/1.1");//向tomcat端请求浏览demo.html网页
out.println("Accept:*/*");
out.println("Accept-Language: zh-cn");
out.println("Host: 192.168.1.101:8080");
out.println("Connection: Keep-Alive");
out.println();
out.println();
BufferedReader bufrIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=bufrIn.readLine())!=null)
System.out.println(line);
s.close();
}
}
/*
客户端(浏览器) 向 服务端(自定义) 发送的内容:
GET / HTTP/1.1
Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image
/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msw
ord, application/QVOD, application/QVOD,
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0)
Accept-Encoding: gzip, deflate
Host: 192.168.1.101:11000
Connection: Keep-Alive
*/
/*
现在自定义一个客户端,向tomcat服务端发送标准内容,要求浏览demo.html页面,
看看tomcat服务端向自定义的客户端发什么内容呢?
以下就是tomcat向自定义客户端发送的内容:
192.168.1.101...connected
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"347-1430970502515"
Last-Modified: Thu, 07 May 2015 03:48:22 GMT
Content-Type: text/html
Content-Length: 347
Date: Thu, 07 May 2015 07:00:36 GMT
Connection: close
<html>
<body>
<h1>This is my page</h1>
<font size=5 color=red>welcome to my page</font>
<div>
李开复哈佛路就阿狸犯贱啊浪费空间</br>
李开复哈佛路就阿狸犯贱啊浪费空间</br>
李开复哈佛路就阿狸犯贱啊浪费空间</br>
李开复哈佛路就阿狸犯贱啊浪费空间</br>
李开复哈佛路就阿狸犯贱啊浪费空间</br>
</div>
</body>
</html>
*/
day24/TcpServerDemo2.java
最新推荐文章于 2022-03-23 23:35:32 发布