繁体   English   中英

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

[英]Connecting a UDP Server to a Client in JAVA

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

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

Client2代码:

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);
         }

    }
  }

Client1代码:

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的消息

当一个人向多播组发送消息时,该主机和端口的所有订阅收件人都收到该消息

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

  • 端口1: Client1 <-> Server
  • 端口2: Client2 <-> Server

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM