socket编写浏览器
Socket socket = new Socket("www.rupeng.com", 80);//www.rupeng.com为域名
//向服务器发送请求
OutputStream ops = socket.getOutputStream();
OutputStreamWriter opsw = new OutputStreamWriter(ops);
//index.shtml相对于域名的相对路径
opsw.append("GET /index.shtml HTTP/1.1\n");
opsw.append("HOST: www.rupeng.com:80\n");
opsw.append("\n");
opsw.flush();
//接受服务器的响应;
InputStream ips = socket.getInputStream();
InputStreamReader ipsReader = new InputStreamReader(ips, "UTF-8");
BufferedReader buffReader = new BufferedReader(ipsReader);
String line;
while((line=buffReader.readLine())!=null)
{System.out.println(line)};
//关闭流
socket编写服务器
ServerSocket serversocket=new ServerSocket(端口号);//创建服务器的端口号
while(true){
Socket clientSocket=serversocket.accept();//创建一个监视客户端请求的socket
OutputStream ops=clientSocket.getOutputStream();
OutputStreamWriter opsw=new OutputStreamWriter(ops);
//服务器响应内容
opsw.wirter("HTTP/1.1 200 OK \r\n");
opsw.write("\r\n");
opsw.write(
"<html><head><body><a href=‘http://www.rupeng.com‘>rupeng.com如鹏网</a></body></head></html>\r\n");
opsw.flush();
//关闭流