JAVA socket 5 대리 실현
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
public class SocketProxy {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
new SocketThread(socket).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class SocketThread extends Thread {
private Socket socketIn;
private InputStream isIn;
private OutputStream osIn;
//
private Socket socketOut;
private InputStream isOut;
private OutputStream osOut;
public SocketThread(Socket socket) {
this.socketIn = socket;
}
private byte[] buffer = new byte[4096];
// private static final byte[] VER = { 0x5, 0x0 };
// private static final byte[] CONNECT_OK = { 0x5, 0x0, 0x0, 0x1, 0, 0, 0, 0, 0, 0 };
private static final byte[] VER = { 5, 0};
private static final byte[] CONNECT_OK = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
static{
System.out.println(Arrays.toString(VER));
System.out.println(Arrays.toString(CONNECT_OK));
}
public void run() {
try {
System.out.println("
a client connect " + socketIn.getInetAddress() + ":" + socketIn.getPort());
isIn = socketIn.getInputStream();
osIn = socketIn.getOutputStream();
int len = isIn.read(buffer);
System.out.println("< " + bytesToHexString(buffer, 0, len));
osIn.write(VER);
osIn.flush();
System.out.println("> " + bytesToHexString(VER, 0, VER.length));
len = isIn.read(buffer);
System.out.println("< " + bytesToHexString(buffer, 0, len));
//
String host = findHost(buffer, 4, 7);
int port = findPort(buffer, 8, 9);
System.out.println("host=" + host + ",port=" + port);
socketOut = new Socket(host, port);
isOut = socketOut.getInputStream();
osOut = socketOut.getOutputStream();
//
for (int i = 4; i <= 9; i++) {
CONNECT_OK[i] = buffer[i];
}
osIn.write(CONNECT_OK);
osIn.flush();
System.out.println("> " + bytesToHexString(CONNECT_OK, 0, CONNECT_OK.length));
SocketThreadOutput out = new SocketThreadOutput(isIn, osOut);
out.start();
SocketThreadInput in = new SocketThreadInput(isOut, osIn);
in.start();
out.join();
in.join();
} catch (Exception e) {
System.out.println("a client leave");
} finally {
try {
if (socketIn != null) {
socketIn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("socket close");
}
public static String findHost(byte[] bArray, int begin, int end) {
StringBuffer sb = new StringBuffer();
for (int i = begin; i <= end; i++) {
sb.append(Integer.toString(0xFF & bArray[i]));
sb.append(".");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
public static int findPort(byte[] bArray, int begin, int end) {
int port = 0;
for (int i = begin; i <= end; i++) {
port <<= 16;
port += bArray[i];
}
return port;
}
// 4A 7D EB 69
// 74 125 235 105
public static final String bytesToHexString(byte[] bArray, int begin, int end) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = begin; i < end; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
sb.append(" ");
}
return sb.toString();
}
}
class SocketThreadInput extends Thread {
private InputStream isOut;
private OutputStream osIn;
public SocketThreadInput(InputStream isOut, OutputStream osIn) {
this.isOut = isOut;
this.osIn = osIn;
}
private byte[] buffer = new byte[409600];
public void run() {
try {
int len;
while ((len = isOut.read(buffer)) != -1) {
if (len > 0) {
System.out.println(new String(buffer, 0, len));
osIn.write(buffer, 0, len);
osIn.flush();
}
}
} catch (Exception e) {
System.out.println("SocketThreadInput leave");
}
}
}
class SocketThreadOutput extends Thread {
private InputStream isIn;
private OutputStream osOut;
public SocketThreadOutput(InputStream isIn, OutputStream osOut) {
this.isIn = isIn;
this.osOut = osOut;
}
private byte[] buffer = new byte[409600];
public void run() {
try {
int len;
while ((len = isIn.read(buffer)) != -1) {
if (len > 0) {
// System.out.println(new String(buffer, 0, len));
osOut.write(buffer, 0, len);
osOut.flush();
}
}
} catch (Exception e) {
System.out.println("SocketThreadOutput leave");
}
}
}
/*
,
findHost ,80
:9999,8978 ....
socket 10 byte
IP: byte[4-8] . xx.xx.xx.xx
9999, byte[9-10] 39,15
http://ethen.iteye.com/blog/783338
:
http://www.open-open.com/lib/view/open1344004339456.html
*/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.