Question

Bonjour les amis j'ai créé un programme bavardant UDP à travers lequel les clients peuvent communiquer sur le LAN.

J'ai créé un programme genaralized dire que je lance le même code avec nos différents ports. et l'adresse IP lorsque le réseau local

Mon problème est que ce code fonctionne bien en dessous sur localhost mais lorsque je tente de connecter deux machines ne marche pas ce code work..there est pas erreur de tel, mais encore les deux clients ne peuvent pas communiquer

J'ai coupé le firewalls.But je ne peux pas savoir pourquoi je ne peux pas communiquer entre deux machines

Le code est le suivant ::

    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)
            {
            }
        }
    }
}
Était-ce utile?

La solution

Voici un problème:

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

remoteHost.getLocalHost() retourne un InetAddress pour votre hôte local. Essayez simplement passer remoteHost au lieu de remoteHost.getLocalHost():

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

Autres conseils

effectue en plus de l'écho réponse que je voudrais ajouter que vous devez ajouter au moins e.printStackTrace(); à vos blocs catch.

Vérifiez également si les deux machines peuvent résoudre chaque autres noms d'hôtes en appelant nslookup hostname. Ou tout simplement ping hostname de chaque machine.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top