首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于java socket服务器的有关问题

2012-04-11 
关于java socket服务器的问题大家看一下我的代码packageserverimportjava.util.*importjava.awt.event.A

关于java socket服务器的问题
大家看一下我的代码
package   server;

import   java.util.*;
import   java.awt.event.ActionEvent;
import   java.awt.event.ActionListener;
import   java.io.*;
import   java.net.*;
import   javax.swing.*;
import   java.awt.*;


public   class   Name_Server   extends   JFrame   implements   ActionListener{
 
private   Vector   input_nodes   =   new   Vector();
private   JButton   server_start;
private   JButton   server_quit;
private   JLabel   copy;
private   JLabel   ip;
private   JLabel   port;
private   JTextField   i_type;
private   JTextField   p_type;

Container   contentpane;

private   ServerSocket   serv_soc;

public   Name_Server()
{
        this.setTitle( "Name   Server... ");
        this.setSize(350,300);
        this.setVisible(true);
       
        contentpane   =   this.getContentPane();
contentpane.setLayout(null);
copy   =   new   JLabel( "By   Peng   Li ");
copy.setBounds(20,20,200,15);
contentpane.add(copy);

ip   =   new   JLabel( "IP: ");
ip.setBounds(20,50,50,20);
contentpane.add(ip);

i_type   =   new   JTextField();
i_type.setBounds(50,50,100,20);
contentpane.add(i_type);

port   =   new   JLabel( "PORT: ");
port.setBounds(180,50,50,20);
contentpane.add(port);

p_type   =   new   JTextField();
p_type.setBounds(220,50,100,20);
contentpane.add(p_type);

server_start   =   new   JButton( "Connect ");//   Initializing   the   GUI   Component
server_start.setBounds(20,90,80,35);//   Positioning   the   GUI   Component.  
server_start.addActionListener(this);
contentpane.add(server_start);

server_quit   =   new   JButton( "Quit ");
server_quit.setBounds(150,90,80,35);//   Positioning   the   GUI   Component.
server_quit.addActionListener(this);
contentpane.add(server_quit);

i_type.setText( "127.0.0.1 ");
p_type.setText( "6666 ");
}

public   void   actionPerformed(ActionEvent   ae)  
{
if(ae.getSource()   ==   server_start)
{
this.server_start.setEnabled(false);  
        this.listen();
}
else   if(ae.getSource()   ==   server_quit)
{
try
{
        this.serv_soc.close();
}
catch(Exception   e)
{
System.out.println( "Server   Close "+e);
}
}
}
public   void   listen()
{
try
{
          this.serv_soc   =   new   ServerSocket(Integer.parseInt(p_type.getText()));
          while(true)
  {
Socket   soc   =   serv_soc.accept();
System.out.println( "Accept ");
BufferedReader   name_server_in   =   new   BufferedReader(new   InputStreamReader(soc.getInputStream()));


DataOutputStream   name_server_out   =   new   DataOutputStream(soc.getOutputStream());
String   r   ;
r=name_server_in.readLine();              
        String   div[]   =   r.split( ", ");
       
String   replysentence   =new   String();
if(input_nodes.size()==0)
{
          replysentence   =   "null ";
}
else
{
          for(int   i=0;   i <input_nodes.size();   i++)
          {
        if(i <input_nodes.size()-1)
        replysentence   =   replysentence   +   ((Node_Info)input_nodes.get(i)).getIP()+ "/ "+((Node_Info)input_nodes.get(i)).getPort()+ "; ";
                else
                replysentence   =   replysentence   +   ((Node_Info)input_nodes.get(i)).getIP()+ "/ "+((Node_Info)input_nodes.get(i)).getPort();
            }
  }
  Node_Info   node   =   new   Node_Info();
  node.setIP(div[0]);
  node.setPort(div[1]);
  input_nodes.add(node);
  name_server_out.writeBytes(replysentence+ '\n ');
}
}
        catch(Exception   e)
        {
        System.out.println( "Name_Server: "+e);
        }
}

public   static   void   main(String[]   args)   throws   Exception
{
Name_Server   name_server   =   new   Name_Server();
//name_server.listen();
}      
}
为什么触发开始,这个服务端好像有一种死掉的感觉,界面都没有了,关也关不掉,不知道是什么原因?请大侠帮忙~~

[解决办法]
太长了,看着就晕,不过还是帮你顶一下.
[解决办法]
Node_Info类好像有问题……
[解决办法]
up
[解决办法]
为listen()方法单独开一线程,把它置于run()中.

[解决办法]
恩,线程单独开一个。不然肯定要挂起来的。。

热点排行