UDP Chatting Problem
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)
{
}
}
}
}
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));
In addtion to echo's answer I would like to add that you should at least add e.printStackTrace();
to your catch blocks.
Also check whether both machines can resolve each others hostnames by calling nslookup hostname
. Or just ping hostname
from each machine.
精彩评论