![](/img/trans.png)
[英]Why do I get a (java) NullPointerException when writing to a socket's output, defined on another thread?
[英]In Java, with Multicast Socket, I'm trying to get output from another computer, but fail. Wat do?
我試圖從另一台計算機上獲取一些數據(在這種特殊情況下,以及在我要發送的代碼中,日期對象(帶有.toString()
))。 設置就像下面的步驟一樣;
我期望從服務器到客戶端獲取5個日期對象,但是當我在另一台計算機上打開客戶端時,我無法接收到此類數據。 我將分享我到目前為止所寫的內容,但是如果您不滿意,可以在此處查看示例。
我的代碼如下。
ServerThread:
public class MulticastServerThread extends QuoteServerThread {
private long FIVE_SECONDS = 5000;
public MulticastServerThread() throws IOException {
super("MulticastServerThread");
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// construct quote
String dString = null;
if (in == null)
dString = new Date().toString();
else
dString = getNextQuote();
buf = dString.getBytes();
InetAddress group = InetAddress.getByName("255.255.255.255");
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, 4446);
socket.send(packet);
try {
sleep((long)(Math.random() * FIVE_SECONDS));
} catch (InterruptedException e) { }
} catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
}
和客戶端類:
public class MulticastClient {
public static void main(String[] args) throws IOException {
MulticastSocket socket = new MulticastSocket(4446);
InetAddress address = InetAddress.getByName("224.0.0.252");
socket.joinGroup(address);
DatagramPacket packet;
// get a few quotes
for (int i = 0; i < 5; i++) {
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
}
socket.leaveGroup(address);
socket.close();
}
}
您需要讓服務器將信息投射到客戶端已加入的同一組中。 將您的服務器組設置為: InetAddress group = InetAddress.getByName("224.0.0.252");
public class MulticastServerThread extends QuoteServerThread {
private long FIVE_SECONDS = 5000;
public MulticastServerThread() throws IOException {
super("MulticastServerThread");
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// construct quote
String dString = null;
if (in == null)
dString = new Date().toString();
else
dString = getNextQuote();
buf = dString.getBytes();
InetAddress group = InetAddress.getByName("255.255.255.255"); //Keep this as the same multicast ip as in your client
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, 4446);
socket.send(packet);
try {
sleep((long)(Math.random() * FIVE_SECONDS));
} catch (InterruptedException e) { }
} catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
}
試試這個代碼。
public class Server3 {
public static void main(String[] args) throws IOException {
MulticastSocket multiSocket = new MulticastSocket(3575);
InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
multiSocket.setBroadcast(true);
multiSocket.joinGroup(groupMulticast);
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Sending...");
String msg = "Hai";
byte[] bufSend = msg.getBytes();
DatagramPacket packetSend = new DatagramPacket(bufSend, bufSend.length, groupMulticast, 3575);
try {
multiSocket.send(packetSend);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Client3 {
public static void main(String[] args) throws IOException {
MulticastSocket multiSocket = new MulticastSocket(3575);
InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
multiSocket.setBroadcast(true);
multiSocket.joinGroup(groupMulticast);
byte[] bufReceive = new byte[1024];
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Receiving...");
DatagramPacket packetReceive = new DatagramPacket(bufReceive, bufReceive.length);
try {
multiSocket.receive(packetReceive);
System.out.println("msg...");
System.out.println(new String(bufReceive));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.