下面这个JAVA做的 Server 为什么不能运行,点击“start”按钮 窗口就会自动卡死
/**ComputerNetworks*Purpose:Thisisaserver*thatcouldusetomonitorandrecievemessagefromo...
/*
* Computer Networks
* Purpose : This is a server
* that could use to monitor and recieve message from other PCs
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import javax.swing.*;
/**
* @version 1.0.0
* @author
*/
public class Server extends JFrame implements ActionListener{
// constructor
JButton button1;
JButton button2;
Thread thread;
DatagramSocket dataSocket;
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
static JTextArea text = new JTextArea("Messages : ");
public Server(){
super("Server");
setBounds(400,400,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
text.setEditable(false);
button1 = new JButton("Start Server");
button1.addActionListener(this);
button2 = new JButton("Close Server");
button2.addActionListener(this);
panelOne.add(button1);
panelOne.add(button2);
panelTwo.add(text);
add("North",panelOne);
add("Center",panelTwo);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
text.setText("Server 5001 ports is monitoring\n");
try
{
dataSocket = new DatagramSocket(5001); // $$$$$
}
catch(Exception exception)
{
exception.printStackTrace();
}
byte buf[] = new byte[1024];
DatagramPacket datagram = new DatagramPacket(buf, buf.length);
while(datagram.getData() != null)
{
try
{
dataSocket.receive(datagram);
text.append(new String(buf, 0, datagram.getLength()) + "\n");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}
if(e.getSource() == button2){
text.setText("Server 6000 ports is closed\n");
dataSocket.close();
}
}
public static void main(String args[]){
Server myServer = new Server();
}
} 展开
* Computer Networks
* Purpose : This is a server
* that could use to monitor and recieve message from other PCs
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import javax.swing.*;
/**
* @version 1.0.0
* @author
*/
public class Server extends JFrame implements ActionListener{
// constructor
JButton button1;
JButton button2;
Thread thread;
DatagramSocket dataSocket;
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
static JTextArea text = new JTextArea("Messages : ");
public Server(){
super("Server");
setBounds(400,400,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
text.setEditable(false);
button1 = new JButton("Start Server");
button1.addActionListener(this);
button2 = new JButton("Close Server");
button2.addActionListener(this);
panelOne.add(button1);
panelOne.add(button2);
panelTwo.add(text);
add("North",panelOne);
add("Center",panelTwo);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
text.setText("Server 5001 ports is monitoring\n");
try
{
dataSocket = new DatagramSocket(5001); // $$$$$
}
catch(Exception exception)
{
exception.printStackTrace();
}
byte buf[] = new byte[1024];
DatagramPacket datagram = new DatagramPacket(buf, buf.length);
while(datagram.getData() != null)
{
try
{
dataSocket.receive(datagram);
text.append(new String(buf, 0, datagram.getLength()) + "\n");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}
if(e.getSource() == button2){
text.setText("Server 6000 ports is closed\n");
dataSocket.close();
}
}
public static void main(String args[]){
Server myServer = new Server();
}
} 展开
4个回答
展开全部
dataSocket.receive(datagram);这句是一直在等待数据包吧,没有另外的程序给dataSocket这个对象所绑定的端口5001发包的话就会一直处于等待状态,所以你的程序在会在那边挂起的吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你一次给它渲染这么多控件!不死才怪
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
报什么错没有
追问
没有报错
追答
把连接提出去另外写试试,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询