我是英特尔爱迪生编程的新手。我设置了我的设备并通过Wi-Fi将其连接到我的本地网络。我使用以下代码创建ServerSocket
:
public class SensorServer {
ServerSocket serverSocket;
Socket client;
int port;
public SensorServer(int port) {
this.port = port;
}
private void waitForConnectionAndConnect() throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("***** Server: Wait for connection");
client = serverSocket.accept();
System.out.println("***** Server: Connected!");
while(!client.isConnected())
;
System.out.println("***** Server started successfully!");
}
// Helper method for closing the TCP connections
private void closeConnections() throws IOException {
client.close();
serverSocket.close();
}
// Helper method to send a message via one of TCP sockets
private void sendSensorData(SensorData sensorData, ObjectMapper om) throws IOException {
String json = om.writeValueAsString(sensorData);
PrintWriter printWriter;
printWriter = new PrintWriter(client.getOutputStream(), true);
printWriter.print(json + "\n");
printWriter.flush();
}
public static void main(String[] args) {
try {
SensorServer server = new SensorServer(1234);
server.waitForConnectionAndConnect();
System.out.println("Connected!!!");
ObjectMapper om = new ObjectMapper();
for(int i = 0; i < 10; i++) {
SensorData sd = server.generateSensorDataRandomly();
server.sendSensorData(sd, om);
Thread.sleep(2000);
}
server.closeConnections();
} catch (Exception e) {
e.printStackTrace();
}
}
private SensorData generateSensorDataRandomly() {
...
}
}
此服务器套接字似乎已成功创建,因为我没有任何例外。当我尝试从我的PC连接到我的爱迪生时,我得到Connection Refused: Connect
例外。这是客户端代码:
public class ClientClass {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("edison-public-ip", 1234);
OutputStream raus = socket.getOutputStream();
PrintStream ps = new PrintStream(raus, true);
ps.println("Hallo Welt!");
InputStream rein = socket.getInputStream();
System.out.println("verfügbare Bytes: " + rein.available());
BufferedReader buff = new BufferedReader(new InputStreamReader(rein));
while (buff.ready()) {
System.out.println(buff.readLine());
}
} catch (UnknownHostException e) {
System.out.println("Unknown Host...");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOProbleme...");
e.printStackTrace();
} finally {
if (socket != null)
try {
socket.close();
System.out.println("Socket geschlossen...");
} catch (IOException e) {
System.out.println("Socket nicht zu schliessen...");
e.printStackTrace();
}
}
}
}
这是我的例外:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at test.main(ClientClass.java:15)
当我尝试将两台笔记本电脑相互连接时,代码工作正常!
编辑:在我的主板上编辑了iptables,仍然没有成功
我添加了两条规则,允许所有传入和传出的流量通过iptables
:
iptables -F // Remove all rules
iptables -A INPUT -j ACCEPT
iptables -A OUTPUT -j ACCEPT
但仍然有例外!