Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:49:44+00:00 2026-06-07T19:49:44+00:00

so i have 3 clients and 1 server that should route the messages to

  • 0

so i have 3 clients and 1 server that should route the messages to the correct client. the client send a message along with the name of the other client that should get the message. and server should compare the name of the receiving client and replace the correct IP in the packet and send the message to the correct client. the problem is that the server doesn’t replace the IP so the message is not delivered. Please help me to fix the prblem. here is my server code:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:49:46+00:00Added an answer on June 7, 2026 at 7:49 pm

    Might the problem be this line in sendPacketToClient

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

    You should probably be putting the resolved InetAddress in the new packet instead of the one you got from the sending client.

    // create packet to send
    DatagramPacket sendPacket = new DatagramPacket(receivePacket.getData(),
       receivePacket.getLength(),
       nextClient,
       receivePacket.getPort());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that receives various xml messages from clients (one thread per
I have some Lotus Notes clients that looses server connection. They suddenly get server
I have a server that connects to multiple clients using TCP/IP connections, using C
I have a server that is serving files to several windows clients using the
I have coded a server in Java that will have several clients connected to
I have a desktop webapi server that I want to expose to clients through
I have a client-server application that utilises MSMQ and NServiceBus for messaging. During some
We have a Flex client and a server that is using the Spring/Blazeds project.
I have a client and a server app on Android (that uses ZeroC -
I have an application that runs on a client's server built on a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.