• 쓴 때 : 20010806
  • 설명 : 특정 포트로 들어오는 소켓연결을 지정된 호스트의 지정된 포트로 포워딩 시키는 코드

예: 10.80.3.54라는 서버에 다음과 같이 실행시킨다.

java StreamBridge 3333 wiki.gimslab.com 80

그리고 이제 다른 클라이언트에서 다음과 같이 접속한다.


그러면 wiki.gimslab.com의 80번포트와 연결이된다.

/**
 * 특정 포트로 들어오는 소켓연결을 지정된 호스트의 지정된 포트로 포워딩 시킴
 * 작성 날짜: (2001-07-31 오후 7:48:57)
 */

import java.io.*;
import java.net.*;
 
public class StreamBridge 
{

private ServerSocket ssoc = null;
private Socket client = null;
private Socket host = null;

private WriterThread clientListener = null;
private WriterThread serverHostListener = null;


class WriterThread extends Thread
{

private boolean shouldRun = false;
private InputStream read = null;
private OutputStream write = null;

public WriterThread(InputStream is, OutputStream os)
{
	read = is;
	write = os;
}
	
public void startMe()
{
	shouldRun = true;
	start();
}

public void stopMe()
{
	shouldRun = false;
}

public void run()
{
	int i = 0;

	try{
		while(shouldRun && (i = read.read())!=-1){
			write.write(i);
		}
	}
	catch(Exception e){
		System.out.println("Ex200107312012(WriterThread::run()), "+e);
	}
	
}

	
}



/**
 * StreamBridge 생성자 주석.
 */
private StreamBridge() 
{
	
}
/**
 * StreamBridge 생성자 주석.
 */
public StreamBridge(int port1, String hostIp, int port2) 
{
	super();

	try{
		
		// 서버소켓 생성
		ssoc = new ServerSocket(port1);

		// 클라이언트 소켓 얻기
		client = ssoc.accept();

		// 호스트 연결
		host = new Socket(hostIp, port2);

		// client --> host
		clientListener = new WriterThread(client.getInputStream(), host.getOutputStream());
		clientListener.startMe();
		
		// host --> client
		serverHostListener = new WriterThread(host.getInputStream(), client.getOutputStream());
		serverHostListener.startMe();
		

		
	}
	catch(Exception e){
		System.out.println("Ex200107311957(StreamBride::StreamBridge(int, int)), "+e);
		System.exit(1);
	}
	finally{

		// 소켓 닫기
		if(ssoc!=null){
			try{
				ssoc.close();
			}
			catch(Exception e){
				System.out.println("Ex200107311958(StreamBride::StreamBridge(int, int)), "+e);
			}
		}
		
	}
	
}
/**
 * 응용프로그램을 시작합니다.
 * @param args 명령행 인수 배열
 */
public static void main(java.lang.String[] args) 
{

	if(args.length<2){
		System.out.println("usage: StreamBridge port1, hostIp, port2");
	}
	else{
		try{
			int p1 = Integer.parseInt(args[0]);
			int p2 = Integer.parseInt(args[2]);
			new StreamBridge(p1, args[1], p2);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	

}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-04-20 18:54:25
Processing time 0.0056 sec