
Маш хялбархан вэб үйлчлүүлэгчийн жишээ хөтөлбөрийг дор харуулав. Үүгээр ямар ч вэб үйлчлэгч рүү GET хүсэлт илгээн хариу авч болно. Гэхдээ хариугаа зөвхөн бичвэр хэлбэрээр авна. Өмнөх хичээлээр бүтээсэн маш хялбархан вэб үйлчлэгчтэй ярилцсан харилцан яриаг жишээ болгон хавсаргав.
package web.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
*
* @author Erdenebat Byambaa, USI company
*/
public class SimplestClient {
static int PORT = 1010;
static String NEWLINE = "\r\n";
static String getHeader = "GET / HTTP/1.1" + NEWLINE;
static String hostHeader = "Host: localhost:" + PORT + NEWLINE;
static String agentHeader ="User-Agent: Simple Client" + NEWLINE;
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println("SimplestClient: starting...");
// connecting to server
Socket socket = new Socket("localhost", PORT);
// obtaining input & output stream from socket
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
// sending GET request to server
out.write(getHeader.getBytes());
out.write(hostHeader.getBytes());
out.write(agentHeader.getBytes());
// end of request
out.write(NEWLINE.getBytes());
// preparing to receive response from server
int data = 0; // holds single byte;
byte[] line = new byte[256]; // used to compose/collect a line by sequentally reading single byte from input stream
// a line is terminated by two bytes: 13 10
boolean CR = false; // true, if data == 13 (or 0d)
boolean NL = false; // true, if data == 10 (or 0a) and preceding data == 13
int lineCounter = 0;
System.out.println("SimplestClient: header start");
// starting to recieve request from server
while ( (data = in.read()) > -1 ) {
line[lineCounter] = (byte)data;
lineCounter++;
String hex = Integer.toHexString(data);
hex = (hex.length()==1? "0"+hex: hex);
System.out.print(hex + " ");
// simple state machine used to detect line terminator
switch (data) {
case 13: CR = true; break;
case 10: NL = (CR ? true: false); break;
default: CR = false; NL = false; break;
}
if ( CR && NL ) {
if (lineCounter == 2) {
// empty line contains only two bytes: 13 10
// it happens at the end of request header
break;
} else {
// reached at the end of line
// let's build a string representing a line and print it
byte[] temp = new byte[lineCounter];
System.arraycopy(line, 0, temp, 0, lineCounter);
System.out.println();
System.out.print(new String(temp));
System.out.println();
lineCounter = 0;
CR = false;
NL = false;
}
}
} // end of header part
System.out.println();
System.out.println("SimplestClient: header end");
// starting to receive content part
int contentCounter = 0;
byte[] tempContent = new byte[1024];
while ( (data = in.read()) > -1 )
tempContent[contentCounter++] = (byte)data;
byte[] actualContent = new byte[contentCounter];
System.arraycopy(tempContent, 0, actualContent, 0, contentCounter);
System.out.println();
System.out.println("SimplestClient: content start");
System.out.println("SimplestClient: content length = " + contentCounter);
System.out.println(new String(actualContent));
System.out.println("SimplestClient: content end");
System.out.println();
socket.close();
System.out.println("SimplestClient: ending...");
}
}
энэ хөтөлбөрийн гарц:
SimplestClient: starting...
SimplestClient: header start
48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d 0a
HTTP/1.1 200 OK
43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 65 78 74 2f 68 74 6d 6c 0d 0a
Content-Type: text/html
43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 38 39 0d 0a
Content-Length: 89
44 61 74 65 3a 20 54 75 65 20 4e 6f 76 20 31 34 20 31 31 3a 31 30 3a 30 37 20 55 4c 41 54 20 32 30 30 36 0d 0a
Date: Tue Nov 14 11:10:07 ULAT 2006
53 65 72 76 65 72 3a 20 53 69 6d 70 6c 65 73 74 20 57 65 62 20 53 65 72 76 65 72 0d 0a
Server: Simplest Web Server
0d 0a
SimplestClient: header end
SimplestClient: content start
SimplestClient: content length = 89
<html><head><title>Welcome</title></head><body><h1>Welcome, Sir/Madame</h1></body></html>
SimplestClient: content end
SimplestClient: ending...
No comments:
Post a Comment