`
- 浏览:
51633 次
- 性别:
- 来自:
南宁
-
- import java.io.*;
- import java.net.*;
- public class TcpServer {
- public static void main(String[] args) throws IOException {
- ServerSocket serverSocket = new ServerSocket(9080);
-
- System.out.println("TcpServer在9080端口上侦听......");
- while (true) {
- try {
- Socket socket = serverSocket.accept();
- System.out.println("建立一个连接...");
-
- RequestThread request = new RequestThread(socket);
- request.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- import java.io.*;
- import java.net.*;
- public class RequestThread extends Thread {
- Socket socket = null;
- public RequestThread(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- SocketRequest socketRequest = new SocketRequest(this.socket);
- socketRequest.readData();
- }
- }
暗暗
- import java.io.*;
- import java.net.*;
- public class SocketRequest {
- private Socket socket = null;
- private InputStream input = null;
- private String uri;
- private StringBuffer request = new StringBuffer();
- private int CONTENT_LENGTH = 0;
- private boolean bePost = false;
- private boolean beHttpResponse = false;
- private boolean beChucked = false;
- private boolean beGet = false;
- private byte crlf13 = (byte) 13;
- private byte crlf10 = (byte) 10;
- private String requestFile = System.currentTimeMillis() + "";
- public SocketRequest(Socket socket) {
- try {
- this.socket = socket;
- this.input = socket.getInputStream();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- public void readData() {
-
- ReadHeader();
- if (beChucked)
- {
- int ChuckSize = 0;
- while ((ChuckSize = getChuckSize()) > 0)
- {
- readLenData(ChuckSize + 2);
- }
- readLenData(2);
- }
- if (CONTENT_LENGTH > 0) {
- readBody(CONTENT_LENGTH);
- }
- try {
- this.socket.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void readLenData(int size)
- {
- int readed = 0;
- try {
- int available = 0;
- if (available > (size - readed))
- available = size - readed;
- while (readed < size) {
- while (available == 0) {
-
- available = input.available();
- }
- if (available > (size - readed))
- available = size - readed;
- if (available > 2048)
- available = 2048;
- byte[] buffer = new byte[available];
- int reading = input.read(buffer);
- request = request.append(new String(buffer, 0, reading));
- readed += reading;
- }
- } catch (IOException e) {
- System.out.println("Read readLenData Error!");
- }
- }
- private void readBody(int size) {
- int readed = 0;
- try {
- int available = 0;
- if (available > (size - readed))
- available = size - readed;
- while (readed < size) {
- while (available == 0) {
-
- available = input.available();
- }
- if (available > (size - readed))
- available = size - readed;
- if (available > 2048)
- available = 2048;
- byte[] buffer = new byte[available];
- int reading = input.read(buffer);
-
- if (!new File(this.requestFile).exists())
- this.createFile(this.requestFile);
- this.writeIntoFile(this.requestFile, buffer, available, readed);
- readed += reading;
- }
- } catch (IOException e) {
- System.out.println("Read Body Error!");
- }
- }
- private void ReadHeader()
- {
- byte[] crlf = new byte[1];
- int crlfNum = 0;
- try {
- while (input.read(crlf) != -1)
- {
- if (crlf[0] == crlf13 || crlf[0] == crlf10) {
- crlfNum++;
- } else {
- crlfNum = 0;
- }
- request = request.append(new String(crlf, 0, 1));
- if (crlfNum == 4)
- break;
- }
- } catch (IOException e) {
- System.out.println("Read Http Header Error!");
- return;
- }
- String tempStr = (new String(request)).toUpperCase();
-
- String strMethod = tempStr.substring(0, 4);
- if (strMethod.equals("GET ")) {
- beGet = true;
- } else if (strMethod.equals("POST")) {
- bePost = true;
- getContentlen_Chucked(tempStr);
- } else {
- System.out.println("不支持的HTTP包类型");
- }
- }
- private void getContentlen_Chucked(String tempStr)
-
- {
- String ss1 = "CONTENT-LENGTH:";
- String ss2 = new String("TRANSFER-ENCODING: CHUNKED");
- int clIndex = tempStr.indexOf(ss1);
- int chuckIndex = tempStr.indexOf(ss2);
- byte requst[] = tempStr.getBytes();
- if (clIndex != -1) {
-
- StringBuffer sb = new StringBuffer();
- for (int i = (clIndex + 16);; i++) {
- if (requst[i] != (byte) 13 && requst[i] != (byte) 10) {
- sb.append((char) requst[i]);
- } else
- break;
- }
- CONTENT_LENGTH = Integer.parseInt(sb.toString());
- }
- if (chuckIndex != -1)
- beChucked = true;
- }
- private int getChuckSize()
- {
- byte[] crlf = new byte[1];
- StringBuffer sb1 = new StringBuffer();
- int crlfNum = 0;
- try {
- while (input.read(crlf) != -1)
- {
- if (crlf[0] == crlf13 || crlf[0] == crlf10) {
- crlfNum++;
- } else {
- crlfNum = 0;
- }
- sb1.append((char) crlf[0]);
- request = request.append(new String(crlf, 0, 1));
- if (crlfNum == 2)
- break;
- }
- } catch (IOException e) {
- System.out.println("Read Http Package Error!");
- return 0;
- }
- return Integer.parseInt((sb1.toString()).trim(), 16);
- }
- private void writeIntoFile(String filePath, byte[] bytes, int realLength,
- int finishedFileSize) throws IOException {
- File newFile = new File(filePath);
- RandomAccessFile raf = new RandomAccessFile(newFile, "rw");
- raf.seek(finishedFileSize);
- raf.write(bytes, 0, realLength);
- raf.close();
- }
- private void createFile(String filePath) throws IOException {
- File newFile = new File(filePath);
- RandomAccessFile raf = new RandomAccessFile(newFile, "rw");
- raf.close();
- }
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
微信开发平台开发工具 JDK jdk 6.0 jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008
jdk6.0学习笔记 jdk学习笔记 java学习 web编程
Java JDK 6.0学习笔记,通俗易懂很适合初学者了解java中的要点。。。。。。
良葛格 java jdk 6.0 学习笔记
最新的Java JDK6.0课件及源代码,初学Java的朋友不要错过
Java学习笔记 入门准备 接触物件 物件向导 资源管理 物件容器 输入输出 執行緒 反射 metadata 资料库 拾遗补缺 视窗程式 参考资料
中文版的JDK,能让你更加清楚的知道如何运用API,能更加方便你的查询。
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/759635
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/759473
jdk6.0-04.zipjdk6.0-04.zipjdk6.0-04.zipjdk6.0-04.zip
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/765024
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/764901
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/765052
java6.0文档chm版,分成三部分,分别为JDK6.0+中文文档-lytim24.part1.rar、JDK6.0+中文文档-lytim24.part2.rar、JDK6.0+中文文档-lytim24.part3.rar
最全 JDK6.0 API 文档,最全 JDK6.0 API 文档。
jdk 6.0 API Document 文档,jdk 6.0 API Document 文档,jdk 6.0 API Document 文档,jdk 6.0 API Document 文档,jdk 6.0 API Document 文档,jdk 6.0 API Document 文档
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/758765
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/759558