package CheviC_UDP;import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;public class UDPChat extends Frame
{ Label label_myPort = null; TextField textF_myPort = null; Button but_sureMyPort = null; TextArea textA_Chat = null; ScrollPane scrollPane = null; Label label_ip = null; TextField textF_ip = null; Label label_port = null; TextField textF_port = null; Label label_name = null; TextField textF_name = null; TextField textF_send = null; Button butSend = null; DatagramSocket socket = null; AcceptMsg am = null; // 构造函数
public UDPChat()
{
super("CheviC UDP Chat");
setBounds(400, 400, 600, 350);
setLayout(new BorderLayout());
setVisible(true);
init();
actionAdd();
acceptMsg();
} // 添加组件
void init()
{ Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
label_myPort = new Label("set your port here:");
p1.add(label_myPort);
textF_myPort = new TextField("5482", 3);
p1.add(textF_myPort);
but_sureMyPort = new Button("set");
p1.add(but_sureMyPort);
label_ip = new Label("           set the connect ip here:");
p1.add(label_ip);
textF_ip = new TextField("127.0.0.1",11);
p1.add(textF_ip);
label_port = new Label("port:");
p1.add(label_port);
textF_port = new TextField("5483", 3);
p1.add(textF_port);
add(p1, BorderLayout.NORTH); textA_Chat = new TextArea(10, 40);
scrollPane = new ScrollPane();
scrollPane.add(textA_Chat);
add(scrollPane, BorderLayout.CENTER); Panel p = new Panel();
label_name = new Label("name:");
p.add(label_name);
textF_name = new TextField("Naruto", 6);
p.add(textF_name);
textF_send = new TextField(51);
p.add(textF_send);
butSend = new Button("  Send  ");
p.add(butSend);
add(p, BorderLayout.SOUTH);
} // 事件添加
void actionAdd()
{
// 窗体关闭
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}); // 设置端口
but_sureMyPort.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setMyPort();
}
}); // 发送信息
butSend.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
send();
}
}); } // 设置本机端口
void setMyPort()
{
try
{
int port = Integer.parseInt(textF_myPort.getText());
socket = new DatagramSocket(port); } catch (Exception ex)
{
textF_myPort.setText("error");
}
} // 发送信息
void send()
{
try
{
Calendar time = Calendar.getInstance();
String sendMsg = textF_name.getText() + "("
+ time.getTime().toString() + "):\r\n"
+ textF_send.getText() + "\r\n";
byte[] msg = sendMsg.getBytes();
DatagramPacket out = new DatagramPacket(msg, 0, msg.length,
InetAddress.getByName(textF_ip.getText().trim()), Integer.parseInt(textF_port.getText()));
socket.send(out);
showMsg(sendMsg); } catch (Exception ex)
{
textA_Chat.append(ex.toString()+"\r\n");
} } // 接收消息
void acceptMsg()
{
am = new AcceptMsg();
am.start(); //原来写错写成了am.run();
}

//显示MSG
void showMsg(String msg)
{
textA_Chat.append(msg);
} // 接收信息的类
class AcceptMsg extends Thread
{
byte[] msg = null; AcceptMsg()
{
msg = new byte[256];
} @Override
public void run()
{
if (socket != null)
{
while (true)
{
try
{
DatagramPacket in = new DatagramPacket(msg, msg.length);
socket.receive(in);
showMsg(in.getData().toString());
} catch (Exception ex)
{
// TODO 接收错误时的处理
textA_Chat.append("sorry,there is something wrong.");
}
}
}
}
} public static void main(String[] args)
{
new UDPChat();
}}
各位就帮我看看吧,问题是出在接收那个线程上

解决方案 »

  1. 各位就帮我看看吧,问题是出在接收那个线程上?
    答:程序代码的次序上有问题。程序已修改好。运行正常。可互相发送与接收。
    修改的地方用//《======= 标示了出来
    代码如下:package CheviC_UDP;import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.*;public class UDPChat extends Frame
    {    Label label_myPort = null;    TextField textF_myPort = null;    Button but_sureMyPort = null;    TextArea textA_Chat = null;    ScrollPane scrollPane = null;    Label label_ip = null;    TextField textF_ip = null;    Label label_port = null;    TextField textF_port = null;    Label label_name = null;    TextField textF_name = null;    TextField textF_send = null;    Button butSend = null;    DatagramSocket socket = null;    AcceptMsg am = null;    // 构造函数
        public UDPChat()
        {
            super("CheviC UDP Chat");
            setBounds(400, 400, 600, 350);
            setLayout(new BorderLayout());
           
            init();
            actionAdd();
          //  acceptMsg();  //<=========
            setVisible(true);  //<=========
        }    // 添加组件
        void init()
        {        Panel p1 = new Panel();
            p1.setLayout(new FlowLayout(FlowLayout.LEFT));
            label_myPort = new Label("set your port here:");
            p1.add(label_myPort);
            textF_myPort = new TextField("5482", 3);
            p1.add(textF_myPort);
            but_sureMyPort = new Button("set");
            p1.add(but_sureMyPort);
            label_ip = new Label("           set the connect ip here:");
            p1.add(label_ip);
            textF_ip = new TextField("127.0.0.1",11);
            p1.add(textF_ip);
            label_port = new Label("port:");
            p1.add(label_port);
            textF_port = new TextField("5483", 3);
            p1.add(textF_port);
            add(p1, BorderLayout.NORTH);        textA_Chat = new TextArea(10, 40);
            scrollPane = new ScrollPane();
            scrollPane.add(textA_Chat);
            add(scrollPane, BorderLayout.CENTER);        Panel p = new Panel();
            label_name = new Label("name:");
            p.add(label_name);
            textF_name = new TextField("Naruto", 6);
            p.add(textF_name);
            textF_send = new TextField(51);
            p.add(textF_send);
            butSend = new Button("  Send  ");
            p.add(butSend);
            add(p, BorderLayout.SOUTH);
        }    // 事件添加
        void actionAdd()
        {
            // 窗体关闭
            addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });        // 设置端口
            but_sureMyPort.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    setMyPort();
                }
            });        // 发送信息
            butSend.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    send();
                }
            });    }    // 设置本机端口
        void setMyPort()
        {
            try
            {
                int port = Integer.parseInt(textF_myPort.getText());
                socket = new DatagramSocket(port);            
               
                acceptMsg();//<=========
                        } catch (Exception ex)
            {
                textF_myPort.setText("error");
            }
        }    // 发送信息
        void send()
        {
            try
            {
                Calendar time = Calendar.getInstance();
                String sendMsg = textF_name.getText() + "("
                        + time.getTime().toString() + "):\r\n"
                        + textF_send.getText() + "\r\n";
                byte[] msg = sendMsg.getBytes();
                DatagramPacket out = new DatagramPacket(msg, 0, msg.length,
                        InetAddress.getByName(textF_ip.getText().trim()), Integer.parseInt(textF_port.getText()));
                socket.send(out);
                showMsg(sendMsg);        } catch (Exception ex)
            {
                textA_Chat.append(ex.toString()+"\r\n");
            }    }    // 接收消息
        void acceptMsg()
        {
            am = new AcceptMsg();
            am.start();                    //原来写错写成了am.run();
        }
        
        //显示MSG
        void showMsg(String msg)
        {
            textA_Chat.append(msg);
        }    // 接收信息的类
        class AcceptMsg extends Thread
        {
            byte[] msg = null;        AcceptMsg()
            {
                msg = new byte[256];
            }        @Override
            public void run()
            {
                if (socket != null)
                {
                
                    while (true)
                    {
                        try
                        {
                            DatagramPacket in = new DatagramPacket(msg, msg.length);
                            socket.receive(in);
                            showMsg(new String(in.getData(),in.getOffset(),in.getLength()));//<=========
                        } catch (Exception ex)
                        {
                            // TODO 接收错误时的处理
                            textA_Chat.append("sorry,there is something wrong.");
                        }
                    }
                }
            }
        }    public static void main(String[] args)
        {
            new UDPChat();
        }}
      

类似问题 »