views:

33

answers:

3

I have a socket connecting to a proxy then to a remote server i specify. However i want multiple connections to the remove server from the proxy. So i want to create 4 sockets to the same proxy and from the 4 sockets connect to the remote server.

When i do this it doesn't work, it only connects to the proxy once.

Here is psuedo code:

static Socket[] liveCon = new Socket[300];
// This is the class that assigns a proxy and connects
// it is a temporary thread that connects and ends.
sockClass sockets = new sockClass; 

class main {
    for (int i = 0; i < livecon.length; i++) {
        sockets[i].start(); // Thread ends after it is connected
    }
}

class sockClass{
   main.liveCon[index] = new Socket(proxy);
   main.liveCon[index].connect(ep);
   main.liveCon[index].setPerformancePreferences(1, 2, 0);
   if (main.liveCon[index].isConnected() == true) {
       myOutput = new PrintStream(main.liveCon[index].getOutputStream());
       main.liveCon[index].setKeepAlive(true);
   }
}
A: 

that depends on how the code is program on your proxy,pls provide the proxy info.

Vincent
A: 

Sounds like you're not using threads, but instead you try to connect to the proxy sequentially.

Since you don't post a sample of the problem is hard to trouble shot.

Try running your program 4 times instead to see if the problem is in your code or in the proxy it self.

I hope this helps.

OscarRyz
yea im not using threading, im using an array of sockets. Is not threading the connections the problem? If so is there any way i can fix it without resorting to threading.
Kalman123
Using different programs. The problem is. You have an array of sockets, right, the second socket will connect only after the first finish, and the third only after the second, etc. So, you are connecting them sequentially when what you want is to have then connected in parallel. Is not that hard ( when you know how to do it :P as with anything ) But, this ( the threading ) is the main problem you're having.
OscarRyz
why do u need to connect the 4 sockets at the same time?
Kalman123
Isn't that what you want?
OscarRyz
Oh, I see, forget about what I said ( although it would be a good alternative to use thread ) Most likely you're dropping the connection when the first socket connect and successive connections fail. It is almost impossible to answer what your problem is without looking at the source code. I hope you're aware of this.
OscarRyz
my code is pages long lol, but essentially what im doing is connecting to hundreds of proxies and after i loop through each one i loop through them again and create another instance of a connection. I do the loop 4 times and maintain each socket to stay up. However i only get 1 per socket connected to the proxy which is weird. So after i create one socket for a proxy about 30 seconds later i create another socket using the same proxy. I'll try to post some code tomarow and show you what im doing, but i dont feel like going through all the code right now.
Kalman123
A: 

If you're not using threading you cannot do this correctly unless you go to non-blocking I/O.

EJP
I do not understand why this is a problem though. All my sockets try to connect to the single proxy fine but only 1 connects successfully. Can you explain why this happens?
Kalman123
Not without seeing your code. But it's pointless as is. Fix to use a thread per socket and there is some point to the discussion.
EJP
I added psuedo code to main post
Kalman123
So what do you mean by 'it doesn't work?' What happens to the other three connection attempts? And your socket-handling threads shouldn't finish when they are connected, they should keep going to handle all the I/O for that socket.
EJP
i have one thread going around maintaining io for all sockets.
Kalman123