开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约

蒋固金 / JSocketJavaApache-2.0

Watch 1 Star 0 Fork 0
Sign up for free
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
套接字简易抽象实现 spread retract

Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

第一部分 简介

JSocket是一个简易的Socket封装。

第二部分 开始使用

使用JSocket可以直接下载源代码编译或者下载已经编译的jar文件,如果您是使用maven来构建项目,也可以直接在pom.xml中添加JSocket的坐标:

Maven central

<!-- http://mvnrepository.com/artifact/com.jianggujin/JSocket -->
<dependency>
    <groupId>com.jianggujin</groupId>
    <artifactId>JSocket</artifactId>
    <version>最新版本</version>
</dependency>

最新的版本可以从Maven仓库或者码云获取。

2.1 一个例子

package com.jianggujin.socket.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.SocketException;

import org.junit.Test;

import com.jianggujin.socket.JSocketClient;

public class SocketClientTest {
   @Test
   public void test() throws SocketException, IOException {
      JSocketClient client = new JSocketClient();
      client.connect("www.baidu.com", 80);
      InputStream input = client.getInputStream();
      OutputStream out = client.getOutputStream();
      out.write("GET http://www.baidu.com/ HTTP/1.1\r\n".getBytes("UTF-8"));
      out.write("Accept-Encoding: gzip,deflate\r\n".getBytes("UTF-8"));
      out.write("Host: www.baidu.com\r\n".getBytes("UTF-8"));
      out.write("Connection: Keep-Alive\r\n".getBytes("UTF-8"));
      out.write("User-Agent: SocketClient\r\n\r\n".getBytes("UTF-8"));
      BufferedReader reader = new BufferedReader(new InputStreamReader(input));
      String line = null;
      // 只读取响应头信息
      while ((line = reader.readLine()) != null && line.length() > 0) {
         System.out.println(line);
      }
      reader.close();
      client.close();
   }
}

Comments ( 0 )

You need to Sign in for post a comment

Help Search