我有一個套接字連接到代理,然後到我指定的遠程服務器。不過,我想從代理多個連接到刪除服務器。所以我想創建4個套接字到同一個代理,並從4個套接字連接到遠程服務器。java中的套接字/代理問題
當我這樣做不起作用,它只連接到代理一次。
這裏是僞代碼:
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);
}
}
是啊即時通訊不使用線程,即時通訊使用插座數組。不是線程連接的問題?如果是的話,有什麼辦法可以解決它,而不訴諸於線程。 – Kalman123 2010-09-23 04:48:47
使用不同的程序。問題是。你有一個套接字數組,右邊,第二個套接字只在第一個完成後連接,第三個只在第二個接下來之後連接,等等。所以,當你想要的是並行連接的時候,你是按順序連接它們的。是不是很難(當你知道如何做到這一點:P與任何東西)但是,這(線程)是你遇到的主要問題。 – OscarRyz 2010-09-23 05:20:57
爲什麼你需要同時連接4個插座? – Kalman123 2010-09-23 05:33:27