/*
* Copyright 2015-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package sockslib.example;
import sockslib.client.SocksProxy;
import sockslib.client.SocksProxyFactory;
import sockslib.client.SocksSocket;
import sockslib.utils.ResourceUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
public class TestSocksFactory {
public static void main(String[] args) {
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
StringBuffer response = null;
int length = 0;
byte[] buffer = new byte[2048];
try {
SocksProxy proxy = SocksProxyFactory.parse(
"localhost,1080,,,classpath:client-ssl-config/clientTrust.jks,"
+ "123456,classpath:client-ssl-config/client.jks,123456");
socket = new SocksSocket(proxy);
socket.connect(new InetSocketAddress("whois.internic.net", 43));
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.print("domain google.com\r\n");
printWriter.flush();
response = new StringBuffer();
while ((length = inputStream.read(buffer)) > 0) {
response.append(new String(buffer, 0, length));
}
System.out.println(response.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
ResourceUtil.close(inputStream, outputStream, socket);
}
}
}