Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  getting through proxy  (Read 2913 times)
0 Members and 1 Guest are viewing this topic.
Offline indexunknown

Junior Member





« Posted 2009-11-22 23:27:39 »

hi, i would like to ask how could i connect to my udp server with an udp client if my isp is a proxy and seems to block incoming connections.
Offline DzzD
« Reply #1 - Posted 2009-11-23 02:12:02 »

hi, i would like to ask how could i connect to my udp server with an udp client if my isp is a proxy and seems to block incoming connections.

if you try to send to your server the fact it block incoming packets is not important.

it depend on what kind of proxy it is : if it is an http only proxy forget you wont be able to send anything then tcp packets (and maybe/sometimes only to port 80)

if it is a socks proxy you have to embed your packets into proxy packet using

1  
new Socket(Proxy)


http://www.j2ee.me/j2se/1.5.0/docs/api/java/net/Proxy.html
http://www.j2ee.me/j2se/1.5.0/docs/api/java/net/Proxy.Type.html

seems that you can also configure your JRE to use your proxy with a runtime argument:

complete guide here : http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

Offline indexunknown

Junior Member





« Reply #2 - Posted 2009-11-23 12:53:26 »

Hi, i don't seem to understand how to set the connection up and the documentations don't seem to give any hints either.
My current code, don't know if it is even correct:

Server:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
public class Server {
   public static void main(String[] args) {  
      new ServerThread().start();
   }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
public class ServerThread extends Thread {

   ServerSocket server;
   Socket incoming, outgoing = null;
   InetSocketAddress dest;
   PrintWriter out = null;
   BufferedReader in = null;
   boolean error = false;

   public ServerThread() {
      try {                  
         server = new ServerSocket(5556);
         incoming = server.accept();  
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public void run() {  
      while (!error) {
         try {            
            in = new BufferedReader(new InputStreamReader(incoming
                  .getInputStream()));
            String input;
            while ((input = in.readLine()) != null) {              
               System.out.println("server got: " + input);
            }  
           
            SocketAddress local = new InetSocketAddress(InetAddress.getLocalHost(), 5556);            
            SocketAddress dest = new InetSocketAddress(incoming.getInetAddress(),incoming.getPort());
            Proxy proxy = new Proxy(Proxy.Type.SOCKS, local);            
            outgoing = new Socket(proxy);
            outgoing.connect(dest);                        
            out = new PrintWriter(outgoing.getOutputStream(), true);
            out.write("From server "+System.currentTimeMillis()%1000);
         } catch (IOException e) {
            e.printStackTrace();
         } finally {
            error = true;
         }
      }
      try {
         incoming.close();
         outgoing.close();
         out.close();
         in.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public synchronized void start() {
      super.start();
   }
}


Client:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
public class Client {
   public static void main(String[] args) {
      new ClientThread().start();
   }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;

public class ClientThread extends Thread {
   Proxy proxy;
   Socket socket;
   InetSocketAddress dest;
   PrintWriter out = null;
   BufferedReader in = null;
   boolean error = false;

   public ClientThread() {
      try {
         byte[] d = { (byte) MY, (byte) EXTERNAL, (byte) IP, (byte) ADDRESS };        
         SocketAddress local = new InetSocketAddress(InetAddress.getLocalHost(), 3000);
         SocketAddress dest = new InetSocketAddress(InetAddress.getByAddress(d),5556);        
         proxy = new Proxy(Proxy.Type.SOCKS, local);
         socket = new Socket(proxy);
         socket.connect(dest);      
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public void run() {      
      while (!error) {        
         try {
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket
                  .getInputStream()));
            String input;

            while ((input = in.readLine()) != null) {              
               System.out.println("client got: " + input);
            }
            out.write("From client "+System.currentTimeMillis()%1000);

         } catch (IOException e) {
            e.printStackTrace();
         } finally {
            error = true;
         }
      }
      try {
         socket.close();
         out.close();
         in.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public synchronized void start() {  
      super.start();
   }
}


Hope anybody can help. Currently it says in client thread:  Connection refused: connect
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2009-11-23 17:34:47 »

Excuse me, this is way offtopic but... I browsed through java.net.SocksSocketImpl.class

1  
2  
3  
4  
   byte[] data = new byte[8];
   int n = readSocksReply(in, data);
   if (n != 8)
       throw new SocketException("Reply from SOCKS server has bad length: " + n);


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
    private int readSocksReply(InputStream in, byte[] data) throws IOException {
   int len = data.length;
   int received = 0;
   for (int attempts = 0; received < len && attempts < 3; attempts++) {
       int count = in.read(data, received, len - received);
       if (count < 0)
      throw new SocketException("Malformed reply from SOCKS server");
       received += count;
   }
   return received;
    }


So if it takes more than 3 socket reads to receive 8 bytes, you're screwed! Shame on you Sun!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2009-11-23 17:45:58 »

To make up for the offtopic-ness, I wrote some code. This is all from the specs, not even compiled, so use as a reference:

To create a relay server:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
Serversocket ss = ...;
while(true)
{
   Socket s = ss.accept();
   // create new thread
}

// on another thread
InputStream in = s.getInputStream();
int mustBe4 = in.read(); // SOCKS4
int mustBe1 = in.read(); // stream

int port = 0;
port |= (in.read() & 0xFF << 8);
port |= (in.read() & 0xFF << 0);

byte[] addr = new byte[4];
for(int i=0; i<4; i++) addr[i] = (byte)in.read();

byte[] userid = new byte[128];
int len = -1;
for(int i=0; i<userid.length; i++)
{  
    int b = in.read();
    if(b == 0x00) { len = i; break;}
    userid[i] = (byte)b;
}
if(len==-1) throw new ISE("userid too long");

char[] username = new char[len];
for(int i=0; i<len; i++)
   username[i] = (char)userid[i];
System.out.println(new String(username));

out.write(0x00);
out.write(0x5a); // request granted
for(int i=0; i<2; i++) out.write(0x13); // 2 arbitrary bytes, that should be ignored
for(int i=0; i<4; i++) out.write(0x14); // 4 arbitrary bytes, that should be ignored
out.flush();

InetAddress iaddr = InetAddress.getByAddress(addr);
Socket dest = new Socket(iaddr, port);
// launch 2 threads that pump IN and OUT bytes between both endpoints.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2009-11-23 18:27:10 »

This messy code actually works!

Setup Firefox to use a proxy (SOCKS4, not SOCKS5) using 127.0.0.1@1080, and watch the STDOUT fly by. Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SOCKS4
{
   public static void main(String[] args) throws IOException
   {
      ServerSocket ss = new ServerSocket(1080, 50, InetAddress.getByName("127.0.0.1"));

      while (true)
      {
         Socket s = ss.accept();

         new Thread(new Stream(s)).start();
      }
   }

   static class Stream implements Runnable
   {
      private final Socket src;

      public Stream(Socket src)
      {
         this.src = src;
      }

      public void run()
      {
         try
         {
            InputStream in = this.src.getInputStream();
            OutputStream out = this.src.getOutputStream();

            int mustBe4 = in.read(); // SOCKS4
           int mustBe1 = in.read(); // stream

            if ((mustBe4 | mustBe1) < 0)
               throw new EOFException();
            if (mustBe4 != 4)
               throw new IllegalStateException("mustBe4=" + mustBe4);
            if (mustBe1 != 1)
               throw new IllegalStateException("mustBe1=" + mustBe1);

            int port = 0;
            port |= (in.read() << 8);
            port |= (in.read() << 0);
            if (port < 0)
               throw new EOFException();
            System.out.println("port:" + port);

            byte[] addr = new byte[4];
            for (int i = 0; i < 4; i++)
               addr[i] = (byte) in.read();
            InetAddress iaddr = InetAddress.getByAddress(addr);
            System.out.println("addr:" + iaddr.getHostAddress() + " => " + iaddr.getHostName());

            byte[] userid = new byte[128];
            int len = -1;
            for (int i = 0; i < userid.length; i++)
            {
               int b = in.read();
               if (b == -1)
               {
                  throw new EOFException();
               }

               if (b != 0x00)
               {
                  userid[i] = (byte) b;
               }
               else
               {
                  len = i;
                  break;
               }
            }
            if (len == -1)
               throw new IllegalStateException("userid too long");

            char[] username = new char[len];
            for (int i = 0; i < len; i++)
               username[i] = (char) userid[i];
            System.out.println("user:" + new String(username));

            out.write(0x00);
            out.write(0x5a); // request granted
           for (int i = 0; i < 2; i++)
               out.write(0x13); // 2 arbitrary bytes, that should be ignored
           for (int i = 0; i < 4; i++)
               out.write(0x14); // 4 arbitrary bytes, that should be ignored
           out.flush();

            Socket dst = new Socket(iaddr, port);

            src.setSoTimeout(30 * 1000);
            dst.setSoTimeout(30 * 1000);

            Thread t1 = new Thread(new Pump(dst.getInputStream(), out));
            Thread t2 = new Thread(new Pump(in, dst.getOutputStream()));
            t1.start();
            t2.start();

            t1.join();
            t2.join();

            System.out.println("closing:" + src + " <=> " + dst);
            src.close();
            dst.close();
         }
         catch (InterruptedException exc)
         {
            exc.printStackTrace();
         }
         catch (IOException exc)
         {
            exc.printStackTrace();
         }
         finally
         {
            try
            {
               this.src.close();
            }
            catch (IOException exc)
            {
               // ignore
           }
         }
      }
   }

   static class Pump implements Runnable
   {
      private final InputStream  in;
      private final OutputStream out;

      public Pump(InputStream in, OutputStream out)
      {
         this.in = in;
         this.out = out;
      }

      @Override
      public void run()
      {
         try
         {
            byte[] buf = new byte[8 * 1024];

            while (true)
            {
               int b = this.in.read(buf);
               if (b == -1)
                  break;
               this.out.write(buf, 0, b);
               this.out.flush();
            }
         }
         catch (IOException exc)
         {
            // ignore
        }
         finally
         {
            try
            {
               this.in.close();
            }
            catch (IOException exc)
            {
               // ignore
           }

            try
            {
               this.out.close();
            }
            catch (IOException exc)
            {
               // ignore
           }
         }
      }
   }
}



Using the above code, I saw how MSIE makes a connection to Microsoft on port 443, for every regular hit. persecutioncomplex

addr=65.55.157.60,port=443

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline h3ckboy
« Reply #6 - Posted 2009-11-23 18:32:34 »

This messy code actually works!
love that feeling;) !
Offline indexunknown

Junior Member





« Reply #7 - Posted 2009-11-23 18:48:23 »

so if my isp uses a proxy then there's no way i can make a low level connection from outside to my custom program?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2009-11-23 18:52:31 »

You'd have to have a publicly reachable server, that you can connect to from your home (this shouldn't be a problem), and listen on that server for connections, using some custom protocol to send all incoming bytes over that single connection back to your home system, and back ofcource.

In that situation, you'd probably use that server anyway, instead of your home PC...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline indexunknown

Junior Member





« Reply #9 - Posted 2009-11-23 18:58:33 »

okay thanks, those private servers where u can run anything cost a lot so first u have to complete a huge game and then u can try if it even works correctly over open internet.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #10 - Posted 2009-11-23 19:02:38 »

This messy code actually works!

Setup Firefox to use a proxy (SOCKS4, not SOCKS5) using 127.0.0.1@1080, and watch the STDOUT fly by. Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SOCKS4
{
   public static void main(String[] args) throws IOException
   {
      ServerSocket ss = new ServerSocket(1080, 50, InetAddress.getByName("127.0.0.1"));

      while (true)
      {
         Socket s = ss.accept();

         new Thread(new Stream(s)).start();
      }
   }

   static class Stream implements Runnable
   {
      private final Socket src;

      public Stream(Socket src)
      {
         this.src = src;
      }

      public void run()
      {
         try
         {
            InputStream in = this.src.getInputStream();
            OutputStream out = this.src.getOutputStream();

            int mustBe4 = in.read(); // SOCKS4
           int mustBe1 = in.read(); // stream

            if ((mustBe4 | mustBe1) < 0)
               throw new EOFException();
            if (mustBe4 != 4)
               throw new IllegalStateException("mustBe4=" + mustBe4);
            if (mustBe1 != 1)
               throw new IllegalStateException("mustBe1=" + mustBe1);

            int port = 0;
            port |= (in.read() << 8);
            port |= (in.read() << 0);
            if (port < 0)
               throw new EOFException();
            System.out.println("port:" + port);

            byte[] addr = new byte[4];
            for (int i = 0; i < 4; i++)
               addr[i] = (byte) in.read();
            InetAddress iaddr = InetAddress.getByAddress(addr);
            System.out.println("addr:" + iaddr.getHostAddress() + " => " + iaddr.getHostName());

            byte[] userid = new byte[128];
            int len = -1;
            for (int i = 0; i < userid.length; i++)
            {
               int b = in.read();
               if (b == -1)
               {
                  throw new EOFException();
               }

               if (b != 0x00)
               {
                  userid[i] = (byte) b;
               }
               else
               {
                  len = i;
                  break;
               }
            }
            if (len == -1)
               throw new IllegalStateException("userid too long");

            char[] username = new char[len];
            for (int i = 0; i < len; i++)
               username[i] = (char) userid[i];
            System.out.println("user:" + new String(username));

            out.write(0x00);
            out.write(0x5a); // request granted
           for (int i = 0; i < 2; i++)
               out.write(0x13); // 2 arbitrary bytes, that should be ignored
           for (int i = 0; i < 4; i++)
               out.write(0x14); // 4 arbitrary bytes, that should be ignored
           out.flush();

            Socket dst = new Socket(iaddr, port);

            src.setSoTimeout(30 * 1000);
            dst.setSoTimeout(30 * 1000);

            Thread t1 = new Thread(new Pump(dst.getInputStream(), out));
            Thread t2 = new Thread(new Pump(in, dst.getOutputStream()));
            t1.start();
            t2.start();

            t1.join();
            t2.join();

            System.out.println("closing:" + src + " <=> " + dst);
            src.close();
            dst.close();
         }
         catch (InterruptedException exc)
         {
            exc.printStackTrace();
         }
         catch (IOException exc)
         {
            exc.printStackTrace();
         }
         finally
         {
            try
            {
               this.src.close();
            }
            catch (IOException exc)
            {
               // ignore
           }
         }
      }
   }

   static class Pump implements Runnable
   {
      private final InputStream  in;
      private final OutputStream out;

      public Pump(InputStream in, OutputStream out)
      {
         this.in = in;
         this.out = out;
      }

      @Override
      public void run()
      {
         try
         {
            byte[] buf = new byte[8 * 1024];

            while (true)
            {
               int b = this.in.read(buf);
               if (b == -1)
                  break;
               this.out.write(buf, 0, b);
               this.out.flush();
            }
         }
         catch (IOException exc)
         {
            // ignore
        }
         finally
         {
            try
            {
               this.in.close();
            }
            catch (IOException exc)
            {
               // ignore
           }

            try
            {
               this.out.close();
            }
            catch (IOException exc)
            {
               // ignore
           }
         }
      }
   }
}



Using the above code, I saw how MSIE makes a connection to Microsoft on port 443, for every regular hit. persecutioncomplex

addr=65.55.157.60,port=443

very nice code source snipet , what about cleaning it (or no Smiley ) a little and put it into shared section ?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2009-11-23 19:21:12 »

very nice code source snipet , what about cleaning it (or no Smiley ) a little and put it into shared section ?

Nobody needs this. Unless you are building spyware maybe, or you're debugging your... pop3/smtp/http/whatever-server. Now... who is doing that??

On a technical note: it desperately needs a thread pool, to get rid of the initial delay on each new connection. That, and with a few minor changes, it can run on my Priobit (NIO) API -- much better than spawning 3 (!) threads per TCP connection.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #12 - Posted 2009-11-23 23:04:33 »

Nobody needs this. Unless you are building spyware maybe, or you're debugging your... pop3/smtp/http/whatever-server. Now... who is doing that??
no this is just nice and can be usefull, yes for debugging mostly but also for doing & understanding a socks4 proxy Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2009-11-23 23:15:16 »

SOCKS4 is ... easy! It's laughable. There is nothing to learn!

Having said that, I'm going to write a nice NIO implementation Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2009-11-24 16:38:22 »

Improved Priobit a bit, to make to possible to use:
  1. fixed-length packets (SOCKS4 header)
  2. null-terminated-strings (SOCKS4 userid)
  3. direct streaming (SOCKS4 transfering received bytes)
in the same TCP-stream.

And the very convenient client.disconnectOnSent(); to ensure the last outbound bytes are sent, before disconnecting.

http://code.google.com/p/priobit/source/browse/trunk/%20priobit/test/jawnae/priobit/SOCKS4.java

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
      // fixed length packet
     ByteFilter header = new ByteFilterLength(8)
      {
         @Override
         public void onReady(ByteBuffer buffer)
         {
            ...
         }
      };

      // null-terminated-string
     ByteFilter userid = new ByteFilterEndsWith(new byte[] { (byte) 0x00 }, 256)
      {
         @Override
         public void onReady(ByteBuffer buffer)
         {
            ...

            // start streaming (direct handling) of bytes
           client.applyReadStrategy(ReadStrategy.PASS_COPIED_BUFFER);
         }
      };

      src.applyReadStrategy(ReadStrategy.USE_BYTE_FILTER);
      src.enqueueByteFilter(header); // onReady will be invoked when done, it will continue with the userid
     src.enqueueByteFilter(userid); // onReady will be invoked when done, it will continue with plain streaming of bytes

// result of PASS_COPIED_BUFFER

   @Override
   public void receivedData(NioClient client, ByteBuffer data)
   {
      NioClient other = (NioClient) client.attachment();

      // just send to the other client what we received
     other.enqueue(data);
   }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (32 views)
2014-04-15 18:08:23

BurntPizza (29 views)
2014-04-15 03:46:01

UprightPath (44 views)
2014-04-14 17:39:50

UprightPath (27 views)
2014-04-14 17:35:47

Porlus (44 views)
2014-04-14 15:48:38

tom_mai78101 (65 views)
2014-04-10 04:04:31

BurntPizza (125 views)
2014-04-08 23:06:04

tom_mai78101 (225 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!