source: trunk/PortForwarder/src/portforwarder/Main.java @ 8

Last change on this file since 8 was 8, checked in by fmguler, 15 years ago

Port yönlendirme uygulaması - ilk import

File size: 2.9 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package portforwarder;
6
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.OutputStream;
10import java.net.ServerSocket;
11import java.net.Socket;
12import java.util.logging.Level;
13import java.util.logging.Logger;
14
15/**
16 *
17 * @author fmguler
18 */
19public class Main {
20    /**
21     * @param args the command line arguments
22     */
23    public static void main(String[] args) {
24        // TODO code application logic here
25        //arg0 -L or -R
26        //arg1 port
27        //arg2 host
28        //arg3 hostport
29
30        if (args.length == 0) {
31            System.out.println("Requires arguments");
32            return;
33        }
34
35        if (args[0].equals("-L")) {
36
37            try {
38                int port = Integer.parseInt(args[1]);
39                ServerSocket serverSocket = new ServerSocket(port);
40                while (true) {
41                    Socket ourclient = serverSocket.accept();
42                    //yeni baðlantý oldu, biz de baðlanalým
43                    String host = args[2];
44                    int hostPort = Integer.parseInt(args[3]);
45                    Socket toHost = new Socket(host, hostPort);
46
47                    pipe(ourclient.getInputStream(), toHost.getOutputStream(), toHost.getInputStream(), ourclient.getOutputStream());
48                }
49
50            } catch (IOException ex) {
51                System.out.println("Error forwarding");
52                ex.printStackTrace();
53            }
54
55        } else if (args[0].equals("-R")) {
56        } else {
57            System.out.println("Invalid switch");
58        }
59    }
60
61    public static void pipe(final InputStream is1, final OutputStream os1, final InputStream is2, final OutputStream os2) {
62
63        new Thread(new Runnable() {
64            public void run() {
65                try {
66                    byte[] buf = new byte[1024];
67                    int c = 0;
68                    while ((c = is1.read(buf)) != -1) {
69                        os1.write(buf, 0, c);
70                    }
71                    System.out.println("one pipe finished-1");
72                } catch (IOException ex) {
73                    System.out.println("Error piping is1 to os2");
74                    ex.printStackTrace();
75                }
76            }
77        }).start();
78
79        new Thread(new Runnable() {
80            public void run() {
81                try {
82                    byte[] buf = new byte[1024];
83                    int c = 0;
84                    while ((c = is2.read(buf)) != -1) {
85                        os2.write(buf, 0, c);
86                    }
87                    System.out.println("one pipe finished-2");
88                } catch (IOException ex) {
89                    System.out.println("Error piping is1 to os2");
90                    ex.printStackTrace();
91                }
92            }
93        }).start();
94    }
95}
Note: See TracBrowser for help on using the repository browser.