所以我試圖在一個項目的進程之間實現套接字通信。但我似乎無法通過回送設備連接到任何端口。我在這裏錯過了什麼嗎?我已經讓它在500個端口上運行,它總是拒絕連接。帶有套接字的Java IPC - 使用環回設備
static final String HOST = "127.0.0.1";
public static void main(String[] args) throws IOException {
int port = 1000;
while (true) {
try {
socket = new Socket(HOST, PORT); // initialing the socket
writer = new OutputStreamWriter(socket.getOutputStream());
reader = new InputStreamReader(socket.getInputStream());
break;
} catch (ConnectException ex) {
System.out.println("failure on port: " + port);
++port; // increment port to try next
}
}
...
};
這是整個程序,如果有人想看到聲明和whatnot。
package socket_ipc;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;
public class Socket_IPC {
static final int NUMBER_OF_MESSAGES = 100; // number of messages to pass
static int[] PRODUCED_MSSG = new int[NUMBER_OF_MESSAGES]; // for comparing
static int[] CONSUMED_MSSG = new int[NUMBER_OF_MESSAGES]; // for comparing
static final String HOST = "127.0.0.1"; // IP address of loopback device
static final int PORT = 1000; // arbitrary port number (local)
static OutputStreamWriter writer; // write to socket
static InputStreamReader reader; // read from socket
static Socket socket; // the socket
private static class s_Producer extends Thread {
@Override
public void run() {
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
try {
PRODUCED_MSSG[i] = (int)(Math.random() * 256); // get data
writer.write(PRODUCED_MSSG[i]); // write data to the socket
} catch (IOException ex) {
System.err.println(ex.toString());
}
}
}
}
private static class s_Consumer extends Thread {
@Override
public void run() {
for(int i = 0; i < NUMBER_OF_MESSAGES; i++) {
try {
int data = reader.read(); // obtain data from the socket
CONSUMED_MSSG[i] = data; // put retrieved data in array
} catch (IOException ex) {
System.err.println(ex.toString());
}
}
}
}
public static void main(String[] args) throws IOException {
int port = PORT; // beginning at 1000
while (true) {
try {
socket = new Socket(HOST, port); // initialing the socket
writer = new OutputStreamWriter(socket.getOutputStream());
reader = new InputStreamReader(socket.getInputStream());
break;
} catch (ConnectException ex) {
System.out.println("failure on port: " + port);
++port; // increment port to try next
}
}
/* insanciating and starting the producer process */
s_Producer p = new s_Producer();
p.start();
/* insanciating and starting the consumer process */
s_Consumer c = new s_Consumer();
c.start();
try { /* joining threads to wait for completion */
p.join();
c.join();
} catch (InterruptedException ex) {
System.err.println(ex.toString());
}
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
System.out.println(
"[" + i + "]: " + PRODUCED_MSSG[i] + " == " + CONSUMED_MSSG[i]);
if (PRODUCED_MSSG[i] != CONSUMED_MSSG[i]) {
System.out.println("PROCESS SYNCHRONIZATION ERROR!");
System.exit(0);
}
}
System.out.println("PROCESS SYNCHRONIZATION SUCCESS!");
}
};
有沒有什麼東西在您試圖連接的端口上偵聽? – fvu
我建議你嘗試連接到有服務已經運行的端口。如果你打印出實際的錯誤,應該說「連接被拒絕」,告訴你沒有任何事情正在監聽那個端口。 –
所以我不會讓套接字在端口上創建時聽到嗎? – whatfield