我有一个简单的单播客户端/服务器程序,该程序与客户端一起从服务器询问一些内容,然后服务器进行应答。
现在,我想做一个变体,但要向其中添加多播。在此阶段,客户可以加入群组并收到如下确认信息:
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行引起的与之相关的绑定(bind)错误。