0

したがって、メッセージを正しいクライアントにルーティングする必要がある3つのクライアントと1つのサーバーがあります。クライアントは、メッセージを取得する必要がある他のクライアントの名前とともにメッセージを送信します。サーバーは受信クライアントの名前を比較し、パケット内の正しいIPを置き換えて、メッセージを正しいクライアントに送信する必要があります。問題は、サーバーがIPを置き換えないため、メッセージが配信されないことです。問題を解決するのを手伝ってください。これが私のサーバーコードです:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Server extends JFrame 
{
   private JTextArea displayArea; // displays packets received
   private DatagramSocket socket; // socket to connect to client
   private String[] message; 
   private String pcName, pc1, pc2, pc3;
   private InetAddress clientIPAddress, nextClient;

   // set up GUI and DatagramSocket
   public Server()
   {
   super( "Server" );

  displayArea = new JTextArea(); // create displayArea
  add( new JScrollPane( displayArea ), BorderLayout.CENTER );
  setSize( 400, 300 ); // set size of window
  setVisible( true ); // show window

  try // create DatagramSocket for sending and receiving packets
  {
     socket = new DatagramSocket( 5000 );
  } // end try
  catch ( SocketException socketException ) 
  {
     socketException.printStackTrace();
     System.exit( 1 );
  } // end catch
  } // end Server constructor

 // wait for packets to arrive, display data and echo packet to client
 public void waitForPackets()
 {
  while ( true ) 
  {
     try // receive packet, display contents, return copy to client
     {
        byte[] data = new byte[ 100 ]; // set up packet
        DatagramPacket receivePacket = 
           new DatagramPacket( data, data.length );

        socket.receive( receivePacket ); // wait to receive packet
        clientIPAddress = receivePacket.getAddress();
        byte[] msgByte = receivePacket.getData();
        String str = new String(msgByte);
        String[] words = str.split(" ");  

         pcName= words[words.length-1];


        if (pcName.equals(pc1)){
            nextClient=  InetAddress.getByName("192.168.1.19");
            }
        else if (pcName.equals(pc2)) 
        {
        nextClient=  InetAddress.getByName("192.168.1.18");
        } else{
            nextClient=  InetAddress.getByName("192.168.1.17");
            }


        // display information from received packet 
        displayMessage( "\nPacket received:" +pcName +
           "\nFrom host: " + nextClient + 
           "\nHost port: " + receivePacket.getPort() + 
           "\nLength: " + receivePacket.getLength() + 
           "\nContaining:\n\t" + new String( receivePacket.getData(), 
              0, receivePacket.getLength() ) );

        sendPacketToClient( receivePacket, nextClient ); // send packet to client
     } // end try
     catch ( IOException ioException )
     {
        displayMessage( ioException + "\n" );
        ioException.printStackTrace();
     } // end catch
  } // end while
 } // end method waitForPackets

  // echo packet to client
  private void sendPacketToClient( DatagramPacket receivePacket, 
  InetAddress nextClient) 
  throws IOException
    {

    displayMessage( "\n\nsending data to client:"+pcName +
          "\nIP:" + clientIPAddress );

     // create packet to send
     DatagramPacket sendPacket = new DatagramPacket( 
     receivePacket.getData(), receivePacket.getLength(), 
      clientIPAddress, receivePacket.getPort() );

     socket.send( sendPacket ); // send packet to client
     displayMessage( "Packet sent\n" );
    } // end method sendPacketToClient

    // manipulates displayArea in the event-dispatch thread
    private void displayMessage( final String messageToDisplay )
    {
     SwingUtilities.invokeLater(
     new Runnable() 
     {
        public void run() // updates displayArea
        {
           displayArea.append( messageToDisplay ); // display message
        } // end method run
     } // end anonymous inner class
  ); // end call to SwingUtilities.invokeLater
  } // end method displayMessage
} // end class Server
4

1 に答える 1

1

問題はこの行にある可能性がありますsendPacketToClient

// create packet to send
DatagramPacket sendPacket = new DatagramPacket(receivePacket.getData(),
   receivePacket.getLength(),
   clientIPAddress,
   receivePacket.getPort());

おそらくInetAddress、送信側クライアントから取得したパケットではなく、解決済みのパケットを新しいパケットに入れる必要があります。

// create packet to send
DatagramPacket sendPacket = new DatagramPacket(receivePacket.getData(),
   receivePacket.getLength(),
   nextClient,
   receivePacket.getPort());
于 2012-07-18T07:10:56.803 に答える