gpt4 book ai didi

java - 在 JAVA 中将 UDP 服务器连接到客户端

转载 作者:行者123 更新时间:2023-11-30 02:39:56 28 4
gpt4 key购买 nike

我有一个服务器和一个客户端(比如 client1),它们都在一个文件上,工作得很好。我遇到这样的情况:我有另一个客户端(比如 client2)向服务器发送信息。该服务器必须将从 client2 获取的信息发送到 client1。但是当我在服务器和客户端1上使用相同的端口号和相同的IP地址时,当我尝试从客户端2发送信息时,客户端1也同时接受。

如何从 client2 发送信息,以便第一个服务器首先接受它,然后将该信息发送到 client1?

客户端2代码:

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

class Client1
{

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

try {
InetAddress ip=InetAddress.getByName("228.5.6.7");
int port=4270;

MulticastSocket sock=new MulticastSocket();
String msg="Hello All";

DatagramPacket packet;
while(true)
{
packet =new DatagramPacket(msg.getBytes(),msg.length(),ip,port);
sock.send(packet);
}

}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

我的服务器和客户端代码:

class Server implements Runnable
{

public void run()
{
try
{
//get the multicast ip
InetAddress ip = InetAddress.getByName("228.5.6.7");

int port=4270;
byte[] buffer=new byte[100];
byte[] data = null;

MulticastSocket sock=new MulticastSocket(port);

//join the multicast group
sock.joinGroup(ip);
while(true)
{
//create a datagram packet in which u will receive the msg

Thread.sleep(2000);
DatagramPacket pack=new DatagramPacket(buffer,buffer.length);
sock.receive(pack);
String msg = new String(buffer);
System.out.println("Sever Has Received :" +msg);

//SENDING THE MESSAGE TO CLIENT
System.out.println("Sending the Message!!!");


data = msg.getBytes();
DatagramPacket pack1 = new DatagramPacket(data,msg.length(),ip,port);
Thread.sleep(2000);
sock.send(pack1);
}

}
catch (Exception ex) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, ex);
}

}
}

客户端1代码:

class Client implements Runnable
{

public void run()
{

try {
InetAddress address1 = InetAddress.getByName("228.5.6.7");
int port=4271;
MulticastSocket socket1 = new MulticastSocket(port);
//join a Multicast group and send the group salutations
socket1.joinGroup(address1);
byte[] data1 = new byte[256];
DatagramPacket packet1 = new DatagramPacket(data1,data1.length);
while(true)
{
// receive the packets
socket1.receive(packet1);
String str = new String(packet1.getData(),0,packet1.getLength());
Thread.sleep(2000);
System.out.println("Client Received : "+str);
}
}
catch (Exception ex) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, ex);
}
}
}

主程序

class ClientAndServer
{

public static void main(String[] args)
{

Server s = new Server();

//First start Server and then Start client
Thread t1 = new Thread(s);
t1.start();
Client c = new Client();
Thread t2 = new Thread(c);

t2.start();
}
}

最佳答案

由于您使用 MulticastSocket 链接 Client1 <-> Server <-> Client2。如果服务器发送一条消息,每个客户端都会收到它,这是来自 MulticastSocket doc

When one sends a message to a multicast group, all subscribing recipients to that host and port receive the message

如果您不希望这样,您可能需要使用两个不同的套接字,使用两个端口

  • 端口1:客户端1 <-> 服务器
  • 端口2:客户端2 <-> 服务器

然后您可以将消息仅重定向到一个或另一个端口,但将有两个不同的消息 channel

关于java - 在 JAVA 中将 UDP 服务器连接到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42086343/

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