//一、获取客户端HTTP协议
//1、模拟http请求,先开启一个serverSocket服务,开启下面类Server.main()方法
//2、然后开启浏览器,输入http://127.0.0.1:10002
//在服务器窗口即打印出
//GET / HTTP/1.1
//Accept: */*
//Accept-Language: zh-cn
//User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.
//0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Cent
//er PC 6.0; .NET4.0C; .NET4.0E)
//Accept-Encoding: gzip, deflate
//Host: 127.0.0.1:10002
//Connection: Keep-Alive
import java.net.*;
import java.io.*;
class Server
{
public static void main(String[] args) throws Exception
{
final ServerSocket server = new ServerSocket(10002);
while(true)
{
final Socket socket = server.accept();
new Thread(new Runnable()
{
public void run()
{
try
{
String ip = socket.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while((line = br.readLine())!=null)
{
System.out.println(line);
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("服务器有问题");
}finally
{
System.out.println("finally");
if(socket !=null)
try
{
socket.close();
}
catch (Exception e)
{
System.out.println("socket 关闭失败");
}
}
}
}).start();
}
}
}
二、获取服务器返回的http协议
//1、开启本地的tomcat服务
//2、将下面Client2中第一句代码的端口改为 tomcat的端口,运行Client2的main()
//即可获取
/*
HTTP/1.1 505 HTTP Version Not Supported
Server: Apache-Coyote/1.1
Date: Mon, 26 May 2014 05:58:58 GMT
Connection: close
*/
class Client2
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),10002);
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("#break");
pw.println("GET / HTTP/1.1");
pw.println("Accept: */*");
pw.println("Accept-Language: zh-cn");
pw.println("Accept-Encoding: gzip, deflate");
pw.println("Host: 127.0.0.1:10002");
pw.println("Connection: closed");
pw.println();
pw.println();
pw.println("#break");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while((line = br.readLine())!=null)
{
System.out.println(line);
}
socket.close();
}
}
//三、模拟http协议发送、与接收
//开启Server2 Client2 即模拟实现
class Server2
{
public static void main(String[] args) throws Exception
{
final ServerSocket server = new ServerSocket(10002);
while(true)
{
final Socket socket = server.accept();
new Thread(new Runnable()
{
public void run()
{
try
{
String ip = socket.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
String breakPoint = br.readLine();
while((line = br.readLine())!=null)
{
if(breakPoint.equals(line))
break;
else
System.out.println(line);
}
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("HTTP/1.1 505 HTTP Version Not Supported");
pw.println("Server: Apache-Coyote/1.1");
pw.println("Date: Mon, 26 May 2014 05:58:58 GMT");
pw.println("Connection: close");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("服务器有问题");
}finally
{
if(socket !=null)
try
{
socket.close();
}
catch (Exception e)
{
System.out.println("socket 关闭失败");
}
}
}
}).start();
}
}
}