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 631427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:57:50+00:00 2026-05-13T19:57:50+00:00

hello friends i have created a UDP chatting program through which the clients can

  • 0

hello friends i have created a UDP chatting program through which the clients can communicate over LAN.

I have created a genaralized program i.e. I run the same code with different port nos. and IP address when on LAN

My problem is that This code below works fine on localhost but when i try to connect two machines this code doesnt work..there is no error as of such but still the two clients cant communicate

I have also switched off the firewalls.But i am unable to find out why i cant communicate between two machines

The Code is as follows ::

    import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

class ChatApplDG extends Frame implements ActionListener
{

    TextField tf = new TextField(50);
    Button btn = new Button("Send");
    Button exit = new Button("Exit");
    TextArea ta = new TextArea(50,10);
    int fromPort, toPort;
    String hostName;

    DatagramSocket dgSock;

    public static void main(String args[]) throws Exception
    {
        ChatApplDG ca = new ChatApplDG();
        ca.startClient(args[0],Integer.parseInt(args[1]),Integer.parseInt(args[2]));
    }

    ChatApplDG()
    {
        Panel p = new Panel();
        add(p,BorderLayout.NORTH);
        p.add(tf);
        p.add(btn);
        p.add(exit);
        add(ta,BorderLayout.CENTER);
        btn.addActionListener(this);
        exit.addActionListener(this);
        setSize(500,300);
        show();
        ta.setEditable(false);
    }

    void startClient(String hName,int fPort,int tPort)
    {
        try
        {
            hostName = hName;
            fromPort=fPort;
            toPort=tPort;
            dgSock = new DatagramSocket(fromPort);
            ta.append("Ready To Send ...\n");
            RunningThreadDG rt = new RunningThreadDG(dgSock,ta);
            Thread thread = new Thread(rt);
            thread.start();
        }
        catch(Exception e)
        {
        }
    }


    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn)
        {
            try
            {
                byte buff[] = new byte[500];
                InetAddress remoteHost = InetAddress.getByName(hostName);
                buff=tf.getText().getBytes();
                dgSock.send(new DatagramPacket(buff,buff.length,remoteHost.getLocalHost(),toPort));
                ta.append("Send : " + tf.getText() + "\n");
                tf.setText("");
            }
            catch(Exception e)
            {
            }
        }
        else
        {
            System.exit(0);
        }
    }
}


class RunningThreadDG extends Frame implements Runnable
{
    DatagramSocket dgSock;
    TextArea ta;
    String str;

    RunningThreadDG(DatagramSocket dgs,TextArea t)
    {
        dgSock=dgs;
        ta=t;
    }   

    public void run()
    {
        byte[] buff = new byte[500];
        while(true)
        {
            try
            {
                DatagramPacket dgPack = new DatagramPacket(buff,buff.length);
                dgSock.receive(dgPack);
                ta.append("Received : " + new String(dgPack.getData(),0,dgPack.getLength()) + "\n");
            }
            catch(Exception e)
            {
            }
        }
    }
}
  • 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-05-13T19:57:50+00:00Added an answer on May 13, 2026 at 7:57 pm

    Here’s a problem:

    dgSock.send(new DatagramPacket(buff,buff.length,remoteHost.getLocalHost(),toPort));
    

    remoteHost.getLocalHost() returns an InetAddress for your local host. Try just passing remoteHost instead of remoteHost.getLocalHost():

    dgSock.send(new DatagramPacket(buff,buff.length,remoteHost,toPort));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.