gpt4 book ai didi

Java UDP 组播

转载 作者:行者123 更新时间:2023-11-29 08:52:25 24 4
gpt4 key购买 nike

我有一个简单的单播客户端/服务器程序,客户端向服务器请求一些东西,然后服务器回答。现在我想做一个变体,但向其中添加多播。在此阶段,客户可以加入群组并收到如下确认信息:

hostAddr is /192.168.56.1 and PORT NUMBER IS: 54767

在服务器端,我期待来自客户端的包,但我从未得到它。

服务器代码:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.util.HashMap;

public class MulticastServer
{
public static MulticastSocket socket;

final static InetSocketAddress d = new InetSocketAddress("239.1.1.3", 4444);

public static void runNotifier(String m_addr, int m_port)
{
//final InetSocketAddress d = new InetSocketAddress(m_addr, m_port);
(new Thread()
{
public void run()
{
while(true)
{
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
byte[] hello = ("hello").getBytes();
DatagramPacket h = new DatagramPacket(hello, hello.length, d.getAddress(), d.getPort());
//System.out.println("ADDR: " + d.getAddress() + " PORT: " + d.getPort());
try {
//System.out.println("SENDING");
socket.send(h);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
).start();

}

public static void initSocket(int port) throws IOException
{
socket = new MulticastSocket(); //port
socket.setTimeToLive(1);
}

public static void main(String args[]) throws Exception
{
initSocket(4444);
runNotifier("239.1.1.3", 4444);
HashMap<String, String> cache = new HashMap<String, String>();
DatagramSocket serverSocket = new DatagramSocket(4444);

byte[] receiveData = new byte[1024]; //buffers
byte[] sendData = new byte[1024];
System.out.println("BEFORE SERVER WHILE");
while(true)
{
System.out.println("INSIDE SERVER WHILE");
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
System.out.println("RECEIVED FROM CLIENT");
//recebe string do cliente
String sentence = new String(receivePacket.getData(),0, receivePacket.getLength());
/*
String[] parts = sentence.split(" ");

if (sentence.contains(" ")) {
cache.put(parts[1], parts[2]);
} else {
System.out.println("SERVER can't do anything with this.");
}

//String name = cache.get(parts[1]);
*/
System.out.println("CLIENT SAID: " + sentence);
//System.out.println("THE NAME IS: " + name);

InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
receiveData = null;
}
}
}

客户端代码

import java.io.*;
import java.net.*;
import java.util.*;

public class MulticastClient {

public static MulticastSocket socket = null;

public static void main(String[] args) throws IOException
{

socket = new MulticastSocket(4444); //multicast port
InetAddress address = InetAddress.getByName("239.1.1.3"); //multicast address
socket.joinGroup(address);
System.out.println("JOINED GROUP");
//recebe o hello
byte[] a = new byte[1024];
DatagramPacket p = new DatagramPacket(a, a.length);
socket.receive(p);
System.out.println("RECEIVED PACKAGE");
InetAddress hostAddr = p.getAddress();
int portNumber = p.getPort();
System.out.println("hostAddr is " + hostAddr + " and PORT NUMBER IS: " + portNumber);
//

HashMap<String, String> cache = new HashMap<String, String>();
//DatagramSocket serverSocket = new DatagramSocket(p.getPort());

byte[] receiveData = new byte[1024]; //buffers
byte[] sendData = new byte[1024];

/* DatagramPacket packet;
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);*/

//escrita consola
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();

DatagramSocket clientSocket = new DatagramSocket();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, hostAddr, portNumber);
clientSocket.send(sendPacket);
System.out.println("SENT TO SERVER");
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);

String modifiedSentence = new String(receivePacket.getData());
System.out.println("SERVER: " + modifiedSentence);
clientSocket.close();

socket.leaveGroup(address);
socket.close();
}

}

到目前为止我最好的猜测是在服务器的第 53 行

DatagramSocket serverSocket = new DatagramSocket(4444); 

我使用的端口号不正确。如果客户端收到端口 54767,则它将发送到该端口。但即使假设这是问题所在,我也不知道如何解决它,因为每次我运行它时,客户端都会获得不同的端口。

我遇到的另一个问题是,由于错误,我似乎无法同时运行两个客户端

Exception in thread "main" java.net.SocketException: Unrecognized Windows Sockets error: 0: Cannot bind

客户端第12行

socket = new MulticastSocket(4444); //multicast port

提前感谢您的宝贵时间。

最佳答案

1-您可以在服务器端创建一个MulticastSocket来接收消息。
2- 我认为你不需要 DatagramSocket 来与组中的其他人通信,因为 MulticastSocket 适用于客户端和服务器。第12行引起的Binding Error与之相关。

关于Java UDP 组播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com