这些类在同一台计算机上成功通信。但是,只要我们将“天线”移动到另一台计算机(仍在同一网络上),服务器仍会获得包但客户端只是等待回复。
我们已经检查过这个网站的其他线程,但我们仍然找不到我们的问题。有谁可以提供帮助?
服务器接收器类:
package Server;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.Vector;
public class RecieverProxy extends Thread {
private DatagramSocket socket;
private static int length;
private String reqS;
public static java.util.Date date = new java.util.Date();
public static Timestamp currentTimestamp = new Timestamp(date.getTime());
@Override
public void run() {
try {
System.out.println(currentTimestamp + " Server[IP=\""
+ InetAddress.getLocalHost().getHostAddress()
+ "\"]: Local server is Running...");
System.out.println(currentTimestamp
+ " SERVER: Connecting to MySQL server...");
DbUpdater.readyDatabase();
System.out.println(currentTimestamp
+ " SERVER: MySQL server is connected!");
} catch (UnknownHostException e) {
e.printStackTrace();
System.exit(1);
}
System.err.println(currentTimestamp + " SERVER: Ready... waiting...");
try {
recieveSendUDP();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* The method will wait for a request, process the request, and then return
* the reply.
*
* @throws InterruptedException
*/
public boolean recieveSendUDP() throws IOException, InterruptedException {
socket = new DatagramSocket(6789);
// modtag forespørgsler
while (true) {
try {
byte[] bytes = new byte[1000];
DatagramPacket request = new DatagramPacket(bytes, bytes.length);
socket.receive(request);
length = request.getLength();
String replyS = "ERROR";
String oldreqS = new String(request.getData());
if (oldreqS == "[]") {
System.out.println(currentTimestamp
+ " None bluetooth devices nearby");
replyS = "None bluetooth devices nearby";
} else {
String subreqS = oldreqS.substring(0, length);
String leftreqS = subreqS.replace("[", "'");
String rightreqS = leftreqS.replace("]", "'");
reqS = rightreqS.replaceAll(", ", "','");
replyS = currentTimestamp
+ " SERVER: MAC addresses recieved.";
}
// gør svar klar
int dstPort = request.getPort();
InetAddress dstAdr = request.getAddress();
System.out.println(currentTimestamp
+ " SERVER: Sending reply to IP: " + dstAdr.toString()
+ " på port " + dstPort);
DatagramSocket replySocket = new DatagramSocket();
replySocket.send(new DatagramPacket(replyS.getBytes(), replyS
.getBytes().length, new InetSocketAddress(dstAdr,
dstPort)));
replySocket.close();
DbUpdater.dbUpdate(reqS);
} catch (Exception e) {
}
}
}
}
ANTENNA SENDER CLASS:
package Antenna;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.Vector;
public class SendRemoteDevices {
public static java.util.Date date = new java.util.Date();
public static Timestamp currentTimestamp = new Timestamp(date.getTime());
public static void runMacSender() throws IOException, InterruptedException {
Vector<String> macAdds;
macAdds = RemoteDeviceDiscovery.getMacVec();
System.out.println(currentTimestamp
+ " ANTENNA: Sending macaddresses: " + macAdds);
sendMacc(macAdds);
}
public static void sendMacc(Vector<String> macAdd) {
String request = new Vector<String>(macAdd).toString().trim();
String reply = sendReceive(request);
}
// send string and receive string
// her skal serverens addresse stå
@SuppressWarnings("resource")
public static String sendReceive(String request) {
String ip = "192.168.10.106";
String serverIP = ip; // sæt anden IP ind hvis du kører
// mellem
// to maskiner
DatagramSocket ds = null;
byte[] bytes = request.getBytes(); // husk : er seperator
// sending package after encrypting.
try {
ds = new DatagramSocket();
ds.send(new DatagramPacket(bytes, bytes.length,
new InetSocketAddress(serverIP, 6789)));
System.out.println(currentTimestamp
+ " ANTENNA: Send this to serveren: \"" + request + "\"");
System.out.println(currentTimestamp
+ " ANTENNA: Waiting for answer from Server...");
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// gør mig klar til at modtage
byte[] repBytes = new byte[1000];
DatagramPacket repGram = new DatagramPacket(repBytes, repBytes.length);
try {
ds.receive(repGram);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // receive
// trim er nødvendigt
String reply = new String(repGram.getData()).trim();
System.out.println(currentTimestamp
+ " ANTENNA: Reply from Server: \"" + reply + "\"");
try { // fordi den ikke printer ovenstående før menuen
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return reply;
}
}