UDP聊天问题

时间:2010-02-24 08:38:32

标签: java networking udp lan datagram

你好朋友我创建了一个UDP聊天程序,客户端可以通过它进行通信。

我创建了一个genaralized程序,即我使用不同的端口号运行相同的代码。和LAN上的IP地址

我的问题是,下面这段代码在localhost上运行正常,但是当我尝试连接两台机器时,这段代码无法正常工作..这样没有错误,但两个客户端仍然无法沟通

我也关闭了防火墙。但是我无法找出为什么我不能在两台机器之间进行通信

守则如下:

    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)
            {
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这是一个问题:

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

remoteHost.getLocalHost()返回您的本地主机的InetAddress。尝试传递remoteHost而不是remoteHost.getLocalHost()

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

答案 1 :(得分:1)

除了echo的答案之外,我想补充一点,你至少应该在你的catch块中添加e.printStackTrace();

同时检查两台计算机是否可以通过调用nslookup hostname来解析彼此的主机名。或者只是每台机器ping hostname