UDP Chat Problema
-
22-09-2019 - |
Domanda
Ciao amici ho creato un programma di chat UDP attraverso il quale i clienti possono comunicare tramite LAN.
Ho creato un programma genaralized cioè corro lo stesso codice con diversi numeri di porta. e l'indirizzo IP quando on LAN
Il mio problema è che questo codice di seguito funziona bene su localhost, ma quando provo a collegare due macchine di questo codice doesnt work..there è alcun errore, come tale, ma ancora i due client smussano comunicare
Inoltre ho spento la firewalls.But io sono in grado di scoprire il motivo per cui i cant comunicare tra due macchine
Il codice è il seguente ::
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)
{
}
}
}
}
Soluzione
Ecco un problema:
dgSock.send(new DatagramPacket(buff,buff.length,remoteHost.getLocalHost(),toPort));
remoteHost.getLocalHost()
restituisce un InetAddress per tuo host locale. Prova solo di passaggio remoteHost
invece di remoteHost.getLocalHost()
:
dgSock.send(new DatagramPacket(buff,buff.length,remoteHost,toPort));
Altri suggerimenti
In addtion eco di risposta che vorrei aggiungere che si dovrebbe aggiungere almeno e.printStackTrace();
ai tuoi blocchi catch.
Controllare anche che entrambe le macchine in grado di risolvere ogni altri nomi host chiamando nslookup hostname
. O semplicemente ping hostname
da ogni macchina.